Skip to content

Commit 53439c2

Browse files
committed
AssignToByRefExprFromForeachRule - do not report foreach itself when inside another loop
1 parent 0624342 commit 53439c2

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ private function processStmtNode(
12781278
if (!(new ObjectType(Traversable::class))->isSuperTypeOf($scope->getType($stmt->expr))->no()) {
12791279
$throwPoints[] = ThrowPoint::createImplicit($scope, $stmt->expr);
12801280
}
1281-
if ($stmt->byRef) {
1281+
if ($context->isTopLevel() && $stmt->byRef) {
12821282
$finalScope = $finalScope->assignExpression(new ForeachValueByRefExpr($stmt->valueVar), new MixedType(), new MixedType());
12831283
}
12841284

tests/PHPStan/Rules/Variables/data/assign-to-by-ref-expr-from-foreach.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,14 @@ public function doFoo3(): void
6464
}
6565
}
6666

67+
public function doFoo4(): void
68+
{
69+
while (true) {
70+
$array = [0, 1, 2, 3];
71+
foreach ($array as &$item) {
72+
73+
}
74+
}
75+
}
76+
6777
}

0 commit comments

Comments
 (0)