Skip to content

Commit 4f0994e

Browse files
Address review: reorder conditions and fix closure cache key collision
- Move $parametersAcceptor !== null check before hasSideEffects()->maybe() for function calls - Move $scope->isInClass() and getClassReflection()->is() checks before hasSideEffects()->maybe() for static method calls - Fix closure type cache key collision between PHPDoc-aware and native scopes by including nativeTypesPromoted flag in getClosureScopeCacheKey() - Revert native-types-81.php test expectations back to original values Co-authored-by: Ondřej Mirtes <[email protected]>
1 parent 9ad6900 commit 4f0994e

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

src/Analyser/MutatingScope.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,10 @@ private function getClosureScopeCacheKey(): string
10501050
$parts[] = sprintf(',%s', $parameter->getType()->describe(VerbosityLevel::cache()));
10511051
}
10521052

1053+
if ($this->nativeTypesPromoted) {
1054+
$parts[] = '::native';
1055+
}
1056+
10531057
return md5(implode("\n", $parts));
10541058
}
10551059

src/Analyser/NodeScopeResolver.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2924,9 +2924,9 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
29242924
if (
29252925
$functionReflection !== null
29262926
&& $this->rememberPossiblyImpureFunctionValues
2927+
&& $parametersAcceptor !== null
29272928
&& $functionReflection->hasSideEffects()->maybe()
29282929
&& !$functionReflection->isBuiltin()
2929-
&& $parametersAcceptor !== null
29302930
) {
29312931
$scope = $scope->assignExpression(
29322932
new PossiblyImpureCallExpr($normalizedExpr, $normalizedExpr, sprintf('%s()', $functionReflection->getName())),
@@ -3452,11 +3452,11 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
34523452
} elseif (
34533453
$methodReflection !== null
34543454
&& $this->rememberPossiblyImpureFunctionValues
3455-
&& $methodReflection->hasSideEffects()->maybe()
3456-
&& !$methodReflection->getDeclaringClass()->isBuiltin()
34573455
&& $parametersAcceptor !== null
34583456
&& $scope->isInClass()
34593457
&& $scope->getClassReflection()->is($methodReflection->getDeclaringClass()->getName())
3458+
&& $methodReflection->hasSideEffects()->maybe()
3459+
&& !$methodReflection->getDeclaringClass()->isBuiltin()
34603460
) {
34613461
$scope = $scope->assignExpression(
34623462
new PossiblyImpureCallExpr($normalizedExpr, new Variable('this'), sprintf('%s::%s()', $methodReflection->getDeclaringClass()->getDisplayName(), $methodReflection->getName())),

tests/PHPStan/Analyser/nsrt/native-types-81.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ public function doFoo(): void
2525
};
2626

2727
assertType('non-empty-string', $f());
28-
assertNativeType('string', $f());
28+
assertNativeType('non-empty-string', $f());
2929

3030
assertType('non-empty-string', (function (): string {
3131
return funcWithANativeReturnType();
3232
})());
33-
assertNativeType('string', (function (): string {
33+
assertNativeType('non-empty-string', (function (): string {
3434
return funcWithANativeReturnType();
3535
})());
3636

0 commit comments

Comments
 (0)