Skip to content

Commit 3c97119

Browse files
phpstan-botclaude
authored andcommitted
Fix undefined variable false positive for by-reference array items
Restore bug-6799c.php test to use undefined variable in [&$x] (the previous $x = null assignment defeated the test purpose). Allow undefined expressions in by-reference array items using the same set/unset pattern used for function call by-reference arguments. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 1a312aa commit 3c97119

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/Analyser/ExprHandler/ArrayHandler.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
6161
$scope = $keyResult->getScope();
6262
}
6363

64+
if ($arrayItem->byRef) {
65+
$scope = $nodeScopeResolver->lookForSetAllowedUndefinedExpressions($scope, $arrayItem->value);
66+
}
67+
6468
$valueResult = $nodeScopeResolver->processExprNode($stmt, $arrayItem->value, $scope, $storage, $nodeCallback, $context->enterDeep());
6569
$hasYield = $hasYield || $valueResult->hasYield();
6670
$throwPoints = array_merge($throwPoints, $valueResult->getThrowPoints());
@@ -71,6 +75,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
7175
continue;
7276
}
7377

78+
$scope = $nodeScopeResolver->lookForUnsetAllowedUndefinedExpressions($scope, $arrayItem->value);
7479
$scope = $scope->assignExpression($arrayItem->value, new MixedType(), new MixedType());
7580
}
7681
$nodeScopeResolver->callNodeCallback($nodeCallback, new LiteralArrayNode($expr, $itemNodes), $scope, $storage);

tests/PHPStan/Analyser/nsrt/bug-6799c.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66

77
// https://3v4l.org/g5UjS
88

9-
$x = null;
10-
assertType('null', $x);
119
$a = [&$x];
12-
assertType('mixed', $x); // Could stay null
10+
assertType('mixed', $x);
1311

1412
function doFoo(array &$arr) {
1513
$arr[0] = 'string';

0 commit comments

Comments
 (0)