Skip to content

Commit 702ddcd

Browse files
committed
Property unset is an impure point
1 parent 67b140f commit 702ddcd

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

src/Analyser/ImpurePoint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use PHPStan\Node\VirtualNode;
77

88
/**
9-
* @phpstan-type ImpurePointIdentifier = 'echo'|'die'|'exit'|'propertyAssign'|'methodCall'|'new'|'functionCall'|'include'|'require'|'print'|'eval'|'superglobal'|'yield'|'yieldFrom'|'static'|'global'|'betweenPhpTags'
9+
* @phpstan-type ImpurePointIdentifier = 'echo'|'die'|'exit'|'propertyAssign'|'propertyUnset'|'methodCall'|'new'|'functionCall'|'include'|'require'|'print'|'eval'|'superglobal'|'yield'|'yieldFrom'|'static'|'global'|'betweenPhpTags'
1010
* @api
1111
*/
1212
class ImpurePoint

src/Analyser/NodeScopeResolver.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,15 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
16871687
static fn (MutatingScope $scope): ExpressionResult => new ExpressionResult($scope, false, [], []),
16881688
false,
16891689
)->getScope();
1690+
} elseif ($var instanceof PropertyFetch) {
1691+
$scope = $scope->invalidateExpression($var);
1692+
$impurePoints[] = new ImpurePoint(
1693+
$scope,
1694+
$var,
1695+
'propertyUnset',
1696+
'property unset',
1697+
true,
1698+
);
16901699
} else {
16911700
$scope = $scope->invalidateExpression($var);
16921701
}

tests/PHPStan/Rules/DeadCode/BetterNoopRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,9 @@ public function testRuleImpurePoints(): void
143143
]);
144144
}
145145

146+
public function testBug11001(): void
147+
{
148+
$this->analyse([__DIR__ . '/data/bug-11001.php'], []);
149+
}
150+
146151
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Bug11001;
4+
5+
class Foo
6+
{
7+
8+
/** @var int */
9+
public $x;
10+
11+
/** @var int */
12+
public $foo;
13+
14+
public function doFoo(): void
15+
{
16+
$x = new self();
17+
18+
(function () use ($x) {
19+
unset($x->foo);
20+
})();
21+
}
22+
23+
}

0 commit comments

Comments
 (0)