Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions src/Resolution/VariableBindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<VariableBinding> $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.
*
Expand Down
9 changes: 9 additions & 0 deletions tests/Fixtures/src/Utility/BindingSites.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,13 @@ public function reassignFromItself(int $reassigned): void
{
$reassigned = $reassigned + 1;
}

/**
* @param list<int> $rebound
*/
public function iterateIntoItself(array $rebound): void
{
foreach ($rebound as $rebound) {
}
}
}
2 changes: 2 additions & 0 deletions tests/Fixtures/src/Utility/BranchingBindingSites.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public function method(): void
{
try {
$inTry = 1;
} catch (\RuntimeException) {
$inUnnamedCatch = 7;
} finally {
$inFinally = 2;
}
Expand Down
42 changes: 37 additions & 5 deletions tests/Resolution/VariableBindingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'));
Expand Down
Loading