Skip to content

Commit 8de182d

Browse files
committed
Property can be written in get hook
1 parent 99a6a82 commit 8de182d

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/Rules/DeadCode/UnusedPrivatePropertyRule.php

+4
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ public function processNode(Node $node, Scope $scope): array
132132
$methodReflection instanceof PhpMethodFromParserNodeReflection
133133
&& $methodReflection->isPropertyHook()
134134
&& $methodReflection->getHookedPropertyName() === $propertyName
135+
&& (
136+
$methodReflection->getPropertyHookName() === 'set'
137+
|| $usage instanceof PropertyRead
138+
)
135139
) {
136140
continue;
137141
}

tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -387,4 +387,16 @@ public function testPropertyHooks(): void
387387
]);
388388
}
389389

390+
public function testBug12621(): void
391+
{
392+
if (PHP_VERSION_ID < 80400) {
393+
$this->markTestSkipped('Test requires PHP 8.4.');
394+
}
395+
396+
$this->alwaysWrittenTags = [];
397+
$this->alwaysReadTags = [];
398+
399+
$this->analyse([__DIR__ . '/data/bug-12621.php'], []);
400+
}
401+
390402
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php // lint >= 8.4
2+
3+
declare(strict_types=1);
4+
5+
namespace Bug12621;
6+
7+
final class Test
8+
{
9+
private string $a {
10+
get => $this->a ??= $this->b;
11+
}
12+
13+
public function __construct(
14+
private readonly string $b
15+
) {}
16+
17+
public function test(): string
18+
{
19+
return $this->a;
20+
}
21+
}

0 commit comments

Comments
 (0)