Skip to content

Commit 4a2f65d

Browse files
committed
Fix unused private property is not sometimes detected
1 parent 0519ced commit 4a2f65d

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/Rules/DeadCode/UnusedPrivatePropertyRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function processNode(Node $node, Scope $scope): array
120120
$propertyNameType = $usage->getScope()->getType($fetch->name);
121121
$strings = $propertyNameType->getConstantStrings();
122122
if (count($strings) === 0) {
123-
return [];
123+
continue;
124124
}
125125

126126
$propertyNames = array_map(static fn (ConstantStringType $type): string => $type->getValue(), $strings);

tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ public function testRule(): void
107107
117,
108108
$tip,
109109
],
110+
[
111+
'Property UnusedPrivateProperty\Ipsum::$foo is never read, only written.',
112+
136,
113+
$tip,
114+
],
110115
[
111116
'Property class@anonymous/tests/PHPStan/Rules/DeadCode/data/unused-private-property.php:152::$bar is unused.',
112117
153,
@@ -336,4 +341,19 @@ public function testBug7251(): void
336341
$this->analyse([__DIR__ . '/data/bug-7251.php'], []);
337342
}
338343

344+
public function testBug11802(): void
345+
{
346+
$tip = 'See: https://phpstan.org/developing-extensions/always-read-written-properties';
347+
348+
$this->alwaysWrittenTags = [];
349+
$this->alwaysReadTags = [];
350+
$this->analyse([__DIR__ . '/data/bug-11802.php'], [
351+
[
352+
'Property Bug11802\HelloWorld::$isFinal is never read, only written.',
353+
8,
354+
$tip,
355+
],
356+
]);
357+
}
358+
339359
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug11802;
4+
5+
class HelloWorld
6+
{
7+
public function __construct(
8+
private bool $isFinal
9+
)
10+
{
11+
}
12+
13+
public function doFoo(HelloWorld $x, string $y): void
14+
{
15+
$s = $x->{$y()};
16+
}
17+
}

0 commit comments

Comments
 (0)