Skip to content

Commit 3fe47b4

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 <ondrejmirtes@users.noreply.github.com>
1 parent 716e6eb commit 3fe47b4

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

src/Analyser/MutatingScope.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@
145145
use Throwable;
146146
use ValueError;
147147
use function abs;
148-
use function assert;
149148
use function array_filter;
150149
use function array_key_exists;
151150
use function array_key_first;
@@ -157,6 +156,7 @@
157156
use function array_slice;
158157
use function array_unique;
159158
use function array_values;
159+
use function assert;
160160
use function count;
161161
use function explode;
162162
use function get_class;
@@ -1040,6 +1040,9 @@ private function getClosureScopeCacheKey(): string
10401040
{
10411041
$parts = [];
10421042
foreach ($this->expressionTypes as $exprString => $expressionTypeHolder) {
1043+
if ($expressionTypeHolder->getExpr() instanceof VirtualNode) {
1044+
continue;
1045+
}
10431046
$parts[] = sprintf('%s::%s', $exprString, $expressionTypeHolder->getType()->describe(VerbosityLevel::cache()));
10441047
}
10451048
$parts[] = '---';

src/Analyser/NodeScopeResolver.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2941,9 +2941,9 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
29412941
if (
29422942
$functionReflection !== null
29432943
&& $this->rememberPossiblyImpureFunctionValues
2944+
&& $parametersAcceptor !== null
29442945
&& $functionReflection->hasSideEffects()->maybe()
29452946
&& !$functionReflection->isBuiltin()
2946-
&& $parametersAcceptor !== null
29472947
) {
29482948
$scope = $scope->assignExpression(
29492949
new PossiblyImpureCallExpr($normalizedExpr, $normalizedExpr, sprintf('%s()', $functionReflection->getName())),
@@ -3469,11 +3469,11 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
34693469
} elseif (
34703470
$methodReflection !== null
34713471
&& $this->rememberPossiblyImpureFunctionValues
3472-
&& $methodReflection->hasSideEffects()->maybe()
3473-
&& !$methodReflection->getDeclaringClass()->isBuiltin()
34743472
&& $parametersAcceptor !== null
34753473
&& $scope->isInClass()
34763474
&& $scope->getClassReflection()->is($methodReflection->getDeclaringClass()->getName())
3475+
&& $methodReflection->hasSideEffects()->maybe()
3476+
&& !$methodReflection->getDeclaringClass()->isBuiltin()
34773477
) {
34783478
$scope = $scope->assignExpression(
34793479
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)