Skip to content

Commit 061e6ed

Browse files
committed
fixed PHPStan errors
1 parent ba36090 commit 061e6ed

10 files changed

Lines changed: 120 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: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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: '#^Method Nette\\Http\\Url\:\:export\(\) return type has no value type specified in iterable type array\.$#'
53+
identifier: missingType.iterableValue
54+
count: 1
55+
path: src/Http/Url.php
56+
57+
-
58+
message: '#^Method Nette\\Http\\UrlImmutable\:\:export\(\) return type has no value type specified in iterable type array\.$#'
59+
identifier: missingType.iterableValue
60+
count: 1
61+
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
@@ -48,6 +48,7 @@ public function loadConfiguration(): void
4848
{
4949
$builder = $this->getContainerBuilder();
5050
$config = $this->config;
51+
\assert($config instanceof \stdClass);
5152

5253
$requestFactory = $builder->addDefinition($this->prefix('requestFactory'))
5354
->setFactory(Nette\Http\RequestFactory::class)
@@ -96,6 +97,7 @@ public function loadConfiguration(): void
9697
private function sendHeaders(): void
9798
{
9899
$config = $this->config;
100+
\assert($config instanceof \stdClass);
99101
$headers = array_map(strval(...), $config->headers);
100102

101103
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
@@ -42,6 +42,7 @@ public function loadConfiguration(): void
4242
{
4343
$builder = $this->getContainerBuilder();
4444
$config = $this->config;
45+
\assert($config instanceof \stdClass);
4546

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

src/Http/FileUpload.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function __construct(array|string|null $value)
4949
$value = [
5050
'name' => basename($value),
5151
'full_path' => $value,
52-
'size' => filesize($value),
52+
'size' => filesize($value) ?: 0,
5353
'tmp_name' => $value,
5454
'error' => UPLOAD_ERR_OK,
5555
];
@@ -139,7 +139,7 @@ public function getSuggestedExtension(): ?string
139139
}
140140
[, , $type] = Nette\Utils\Helpers::falseToNull(@getimagesize($this->tmpName)); // @ - files smaller than 12 bytes causes read error
141141
if ($type) {
142-
return $this->extension = image_type_to_extension($type, include_dot: false);
142+
return $this->extension = image_type_to_extension($type, include_dot: false) ?: null;
143143
}
144144
$this->extension = false;
145145
}
@@ -272,9 +272,8 @@ public function getImageFileExtension(): ?string
272272
*/
273273
public function getContents(): ?string
274274
{
275-
// future implementation can try to work around safe_mode and open_basedir limitations
276275
return $this->isOk()
277-
? file_get_contents($this->tmpName)
276+
? (string) file_get_contents($this->tmpName)
278277
: null;
279278
}
280279
}

src/Http/RequestFactory.php

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

src/Http/Response.php

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

1010
use Nette;
1111
use Nette\Utils\DateTime;
12-
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;
12+
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;
1313
use const ENT_IGNORE, ENT_QUOTES, PHP_SAPI;
1414

1515

@@ -215,8 +215,10 @@ public function getHeaders(): array
215215
{
216216
$headers = [];
217217
foreach (headers_list() as $header) {
218-
$a = strpos($header, ':');
219-
$headers[substr($header, 0, $a)] = substr($header, $a + 2);
218+
$parts = explode(':', $header, 2);
219+
if (isset($parts[1])) {
220+
$headers[$parts[0]] = ltrim($parts[1]);
221+
}
220222
}
221223

222224
return $headers;

src/Http/Url.php

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

387387
trigger_error('PHP extension intl is not loaded or is too old', E_USER_WARNING);
388+
return $host;
388389
}
389390

390391

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

tests/types/http-types.php

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

0 commit comments

Comments
 (0)