diff --git a/composer.json b/composer.json index ae232f5..337b438 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,7 @@ }, "require-dev": { "jakub-onderka/php-parallel-lint": "0.8.*", - "phpstan/phpstan": "^0.11.5", + "phpstan/phpstan": "^1.10.3", "squizlabs/php_codesniffer": "^3.4", "phperf/xh-tool": "^1.1", "phpunit/phpunit": "^4.8" diff --git a/src/Check/ReturnCheck.php b/src/Check/ReturnCheck.php index 10b0eea..36d6d81 100644 --- a/src/Check/ReturnCheck.php +++ b/src/Check/ReturnCheck.php @@ -32,20 +32,26 @@ public function check(FileInfo $file) continue; } - if ($method['return'] === 'array' && substr($method['docblock']['return'], -2) === '[]') { + $returnTypes = $method['docblock']['return']; + $methodTypes = $method['return']; + + if ($method['return'] === 'array' + && !is_array($returnTypes) + && substr($returnTypes, -2) === '[]' + ) { // Do nothing because this is fine. continue; } - if ($method['return'] !== $method['docblock']['return']) { + if ($methodTypes !== $returnTypes) { $this->fileStatus->add( new ReturnMismatchWarning( $file->getFileName(), $name, $method['line'], $name, - is_array($method['return']) ? implode('|', $method['return']) : $method['return'], - $method['docblock']['return'] + is_array($methodTypes) ? implode('|', $methodTypes) : $methodTypes, + is_array($returnTypes) ? implode('|', $returnTypes) : $returnTypes ) ); continue; diff --git a/src/FileParser/FileParser.php b/src/FileParser/FileParser.php index 606ece8..2f4b660 100644 --- a/src/FileParser/FileParser.php +++ b/src/FileParser/FileParser.php @@ -14,6 +14,7 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Namespace_; use PhpParser\Node\Stmt\Use_; +use PhpParser\Node\UnionType; use PhpParser\NodeAbstract; use PhpParser\Parser; @@ -120,6 +121,8 @@ protected function processStatements($file, array $statements, $prefix = '') if ($type instanceof NullableType) { $type = $type->type->toString(); + } elseif ($type instanceof UnionType) { + $type = trim(implode('|', $type->types)); } elseif ($type instanceof NodeAbstract) { $type = $type->toString(); } @@ -154,6 +157,8 @@ protected function processStatements($file, array $statements, $prefix = '') if ($type instanceof NullableType) { $type = $type->type->toString(); + } elseif ($type instanceof UnionType) { + $type = trim(implode('|', $type->types)); } elseif ($type instanceof NodeAbstract) { $type = $type->toString(); }