From 876d1465e26c23184a048b24e6a148c63580ac32 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Wed, 3 Jun 2020 11:26:51 +0200 Subject: [PATCH] Update nikic/php-parser to 4.5.0 --- composer.json | 2 +- src/Analyser/MutatingScope.php | 8 +++-- src/Analyser/NodeScopeResolver.php | 15 ++++---- .../Analyser/NodeScopeResolverTest.php | 17 +++++++++- .../Analyser/data/catch-without-variable.php | 19 +++++++++++ .../PHPStan/Analyser/data/mixed-typehint.php | 34 +++++++++++++++++++ .../Rules/Methods/ReturnTypeRuleTest.php | 16 --------- 7 files changed, 84 insertions(+), 27 deletions(-) create mode 100644 tests/PHPStan/Analyser/data/catch-without-variable.php create mode 100644 tests/PHPStan/Analyser/data/mixed-typehint.php diff --git a/composer.json b/composer.json index 98de55fb30..3356b02797 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "nette/neon": "^3.0", "nette/schema": "^1.0", "nette/utils": "^3.1.1", - "nikic/php-parser": "^4.4.0", + "nikic/php-parser": "^4.5.0", "ondram/ci-detector": "^3.1", "ondrejmirtes/better-reflection": "^4.3.1", "phpdocumentor/type-resolver": "1.0.1", diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index c63de7b0b8..4815120ab6 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -2701,11 +2701,15 @@ public function enterForeachKey(Expr $iteratee, string $keyName): self /** * @param \PhpParser\Node\Name[] $classes - * @param string $variableName + * @param string|null $variableName * @return self */ - public function enterCatch(array $classes, string $variableName): self + public function enterCatch(array $classes, ?string $variableName): self { + if ($variableName === null) { + return $this; + } + $type = TypeCombinator::union(...array_map(static function (string $class): ObjectType { return new ObjectType($class); }, $classes)); diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index a19adb13d8..fb2716cb58 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -978,10 +978,6 @@ private function processStmtNode( foreach ($stmt->catches as $catchNode) { $nodeCallback($catchNode, $scope); - if (!is_string($catchNode->var->name)) { - throw new \PHPStan\ShouldNotHappenException(); - } - if (!$this->polluteCatchScopeWithTryAssignments) { $catchScopeResult = $this->processCatchNode($catchNode, $scope->mergeWith($tryScope), $nodeCallback); $catchScopeForFinally = $catchScopeResult->getScope(); @@ -1106,11 +1102,16 @@ private function processCatchNode( \Closure $nodeCallback ): StatementResult { - if (!is_string($catchNode->var->name)) { - throw new \PHPStan\ShouldNotHappenException(); + $variableName = null; + if ($catchNode->var !== null) { + if (!is_string($catchNode->var->name)) { + throw new \PHPStan\ShouldNotHappenException(); + } + + $variableName = $catchNode->var->name; } - $catchScope = $catchScope->enterCatch($catchNode->types, $catchNode->var->name); + $catchScope = $catchScope->enterCatch($catchNode->types, $variableName); return $this->processStmtNodes($catchNode, $catchNode->stmts, $catchScope, $nodeCallback); } diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index e5e365029c..582eb6bbb7 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -9917,7 +9917,7 @@ public function dataAssignNestedArray(): array public function dataBug3276(): array { if (!self::$useStaticReflectionProvider && PHP_VERSION_ID < 70400) { - $this->markTestSkipped('Test requires PHP 7.4.'); + return []; } return $this->gatherAssertTypes(__DIR__ . '/data/bug-3276.php'); } @@ -9947,6 +9947,19 @@ public function dataBug3336(): array return $this->gatherAssertTypes(__DIR__ . '/data/bug-3336.php'); } + public function dataCatchWithoutVariable(): array + { + if (!self::$useStaticReflectionProvider && PHP_VERSION_ID < 80000) { + return []; + } + return $this->gatherAssertTypes(__DIR__ . '/data/catch-without-variable.php'); + } + + public function dataMixedTypehint(): array + { + return $this->gatherAssertTypes(__DIR__ . '/data/mixed-typehint.php'); + } + /** * @dataProvider dataBug2574 * @dataProvider dataBug2577 @@ -10000,6 +10013,8 @@ public function dataBug3336(): array * @dataProvider dataConstInFunctionsNamespaced * @dataProvider dataRootScopeMaybeDefined * @dataProvider dataBug3336 + * @dataProvider dataCatchWithoutVariable + * @dataProvider dataMixedTypehint * @param ConstantStringType $expectedType * @param Type $actualType */ diff --git a/tests/PHPStan/Analyser/data/catch-without-variable.php b/tests/PHPStan/Analyser/data/catch-without-variable.php new file mode 100644 index 0000000000..a6ca46db4a --- /dev/null +++ b/tests/PHPStan/Analyser/data/catch-without-variable.php @@ -0,0 +1,19 @@ +doBar()); + } + + public function doBar(): mixed + { + + } + +} + +function doFoo(mixed $foo) +{ + assertType('mixed', $foo); +} + +function (mixed $foo) { + assertType('mixed', $foo); + $f = function (): mixed { + + }; + assertType('mixed', $f()); +}; diff --git a/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php b/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php index 22df7a83e6..c38b671db4 100644 --- a/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php +++ b/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php @@ -159,14 +159,6 @@ public function testReturnTypeRule(): void 'Method ReturnTypes\Foo::misleadingIntReturnType() should return ReturnTypes\integer but returns true.', 371, ], - [ - 'Method ReturnTypes\Foo::misleadingMixedReturnType() should return ReturnTypes\mixed but returns int.', - 381, - ], - [ - 'Method ReturnTypes\Foo::misleadingMixedReturnType() should return ReturnTypes\mixed but returns true.', - 384, - ], [ 'Method ReturnTypes\Stock::getAnotherStock() should return ReturnTypes\Stock but returns ReturnTypes\Stock|null.', 429, @@ -305,14 +297,6 @@ public function testMisleadingTypehintsInClassWithoutNamespace(): void 'Method FooWithoutNamespace::misleadingIntReturnType() should return integer but returns true.', 28, ], - [ - 'Method FooWithoutNamespace::misleadingMixedReturnType() should return mixed but returns int.', - 39, - ], - [ - 'Method FooWithoutNamespace::misleadingMixedReturnType() should return mixed but returns true.', - 43, - ], ]); }