Skip to content

Commit 8428d06

Browse files
phpstan-botclaude
andcommitted
Do not treat private promoted properties as initializing redeclared child property
Private promoted properties are not inherited by child classes, so a child class redeclaring the property should still report it as uninitialized. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 1bffa1d commit 8428d06

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/Node/ClassPropertiesNode.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,10 @@ private function isPropertyInitializedByConstructorChain(ClassReflection $constr
433433
if ($ancestorConstructor !== null) {
434434
foreach ($ancestorConstructor->getParameters() as $param) {
435435
if ($param->getName() === $propertyName && $param->isPromoted()) {
436-
return TrinaryLogic::createYes();
436+
$ancestorNativeReflection = $ancestor->getNativeReflection();
437+
if ($ancestorNativeReflection->hasProperty($propertyName) && !$ancestorNativeReflection->getProperty($propertyName)->isPrivate()) {
438+
return TrinaryLogic::createYes();
439+
}
437440
}
438441
}
439442
}

tests/PHPStan/Rules/Properties/UninitializedPropertyRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ public function testBug13380(): void
244244
'Class Bug13380\Baz3 has an uninitialized property $prop. Give it default value or assign it in the constructor.',
245245
57,
246246
],
247+
[
248+
'Class Bug13380\BarPrivate has an uninitialized property $prop. Give it default value or assign it in the constructor.',
249+
69,
250+
],
247251
]);
248252
}
249253

tests/PHPStan/Rules/Properties/data/bug-13380.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,15 @@ public function __construct()
5656
class Baz3 extends Bar3 {
5757
public string $prop;
5858
}
59+
60+
class FooPrivate
61+
{
62+
public function __construct(
63+
private string $prop,
64+
){
65+
}
66+
}
67+
68+
class BarPrivate extends FooPrivate {
69+
public string $prop;
70+
}

0 commit comments

Comments
 (0)