diff --git a/src/Analyser/TypeSpecifier.php b/src/Analyser/TypeSpecifier.php index 5439383672e..09f1c184c5b 100644 --- a/src/Analyser/TypeSpecifier.php +++ b/src/Analyser/TypeSpecifier.php @@ -329,15 +329,7 @@ public function specifyTypesInCondition( && isset($exprNode->getArgs()[0]) && $constantType instanceof ConstantStringType ) { - return $this->specifyTypesInCondition( - $scope, - new Instanceof_( - $exprNode->getArgs()[0]->value, - new Name($constantType->getValue()), - ), - $context, - $rootExpr, - ); + return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\Identical($expr->left, $expr->right), $context, $rootExpr); } } @@ -1042,6 +1034,23 @@ private function specifyTypesForConstantStringBinaryExpression( } } + if ( + $exprNode instanceof FuncCall + && $exprNode->name instanceof Name + && strtolower($exprNode->name->toString()) === 'get_class' + && isset($exprNode->getArgs()[0]) + ) { + return $this->specifyTypesInCondition( + $scope, + new Instanceof_( + $exprNode->getArgs()[0]->value, + new Name($constantType->getValue()), + ), + $context, + $rootExpr, + ); + } + if ( $exprNode instanceof FuncCall && $exprNode->name instanceof Name diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 1c2b7a69c6f..1b776622e8b 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -1053,6 +1053,7 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/data/array-offset-unset.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8008.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5552.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-1239.php'); } /** diff --git a/tests/PHPStan/Analyser/data/bug-1239.php b/tests/PHPStan/Analyser/data/bug-1239.php new file mode 100644 index 00000000000..588f421123d --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-1239.php @@ -0,0 +1,23 @@ +bat(); + } + } +} + +class Bar extends Foo +{ + public function bat(): void + { + } +}