Skip to content

Commit c4b0b52

Browse files
phpstan-botclaude
authored andcommitted
Fix dynamic variable access producing inconsistent errors on PHP 8.1+
Use getScopeType() instead of getType() in filterBySpecifiedTypes() to bypass the fiber mechanism when reading the narrowed type after addTypeToExpression/assignExpression. On FiberScope, getType() suspends the fiber and returns the pre-modification type from the coordinator, causing conditional expression guard matching to fail. This made $$name dynamic access report false "might not be defined" errors for variables that are conditionally defined based on the name value. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d6d4f4b commit c4b0b52

File tree

2 files changed

+1
-9
lines changed

2 files changed

+1
-9
lines changed

src/Analyser/MutatingScope.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3230,7 +3230,7 @@ public function filterBySpecifiedTypes(SpecifiedTypes $specifiedTypes): self
32303230
} else {
32313231
$scope = $scope->removeTypeFromExpression($expr, $type);
32323232
}
3233-
$specifiedExpressions[$typeSpecification['exprString']] = ExpressionTypeHolder::createYes($expr, $scope->getType($expr));
3233+
$specifiedExpressions[$typeSpecification['exprString']] = ExpressionTypeHolder::createYes($expr, $scope->getScopeType($expr));
32343234
}
32353235

32363236
$conditions = [];

tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,14 +1137,6 @@ public function testDynamicAccess(): void
11371137
'Undefined variable: $buz',
11381138
49,
11391139
],
1140-
[
1141-
'Variable $bar might not be defined.',
1142-
49,
1143-
],
1144-
[
1145-
'Variable $foo might not be defined.',
1146-
49,
1147-
],
11481140
[
11491141
'Variable $foo might not be defined.',
11501142
50,

0 commit comments

Comments
 (0)