Skip to content

Commit da09e61

Browse files
committed
Update to PHPStan 2.x
Fix issues found: - Define the type of the bodyParsers array. - Type hint the input to a body parser to string as that's what it always is. - 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. Remove tests for string and use a type hint
1 parent fd26ab6 commit da09e61

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
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: 7 additions & 3 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|null> */
4344
protected array $bodyParsers;
4445

4546
/**
@@ -49,7 +50,7 @@ final public function __construct(ServerRequestInterface $serverRequest)
4950
{
5051
$this->serverRequest = $serverRequest;
5152

52-
$this->registerMediaTypeParser('application/json', function ($input) {
53+
$this->registerMediaTypeParser('application/json', function (string $input) {
5354
$result = json_decode($input, true);
5455

5556
if (!is_array($result)) {
@@ -59,7 +60,7 @@ final public function __construct(ServerRequestInterface $serverRequest)
5960
return $result;
6061
});
6162

62-
$xmlParserCallable = function ($input) {
63+
$xmlParserCallable = function (string $input) {
6364
$backup = self::disableXmlEntityLoader(true);
6465
$backup_errors = libxml_use_internal_errors(true);
6566
$result = simplexml_load_string($input);
@@ -79,6 +80,9 @@ final public function __construct(ServerRequestInterface $serverRequest)
7980
$this->registerMediaTypeParser('text/xml', $xmlParserCallable);
8081

8182
$this->registerMediaTypeParser('application/x-www-form-urlencoded', function ($input) {
83+
if (!is_string($input)) {
84+
return null;
85+
}
8286
parse_str($input, $data);
8387
return $data;
8488
});
@@ -214,7 +218,7 @@ public function getQueryParams(): array
214218
{
215219
$queryParams = $this->serverRequest->getQueryParams();
216220

217-
if (is_array($queryParams) && !empty($queryParams)) {
221+
if (!empty($queryParams)) {
218222
return $queryParams;
219223
}
220224

tests/ServerRequestTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,25 @@ public function testGetParsedBodyJson()
741741
}
742742
}
743743

744+
public function testGetParsedBodyJsonWithInvalidStream()
745+
{
746+
foreach ($this->factoryProviders as $factoryProvider) {
747+
/** @var Psr17FactoryProvider $provider */
748+
$provider = new $factoryProvider();
749+
$decoratedServerRequestFactory = new DecoratedServerRequestFactory($provider->getServerRequestFactory());
750+
751+
$streamFactory = $provider->getStreamFactory();
752+
$stream = $streamFactory->createStream(0);
753+
754+
$request = $decoratedServerRequestFactory->createServerRequest('POST', 'https://google.com');
755+
$request = $request
756+
->withHeader('Content-Type', 'application/json;charset=utf8')
757+
->withBody($stream);
758+
759+
$this->assertEquals(['foo' => 'bar'], $request->getParsedBody());
760+
}
761+
}
762+
744763
public function testGetParsedBodyInvalidJson()
745764
{
746765
foreach ($this->factoryProviders as $factoryProvider) {

0 commit comments

Comments
 (0)