Skip to content

Commit ea499bc

Browse files
committed
fixed PHPStan errors
1 parent 3dcd40c commit ea499bc

10 files changed

Lines changed: 134 additions & 11 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"nette/tester": "^2.6",
2424
"nette/security": "^3.0",
2525
"tracy/tracy": "^2.8",
26-
"phpstan/phpstan-nette": "^2.0@stable"
26+
"phpstan/phpstan": "^2.0@stable"
2727
},
2828
"conflict": {
2929
"nette/di": "<3.0.3",

phpstan-baseline.neon

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: '#^Parameter \#1 \$callback of function array_map expects \(callable\(Nette\\Utils\\ImageType\)\: mixed\)\|null, Closure\(1\|2\|3\|6\|18\|19\)\: string given\.$#'
5+
identifier: argument.type
6+
count: 1
7+
path: src/Http/FileUpload.php
8+
9+
-
10+
message: '#^Unknown parameter \$sameSite in call to method Nette\\Http\\IResponse\:\:setCookie\(\)\.$#'
11+
identifier: argument.unknown
12+
count: 1
13+
path: src/Http/Helpers.php
14+
15+
-
16+
message: '#^Right side of && is always true\.$#'
17+
identifier: booleanAnd.rightAlwaysTrue
18+
count: 1
19+
path: src/Http/Request.php
20+
21+
-
22+
message: '#^Strict comparison using \=\=\= between 2 and 2 will always evaluate to true\.$#'
23+
identifier: identical.alwaysTrue
24+
count: 1
25+
path: src/Http/Request.php
26+
27+
-
28+
message: '#^Offset ''name'' on non\-empty\-array in isset\(\) always exists and is not nullable\.$#'
29+
identifier: isset.offset
30+
count: 1
31+
path: src/Http/RequestFactory.php
32+
33+
-
34+
message: '#^Variable \$_FILES on left side of \?\? always exists and is not nullable\.$#'
35+
identifier: nullCoalesce.variable
36+
count: 1
37+
path: src/Http/RequestFactory.php
38+
39+
-
40+
message: '#^Method Nette\\Http\\IResponse\:\:setCookie\(\) invoked with 8 parameters, 3\-7 required\.$#'
41+
identifier: arguments.count
42+
count: 1
43+
path: src/Http/Session.php
44+
45+
-
46+
message: '#^Offset ''samesite'' on array\{lifetime\: int\<0, max\>, path\: non\-falsy\-string, domain\: string, secure\: bool, httponly\: bool, samesite\: ''Lax''\|''lax''\|''None''\|''none''\|''Strict''\|''strict''\} on left side of \?\? always exists and is not nullable\.$#'
47+
identifier: nullCoalesce.offset
48+
count: 1
49+
path: src/Http/Session.php
50+
51+
-
52+
message: '#^Parameter \#1 \$callbacks of static method Nette\\Utils\\Arrays\:\:invoke\(\) expects iterable\<callable\(\)\: mixed\>, list\<callable\(Nette\\Http\\Session\)\: void\> given\.$#'
53+
identifier: argument.type
54+
count: 2
55+
path: src/Http/Session.php
56+
57+
-
58+
message: '#^Parameter \#3 \$onError of static method Nette\\Utils\\Callback\:\:invokeSafe\(\) expects callable\(string, int\)\: \(bool\|null\), Closure\(string\)\: void given\.$#'
59+
identifier: argument.type
60+
count: 1
61+
path: src/Http/Session.php
62+
63+
-
64+
message: '#^Method Nette\\Http\\Url\:\:export\(\) return type has no value type specified in iterable type array\.$#'
65+
identifier: missingType.iterableValue
66+
count: 1
67+
path: src/Http/Url.php
68+
69+
-
70+
message: '#^Method Nette\\Http\\UrlImmutable\:\:export\(\) return type has no value type specified in iterable type array\.$#'
71+
identifier: missingType.iterableValue
72+
count: 1
73+
path: src/Http/UrlImmutable.php

phpstan.neon

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
parameters:
2-
level: 5
2+
level: 6
33

44
paths:
55
- src
6+
- tests/types
7+
8+
excludePaths:
9+
- src/Http/UserStorage.php # deprecated, implements interface from optional nette/security
10+
11+
checkMissingCallableSignature: true
12+
13+
includes:
14+
- phpstan-baseline.neon

src/Bridges/HttpDI/HttpExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function loadConfiguration(): void
5050
{
5151
$builder = $this->getContainerBuilder();
5252
$config = $this->config;
53+
\assert($config instanceof \stdClass);
5354

5455
$requestFactory = $builder->addDefinition($this->prefix('requestFactory'))
5556
->setFactory(Nette\Http\RequestFactory::class)
@@ -98,6 +99,7 @@ public function loadConfiguration(): void
9899
private function sendHeaders(): void
99100
{
100101
$config = $this->config;
102+
\assert($config instanceof \stdClass);
101103
$headers = array_map(strval(...), $config->headers);
102104

103105
if (isset($config->frames) && $config->frames !== true && !isset($headers['X-Frame-Options'])) {

src/Bridges/HttpDI/SessionExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function loadConfiguration(): void
4444
{
4545
$builder = $this->getContainerBuilder();
4646
$config = $this->config;
47+
\assert($config instanceof \stdClass);
4748

4849
$session = $builder->addDefinition($this->prefix('session'))
4950
->setFactory(Nette\Http\Session::class);

src/Http/FileUpload.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function __construct(array|string|null $value)
5151
$value = [
5252
'name' => basename($value),
5353
'full_path' => $value,
54-
'size' => filesize($value),
54+
'size' => filesize($value) ?: 0,
5555
'tmp_name' => $value,
5656
'error' => UPLOAD_ERR_OK,
5757
];
@@ -141,7 +141,7 @@ public function getSuggestedExtension(): ?string
141141
}
142142
[, , $type] = Nette\Utils\Helpers::falseToNull(@getimagesize($this->tmpName)); // @ - files smaller than 12 bytes causes read error
143143
if ($type) {
144-
return $this->extension = image_type_to_extension($type, include_dot: false);
144+
return $this->extension = image_type_to_extension($type, include_dot: false) ?: null;
145145
}
146146
$this->extension = false;
147147
}
@@ -274,9 +274,8 @@ public function getImageFileExtension(): ?string
274274
*/
275275
public function getContents(): ?string
276276
{
277-
// future implementation can try to work around safe_mode and open_basedir limitations
278277
return $this->isOk()
279-
? file_get_contents($this->tmpName)
278+
? (string) file_get_contents($this->tmpName)
280279
: null;
281280
}
282281
}

src/Http/RequestFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function fromGlobals(): Request
8686
$this->getMethod(),
8787
$remoteAddr,
8888
$remoteHost,
89-
fn(): string => file_get_contents('php://input'),
89+
fn(): string => (string) file_get_contents('php://input'),
9090
);
9191
}
9292

