Skip to content

Commit

Permalink
Property unset is an impure point
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 15, 2024
1 parent 67b140f commit 702ddcd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Analyser/ImpurePoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use PHPStan\Node\VirtualNode;

/**
* @phpstan-type ImpurePointIdentifier = 'echo'|'die'|'exit'|'propertyAssign'|'methodCall'|'new'|'functionCall'|'include'|'require'|'print'|'eval'|'superglobal'|'yield'|'yieldFrom'|'static'|'global'|'betweenPhpTags'
* @phpstan-type ImpurePointIdentifier = 'echo'|'die'|'exit'|'propertyAssign'|'propertyUnset'|'methodCall'|'new'|'functionCall'|'include'|'require'|'print'|'eval'|'superglobal'|'yield'|'yieldFrom'|'static'|'global'|'betweenPhpTags'
* @api
*/
class ImpurePoint
Expand Down
9 changes: 9 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,15 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
static fn (MutatingScope $scope): ExpressionResult => new ExpressionResult($scope, false, [], []),
false,
)->getScope();
} elseif ($var instanceof PropertyFetch) {
$scope = $scope->invalidateExpression($var);
$impurePoints[] = new ImpurePoint(
$scope,
$var,
'propertyUnset',
'property unset',
true,
);
} else {
$scope = $scope->invalidateExpression($var);
}
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 @@ -143,4 +143,9 @@ public function testRuleImpurePoints(): void
]);
}

public function testBug11001(): void
{
$this->analyse([__DIR__ . '/data/bug-11001.php'], []);
}

}
23 changes: 23 additions & 0 deletions tests/PHPStan/Rules/DeadCode/data/bug-11001.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Bug11001;

class Foo
{

/** @var int */
public $x;

/** @var int */
public $foo;

public function doFoo(): void
{
$x = new self();

(function () use ($x) {
unset($x->foo);
})();
}

}

0 comments on commit 702ddcd

Please sign in to comment.