Skip to content

Commit

Permalink
Properties are read when using AssignRef too
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 9, 2021
1 parent 9384783 commit 74787be
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Node/ClassStatementsGatherer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'], []);
}

}
45 changes: 45 additions & 0 deletions tests/PHPStan/Rules/DeadCode/data/bug-5935.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types=1);

namespace Bug5935;

class Test
{
/**
* @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();
}
}
}

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();
}
}
}

0 comments on commit 74787be

Please sign in to comment.