src/Http/Response.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
use Nette;
1313
use Nette\Utils\DateTime;
14-
use function array_filter, header, header_remove, headers_list, headers_sent, htmlspecialchars, http_response_code, ini_get, is_int, ltrim, ob_get_length, ob_get_status, preg_match, rawurlencode, setcookie, str_replace, strcasecmp, strlen, strncasecmp, strpos, substr, time;
14+
use function array_filter, header, header_remove, headers_list, headers_sent, htmlspecialchars, http_response_code, ini_get, is_int, ltrim, ob_get_length, ob_get_status, preg_match, rawurlencode, setcookie, str_replace, strcasecmp, strlen, strncasecmp, substr, time;
1515
use const ENT_IGNORE, ENT_QUOTES, PHP_SAPI;
1616

1717

@@ -217,8 +217,10 @@ public function getHeaders(): array
217217
{
218218
$headers = [];
219219
foreach (headers_list() as $header) {
220-
$a = strpos($header, ':');
221-
$headers[substr($header, 0, $a)] = substr($header, $a + 2);
220+
$parts = explode(':', $header, 2);
221+
if (isset($parts[1])) {
222+
$headers[$parts[0]] = ltrim($parts[1]);
223+
}
222224
}
223225

224226
return $headers;

src/Http/Url.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ private static function idnHostToUnicode(string $host): string
387387
}
388388

389389
trigger_error('PHP extension intl is not loaded or is too old', E_USER_WARNING);
390+
return $host;
390391
}
391392

392393

@@ -417,7 +418,7 @@ public static function unescape(string $s, string $reserved = '%;/?:@&=+$,'): st
417418
public static function parseQuery(string $s): array
418419
{
419420
$s = str_replace(['%5B', '%5b'], '[', $s);
420-
$sep = preg_quote(ini_get('arg_separator.input'));
421+
$sep = preg_quote(ini_get('arg_separator.input') ?: '&');
421422
$s = preg_replace("#([$sep])([^[$sep=]+)([^$sep]*)#", '&0[$2]$3', '&' . $s);
422423
parse_str($s, $res);
423424
return $res[0] ?? [];

tests/types/http-types.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* PHPStan type tests for Http.
5+
* Run: vendor/bin/phpstan analyse tests/types
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
use Nette\Http\FileUpload;
11+
use Nette\Http\SessionSection;
12+
use function PHPStan\Testing\assertType;
13+
14+
15+
function testSessionSectionIterator(SessionSection $section): void
16+
{
17+
foreach ($section as $key => $value) {
18+
assertType('string', $key);
19+
assertType('mixed', $value);
20+
}
21+
}
22+
23+
24+
function testSessionSectionArrayAccess(SessionSection $section): void
25+
{
26+
$section->remove();
27+
$value = $section['key'];
28+
assertType('mixed', $value);
29+
}
30+
31+
32+
function testFileUploadGetImageSize(FileUpload $upload): void
33+
{
34+
$size = $upload->getImageSize();
35+
assertType('array{int, int}|null', $size);
36+
}

0 commit comments

Comments
 (0)