diff --git a/src/Node/ClassStatementsGatherer.php b/src/Node/ClassStatementsGatherer.php index 14baad972b..58b9763140 100644 --- a/src/Node/ClassStatementsGatherer.php +++ b/src/Node/ClassStatementsGatherer.php @@ -155,6 +155,10 @@ private function gatherNodes(\PhpParser\Node $node, Scope $scope): void $this->gatherNodes($node->var, $scope); return; } + if ($node instanceof Expr\AssignRef) { + $this->gatherNodes($node->expr, $scope); + return; + } if ($node instanceof \PhpParser\Node\Scalar\EncapsedStringPart) { return; } diff --git a/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php b/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php index 0df99db626..5b8b717227 100644 --- a/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php +++ b/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php @@ -190,4 +190,11 @@ public function testNullsafe(): void $this->analyse([__DIR__ . '/data/nullsafe-unused-private-property.php'], []); } + public function testBug5935(): void + { + $this->alwaysWrittenTags = []; + $this->alwaysReadTags = []; + $this->analyse([__DIR__ . '/data/bug-5935.php'], []); + } + } diff --git a/tests/PHPStan/Rules/DeadCode/data/bug-5935.php b/tests/PHPStan/Rules/DeadCode/data/bug-5935.php new file mode 100644 index 0000000000..7a842a8fb7 --- /dev/null +++ b/tests/PHPStan/Rules/DeadCode/data/bug-5935.php @@ -0,0 +1,45 @@ +arr; + if(isset($arr[$id])) + { + return $arr[$id]; + } + else + { + return $arr[$id] = time(); + } + } +} + +class Test2 +{ + /** + * @var int[] + */ + private $arr; + + public function test(int $id): int + { + $arr = &$this->arr; + if(isset($arr[$id])) + { + return $arr[$id]; + } + else + { + return $arr[$id] = time(); + } + } +}