diff --git a/src/Resolution/VariableBindings.php b/src/Resolution/VariableBindings.php index 503ee412..5e81c991 100644 --- a/src/Resolution/VariableBindings.php +++ b/src/Resolution/VariableBindings.php @@ -94,24 +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); } if ($stmt instanceof Stmt\Foreach_) { - self::bind($stmt->keyVar, $scope, $bindings); - self::bind($stmt->valueVar, $scope, $bindings); + 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) { @@ -150,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. * 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/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 f7a3bcf9..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 @@ -160,6 +160,38 @@ 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 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'));