Skip to content

Commit

Permalink
Fix unused private property is not sometimes detected
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Oct 4, 2024
1 parent 0519ced commit 4a2f65d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Rules/DeadCode/UnusedPrivatePropertyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function processNode(Node $node, Scope $scope): array
$propertyNameType = $usage->getScope()->getType($fetch->name);
$strings = $propertyNameType->getConstantStrings();
if (count($strings) === 0) {
return [];
continue;
}

$propertyNames = array_map(static fn (ConstantStringType $type): string => $type->getValue(), $strings);
Expand Down
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public function testRule(): void
117,
$tip,
],
[
'Property UnusedPrivateProperty\Ipsum::$foo is never read, only written.',
136,
$tip,
],
[
'Property class@anonymous/tests/PHPStan/Rules/DeadCode/data/unused-private-property.php:152::$bar is unused.',
153,
Expand Down Expand Up @@ -336,4 +341,19 @@ public function testBug7251(): void
$this->analyse([__DIR__ . '/data/bug-7251.php'], []);
}

public function testBug11802(): void
{
$tip = 'See: https://phpstan.org/developing-extensions/always-read-written-properties';

$this->alwaysWrittenTags = [];
$this->alwaysReadTags = [];
$this->analyse([__DIR__ . '/data/bug-11802.php'], [
[
'Property Bug11802\HelloWorld::$isFinal is never read, only written.',
8,
$tip,
],
]);
}

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

namespace Bug11802;

class HelloWorld
{
public function __construct(
private bool $isFinal
)
{
}

public function doFoo(HelloWorld $x, string $y): void
{
$s = $x->{$y()};
}
}

0 comments on commit 4a2f65d

Please sign in to comment.