From 56b1b7e978da5fb59ed1c247881b438b837cda2f Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Wed, 28 Oct 2026 10:09:27 -0700 Subject: [PATCH 1/7] Reproduce foreach rebinding its subject Co-Authored-By: Claude Opus 5.5 --- .../870-foreach-rebinds-its-subject.php | 19 +++++++++++++++++++ tests/Fixtures/src/Hover/ForeachElement.php | 8 ++++++++ 2 files changed, 27 insertions(+) create mode 100644 tests/EndToEnd/defects/870-foreach-rebinds-its-subject.php diff --git a/tests/EndToEnd/defects/870-foreach-rebinds-its-subject.php b/tests/EndToEnd/defects/870-foreach-rebinds-its-subject.php new file mode 100644 index 00000000..ed916f40 --- /dev/null +++ b/tests/EndToEnd/defects/870-foreach-rebinds-its-subject.php @@ -0,0 +1,19 @@ +getName(); //hover:foreach_const_fetch } } + + public function iterateIntoItsOwnSubject(UserCollection $collection): void + { + $users = $collection->all(); + foreach ($users as $users) { + $users->getName(); //hover:foreach_rebinds_subject + } + } } From 2773c3240ff5a99d580a2bc6ee7bd321c20357d7 Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Wed, 28 Oct 2026 10:09:46 -0700 Subject: [PATCH 2/7] Test foreach binds after its subject Co-Authored-By: Claude Opus 5.5 --- tests/Fixtures/src/Utility/BindingSites.php | 9 +++++++++ tests/Resolution/VariableBindingsTest.php | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/tests/Fixtures/src/Utility/BindingSites.php b/tests/Fixtures/src/Utility/BindingSites.php index 44b53aaa..2d7b6253 100644 --- a/tests/Fixtures/src/Utility/BindingSites.php +++ b/tests/Fixtures/src/Utility/BindingSites.php @@ -43,4 +43,13 @@ public function reassignFromItself(int $reassigned): void { $reassigned = $reassigned + 1; } + + /** + * @param list $rebound + */ + public function iterateIntoItself(array $rebound): void + { + foreach ($rebound as $rebound) { + } + } } diff --git a/tests/Resolution/VariableBindingsTest.php b/tests/Resolution/VariableBindingsTest.php index f7a3bcf9..bacd43dd 100644 --- a/tests/Resolution/VariableBindingsTest.php +++ b/tests/Resolution/VariableBindingsTest.php @@ -160,6 +160,25 @@ public function testAnAssignmentBindsOnlyOnceItIsEvaluated(): void self::assertSame($target, $afterward['reassigned']->node ?? null, 'later code reads the assignment'); } + public function testAForeachBindsOnlyOnceItsSubjectIsEvaluated(): void + { + $ast = self::parseWithParents($this->loadFixture('src/Utility/BindingSites.php')); + $method = self::findMethodByName('iterateIntoItself', $ast); + $scope = Scope::at($method, $ast); + $foreach = self::findFirst([$method], fn (Node $n) => $n instanceof Stmt\Foreach_); + self::assertInstanceOf(Stmt\Foreach_::class, $foreach); + + $inSubject = VariableBindings::visible($scope, $foreach->expr->getStartFilePos()); + $afterward = VariableBindings::visible($scope, $method->getEndFilePos()); + + self::assertSame( + $method->params[0]->var, + $inSubject['rebound']->node ?? null, + 'the subject reads the parameter', + ); + self::assertSame($foreach->valueVar, $afterward['rebound']->node ?? null, 'later code reads the loop value'); + } + public function testThisIsBoundWherePhpBindsIt(): void { $ast = self::parseWithParents($this->loadFixture('TopLevel/this_binding.php')); From ea32c499569ee4b867e39cfc94dbb7fb5824a214 Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Wed, 28 Oct 2026 10:32:40 -0700 Subject: [PATCH 3/7] Bind foreach variables after the subject Co-Authored-By: Claude Opus 5.5 --- src/Resolution/VariableBindings.php | 3 ++- .../870-foreach-rebinds-its-subject.php | 19 ------------------- 2 files changed, 2 insertions(+), 20 deletions(-) delete mode 100644 tests/EndToEnd/defects/870-foreach-rebinds-its-subject.php diff --git a/src/Resolution/VariableBindings.php b/src/Resolution/VariableBindings.php index 503ee412..87a4cede 100644 --- a/src/Resolution/VariableBindings.php +++ b/src/Resolution/VariableBindings.php @@ -103,7 +103,8 @@ private static function walk(array $stmts, int $offset, Scope $scope, array &$bi self::bind($stmt->expr->var, $scope, $bindings); } - if ($stmt instanceof Stmt\Foreach_) { + // PHP evaluates the subject once, before assigning the key and value. + if ($stmt instanceof Stmt\Foreach_ && $stmt->expr->getEndFilePos() < $offset) { self::bind($stmt->keyVar, $scope, $bindings); self::bind($stmt->valueVar, $scope, $bindings); } diff --git a/tests/EndToEnd/defects/870-foreach-rebinds-its-subject.php b/tests/EndToEnd/defects/870-foreach-rebinds-its-subject.php deleted file mode 100644 index ed916f40..00000000 --- a/tests/EndToEnd/defects/870-foreach-rebinds-its-subject.php +++ /dev/null @@ -1,19 +0,0 @@ - Date: Wed, 28 Oct 2026 10:32:52 -0700 Subject: [PATCH 4/7] Drop the foreach rebinding hover fixture Co-Authored-By: Claude Opus 5.5 --- tests/Fixtures/src/Hover/ForeachElement.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tests/Fixtures/src/Hover/ForeachElement.php b/tests/Fixtures/src/Hover/ForeachElement.php index 86c24be8..30d8f985 100644 --- a/tests/Fixtures/src/Hover/ForeachElement.php +++ b/tests/Fixtures/src/Hover/ForeachElement.php @@ -171,12 +171,4 @@ public function iterateConstFetch(): void $user->getName(); //hover:foreach_const_fetch } } - - public function iterateIntoItsOwnSubject(UserCollection $collection): void - { - $users = $collection->all(); - foreach ($users as $users) { - $users->getName(); //hover:foreach_rebinds_subject - } - } } From eeeddf493421b1d49e36c1b772ce78238c00c5d4 Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Wed, 28 Oct 2026 10:56:28 -0700 Subject: [PATCH 5/7] Test catch binds only inside its block Co-Authored-By: Claude Opus 5.5 --- tests/Resolution/VariableBindingsTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/Resolution/VariableBindingsTest.php b/tests/Resolution/VariableBindingsTest.php index bacd43dd..312cf157 100644 --- a/tests/Resolution/VariableBindingsTest.php +++ b/tests/Resolution/VariableBindingsTest.php @@ -179,6 +179,19 @@ public function testAForeachBindsOnlyOnceItsSubjectIsEvaluated(): void self::assertSame($foreach->valueVar, $afterward['rebound']->node ?? null, 'later code reads the loop value'); } + public function testACatchBindsOnlyInsideItsBlock(): void + { + $ast = self::parseWithParents($this->loadFixture('src/Utility/BindingSites.php')); + $method = self::findMethodByName('method', $ast); + $scope = Scope::at($method, $ast); + + $inTry = VariableBindings::visible($scope, self::findAssignmentTarget('inTry', $ast)->getStartFilePos()); + $inCatch = VariableBindings::visible($scope, self::findAssignmentTarget('inCatch', $ast)->getStartFilePos()); + + self::assertArrayNotHasKey('caught', $inTry, 'the try body runs before anything is caught'); + self::assertArrayHasKey('caught', $inCatch, 'the catch block reads what it caught'); + } + public function testThisIsBoundWherePhpBindsIt(): void { $ast = self::parseWithParents($this->loadFixture('TopLevel/this_binding.php')); From a115499568bb27268bbb7edd543b7a287ec96f75 Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Wed, 28 Oct 2026 10:59:10 -0700 Subject: [PATCH 6/7] Test a catch without a variable Co-Authored-By: Claude Opus 5.5 --- tests/Fixtures/src/Utility/BranchingBindingSites.php | 2 ++ tests/Resolution/VariableBindingsTest.php | 10 +++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/Fixtures/src/Utility/BranchingBindingSites.php b/tests/Fixtures/src/Utility/BranchingBindingSites.php index bd8456da..bc7ccbec 100644 --- a/tests/Fixtures/src/Utility/BranchingBindingSites.php +++ b/tests/Fixtures/src/Utility/BranchingBindingSites.php @@ -10,6 +10,8 @@ public function method(): void { try { $inTry = 1; + } catch (\RuntimeException) { + $inUnnamedCatch = 7; } finally { $inFinally = 2; } diff --git a/tests/Resolution/VariableBindingsTest.php b/tests/Resolution/VariableBindingsTest.php index 312cf157..b7393718 100644 --- a/tests/Resolution/VariableBindingsTest.php +++ b/tests/Resolution/VariableBindingsTest.php @@ -78,11 +78,11 @@ public function testEnumeratesBindingsInFinallyElseIfAndSwitch(): void $names = self::namesOf(VariableBindings::before($scope, $method->getEndFilePos())); - self::assertContains('inTry', $names, 'try body binding is enumerated'); - self::assertContains('inFinally', $names, 'finally body binding is enumerated'); - self::assertContains('inIf', $names, 'if body binding is enumerated'); - self::assertContains('inElseIf', $names, 'elseif body binding is enumerated'); - self::assertContains('inCase', $names, 'switch case body binding is enumerated'); + self::assertSame( + ['inTry', 'inUnnamedCatch', 'inFinally', 'inIf', 'inElseIf', 'inCase'], + $names, + 'every branch body is enumerated; a catch without a variable binds nothing itself', + ); } public function testArrowFunctionScopeExposesOnlyItsParams(): void From ed69b57ec8ea9fc6857d6c6696b50a5adaf9b282 Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Wed, 28 Oct 2026 10:59:10 -0700 Subject: [PATCH 7/7] Bind every variable once its code has run Co-Authored-By: Claude Opus 5.5 --- src/Resolution/VariableBindings.php | 39 ++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/Resolution/VariableBindings.php b/src/Resolution/VariableBindings.php index 87a4cede..5e81c991 100644 --- a/src/Resolution/VariableBindings.php +++ b/src/Resolution/VariableBindings.php @@ -94,25 +94,21 @@ private static function walk(array $stmts, int $offset, Scope $scope, array &$bi continue; } - // PHP assigns after evaluating the right side, which may read the earlier binding. - if ( - $stmt instanceof Stmt\Expression - && $stmt->expr instanceof Assign - && $stmt->expr->getEndFilePos() < $offset - ) { - self::bind($stmt->expr->var, $scope, $bindings); + if ($stmt instanceof Stmt\Expression && $stmt->expr instanceof Assign) { + self::bindOnceRun($stmt->expr->var, $stmt->expr, $offset, $scope, $bindings); } - // PHP evaluates the subject once, before assigning the key and value. - if ($stmt instanceof Stmt\Foreach_ && $stmt->expr->getEndFilePos() < $offset) { - self::bind($stmt->keyVar, $scope, $bindings); - self::bind($stmt->valueVar, $scope, $bindings); + if ($stmt instanceof Stmt\Foreach_) { + self::bindOnceRun($stmt->keyVar, $stmt->expr, $offset, $scope, $bindings); + self::bindOnceRun($stmt->valueVar, $stmt->expr, $offset, $scope, $bindings); } if ($stmt instanceof Stmt\TryCatch) { self::walk($stmt->stmts, $offset, $scope, $bindings); foreach ($stmt->catches as $catch) { - self::bind($catch->var, $scope, $bindings); + if ($catch->var !== null) { + self::bindOnceRun($catch->var, $catch->var, $offset, $scope, $bindings); + } self::walk($catch->stmts, $offset, $scope, $bindings); } if ($stmt->finally !== null) { @@ -151,6 +147,25 @@ private static function walk(array $stmts, int $offset, Scope $scope, array &$bi } } + /** + * Records $target once the code PHP runs before assigning it, which ends + * with $runFirst, is complete before $offset. That code may read an + * earlier binding of the same name. + * + * @param list $bindings + */ + private static function bindOnceRun( + ?Node $target, + Node $runFirst, + int $offset, + Scope $scope, + array &$bindings, + ): void { + if ($runFirst->getEndFilePos() < $offset) { + self::bind($target, $scope, $bindings); + } + } + /** * Records $target when it names a variable. *