Skip to content

Commit 74787be

Browse files
committed
Properties are read when using AssignRef too
1 parent 9384783 commit 74787be

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/Node/ClassStatementsGatherer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ private function gatherNodes(\PhpParser\Node $node, Scope $scope): void
155155
$this->gatherNodes($node->var, $scope);
156156
return;
157157
}
158+
if ($node instanceof Expr\AssignRef) {
159+
$this->gatherNodes($node->expr, $scope);
160+
return;
161+
}
158162
if ($node instanceof \PhpParser\Node\Scalar\EncapsedStringPart) {
159163
return;
160164
}

tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,11 @@ public function testNullsafe(): void
190190
$this->analyse([__DIR__ . '/data/nullsafe-unused-private-property.php'], []);
191191
}
192192

193+
public function testBug5935(): void
194+
{
195+
$this->alwaysWrittenTags = [];
196+
$this->alwaysReadTags = [];
197+
$this->analyse([__DIR__ . '/data/bug-5935.php'], []);
198+
}
199+
193200
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug5935;
4+
5+
class Test
6+
{
7+
/**
8+
* @var int[]
9+
*/
10+
private $arr = [];
11+
12+
public function test(int $id): int
13+
{
14+
$arr = &$this->arr;
15+
if(isset($arr[$id]))
16+
{
17+
return $arr[$id];
18+
}
19+
else
20+
{
21+
return $arr[$id] = time();
22+
}
23+
}
24+
}
25+
26+
class Test2
27+
{
28+
/**
29+
* @var int[]
30+
*/
31+
private $arr;
32+
33+
public function test(int $id): int
34+
{
35+
$arr = &$this->arr;
36+
if(isset($arr[$id]))
37+
{
38+
return $arr[$id];
39+
}
40+
else
41+
{
42+
return $arr[$id] = time();
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)