Skip to content

Commit 241918a

Browse files
committed
Update to PHPStan 2.x
Fix issues found: - If the input to a body parser callable isn't a string, then return null. - If we fail to bind $this to a callable, then assign the callable. - Ensure that the uri key from stream_get_meta_data() is a string.
1 parent 7dea3ac commit 241918a

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

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: 12 additions & 2 deletions
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> */
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

@@ -650,7 +660,7 @@ public function getServerParam(string $key, $default = null)
650660
public function registerMediaTypeParser(string $mediaType, callable $callable): ServerRequestInterface
651661
{
652662
if ($callable instanceof Closure) {
653-
$callable = $callable->bindTo($this);
663+
$callable = $callable->bindTo($this) ?? $callable;
654664
}
655665

656666
$this->bodyParsers[$mediaType] = $callable;

0 commit comments

Comments
 (0)