From 1182199bcffbb3f6cd73a1e5058020ecbbcfa02a Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Fri, 27 May 2022 16:25:04 +0200 Subject: [PATCH] TypeSpecifier - observe hasSideEffects() for static methods --- src/Analyser/TypeSpecifier.php | 22 ++++++++++++ .../Analyser/NodeScopeResolverTest.php | 1 + tests/PHPStan/Analyser/data/bug-7210.php | 34 +++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 tests/PHPStan/Analyser/data/bug-7210.php diff --git a/src/Analyser/TypeSpecifier.php b/src/Analyser/TypeSpecifier.php index 437957f263..4b4b3e9b1c 100644 --- a/src/Analyser/TypeSpecifier.php +++ b/src/Analyser/TypeSpecifier.php @@ -1204,6 +1204,28 @@ public function create( } } + if ( + $expr instanceof StaticCall + && $expr->name instanceof Node\Identifier + && $scope !== null + ) { + $methodName = $expr->name->toString(); + if ($expr->class instanceof Name) { + $calledOnType = $scope->resolveTypeByName($expr->class); + } else { + $calledOnType = $scope->getType($expr->class); + } + + $methodReflection = $scope->getMethodReflection($calledOnType, $methodName); + if ($methodReflection === null || $methodReflection->hasSideEffects()->yes()) { + if (isset($resultType) && !TypeCombinator::containsNull($resultType)) { + return $this->createNullsafeTypes($rootExpr, $originalExpr, $scope, $context, $overwrite, $type); + } + + return new SpecifiedTypes([], [], false, [], $rootExpr); + } + } + $sureTypes = []; $sureNotTypes = []; $exprString = $this->printer->prettyPrintExpr($expr); diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 77378f5822..a0920909c5 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -894,6 +894,7 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/data/non-empty-string-substr-specifying.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/unset-conditional-expressions.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/conditional-types-inference.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7210.php'); } /** diff --git a/tests/PHPStan/Analyser/data/bug-7210.php b/tests/PHPStan/Analyser/data/bug-7210.php new file mode 100644 index 0000000000..9509b3e688 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-7210.php @@ -0,0 +1,34 @@ +