Skip to content

Commit d9867ce

Browse files
committed
Update to PHPStan 2.x
Fix issues found: - Define the type of the bodyParsers array. - If the input to a body parser callable isn't a string, then return null. - ServerRequest::getQueryParams() always returns an array, so we don't need to test for it. - Ensure that the uri key in the file metadata is a string before using it.
1 parent fd26ab6 commit d9867ce

4 files changed

Lines changed: 16 additions & 4 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"laminas/laminas-diactoros": "^3.1.0",
4646
"nyholm/psr7": "^1.8.1",
4747
"php-http/psr7-integration-tests": "^1.3.0",
48-
"phpstan/phpstan": "^1.10",
48+
"phpstan/phpstan": "^2.0",
4949
"phpunit/phpunit": "^9.6",
5050
"doctrine/instantiator": "^1.3.1",
5151
"squizlabs/php_codesniffer": "^3.9"

phpstan.neon.dist

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
parameters:
2-
checkMissingIterableValueType: false
32
level: max
43
paths:
54
- src
5+
ignoreErrors:
6+
-
7+
identifier: missingType.iterableValue

src/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public function withFileDownload($file, ?string $name = null, $contentType = tru
270270
? $file->getMetadata()
271271
: stream_get_meta_data($file);
272272

273-
if (is_array($metaData) && isset($metaData['uri'])) {
273+
if (is_array($metaData) && isset($metaData['uri']) && is_string($metaData['uri'])) {
274274
$uri = $metaData['uri'];
275275
if ('php://' !== substr($uri, 0, 6)) {
276276
$fileName = basename($uri);

src/ServerRequest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class ServerRequest implements ServerRequestInterface
4040
{
4141
protected ServerRequestInterface $serverRequest;
4242

43+
/** @var array<string, callable|null> */
4344
protected array $bodyParsers;
4445

4546
/**
@@ -50,6 +51,9 @@ final public function __construct(ServerRequestInterface $serverRequest)
5051
$this->serverRequest = $serverRequest;
5152

5253
$this->registerMediaTypeParser('application/json', function ($input) {
54+
if (!is_string($input)) {
55+
return null;
56+
}
5357
$result = json_decode($input, true);
5458

5559
if (!is_array($result)) {
@@ -60,6 +64,9 @@ final public function __construct(ServerRequestInterface $serverRequest)
6064
});
6165

6266
$xmlParserCallable = function ($input) {
67+
if (!is_string($input)) {
68+
return null;
69+
}
6370
$backup = self::disableXmlEntityLoader(true);
6471
$backup_errors = libxml_use_internal_errors(true);
6572
$result = simplexml_load_string($input);
@@ -79,6 +86,9 @@ final public function __construct(ServerRequestInterface $serverRequest)
7986
$this->registerMediaTypeParser('text/xml', $xmlParserCallable);
8087

8188
$this->registerMediaTypeParser('application/x-www-form-urlencoded', function ($input) {
89+
if (!is_string($input)) {
90+
return null;
91+
}
8292
parse_str($input, $data);
8393
return $data;
8494
});
@@ -214,7 +224,7 @@ public function getQueryParams(): array
214224
{
215225
$queryParams = $this->serverRequest->getQueryParams();
216226

217-
if (is_array($queryParams) && !empty($queryParams)) {
227+
if (!empty($queryParams)) {
218228
return $queryParams;
219229
}
220230

0 commit comments

Comments
 (0)