Skip to content

Commit 0583b77

Browse files
authored
Update to PHPStan 2.x (#247)
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 0583b77

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
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: 5 additions & 4 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);
@@ -78,7 +79,7 @@ final public function __construct(ServerRequestInterface $serverRequest)
7879
$this->registerMediaTypeParser('application/xml', $xmlParserCallable);
7980
$this->registerMediaTypeParser('text/xml', $xmlParserCallable);
8081

81-
$this->registerMediaTypeParser('application/x-www-form-urlencoded', function ($input) {
82+
$this->registerMediaTypeParser('application/x-www-form-urlencoded', function (string $input) {
8283
parse_str($input, $data);
8384
return $data;
8485
});
@@ -214,7 +215,7 @@ public function getQueryParams(): array
214215
{
215216
$queryParams = $this->serverRequest->getQueryParams();
216217

217-
if (is_array($queryParams) && !empty($queryParams)) {
218+
if (!empty($queryParams)) {
218219
return $queryParams;
219220
}
220221

0 commit comments

Comments
 (0)