Skip to content

Commit

Permalink
Fix property assign not being an impure point in arrow function
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 15, 2024
1 parent 702ddcd commit 35ff689
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -1272,17 +1272,38 @@ private function resolveType(string $exprString, Expr $node): Type
}
}

$arrowFunctionImpurePoints = [];
$invalidateExpressions = [];
$arrowFunctionExprResult = $this->nodeScopeResolver->processExprNode(
new Node\Stmt\Expression($node->expr),
$node->expr,
$arrowScope,
static function (): void {
static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpurePoints, &$invalidateExpressions): void {
if ($scope->getAnonymousFunctionReflection() !== $arrowScope->getAnonymousFunctionReflection()) {
return;
}

if ($node instanceof InvalidateExprNode) {
$invalidateExpressions[] = $node;
return;
}

if (!$node instanceof PropertyAssignNode) {
return;
}

$arrowFunctionImpurePoints[] = new ImpurePoint(
$scope,
$node,
'propertyAssign',
'property assignment',
true,
);
},
ExpressionContext::createDeep(),
);
$throwPoints = $arrowFunctionExprResult->getThrowPoints();
$impurePoints = $arrowFunctionExprResult->getImpurePoints();
$invalidateExpressions = [];
$impurePoints = array_merge($arrowFunctionImpurePoints, $arrowFunctionExprResult->getImpurePoints());
$usedVariables = [];
} else {
$closureScope = $this->enterAnonymousFunctionWithoutReflection($node, $callableParameters);
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/DeadCode/BetterNoopRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPStan\Node\Printer\Printer;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use const PHP_VERSION_ID;

/**
* @extends RuleTestCase<BetterNoopRule>
Expand Down Expand Up @@ -145,6 +146,10 @@ public function testRuleImpurePoints(): void

public function testBug11001(): void
{
if (PHP_VERSION_ID < 70400) {
self::markTestSkipped('Test requires PHP 7.4.');
}

$this->analyse([__DIR__ . '/data/bug-11001.php'], []);
}

Expand Down
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/DeadCode/data/bug-11001.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,18 @@ public function doFoo(): void
}

}

class Foo2
{
public function test(): void
{
\Closure::bind(fn () => $this->status = 5, $this)();
}

public function test2(): void
{
\Closure::bind(function () {
$this->status = 5;
}, $this)();
}
}

0 comments on commit 35ff689

Please sign in to comment.