Fix phpstan/phpstan#13380: Promoting protected property to public in subclass generates uninitialized property#5353
Open
phpstan-bot wants to merge 6 commits intophpstan:2.1.xfrom
Conversation
… false uninitialized error - When a child class redeclares a parent's promoted property (e.g. to widen visibility), the property was incorrectly reported as uninitialized - Added check in ClassPropertiesNode::getUninitializedProperties() to detect when the inherited constructor's declaring class promotes the redeclared property - Added regression test with both the valid case (inherited constructor) and invalid case (own constructor without parent::__construct)
Contributor
VincentLanglet
left a comment
There was a problem hiding this comment.
A case like
class Foo
{
public function __construct(
protected string $prop,
){
}
}
class Bar extends Foo
{
public function __construct(
string $prop,
){
parent::__construct($prop);
}
}
class Baz extends Bar {
public string $prop;
}
should be supported too.
When an intermediate class declares its own constructor that calls parent::__construct(), the promoted property lives in a grandparent class. Walk up the ancestor chain from the constructor's declaring class instead of only checking that single class. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Collaborator
Author
|
Background test suite also completed successfully. All changes have been committed and pushed. |
Contributor
VincentLanglet
left a comment
There was a problem hiding this comment.
I think
class Foo3
{
public function __construct(
protected string $prop,
){
}
}
class Bar3 extends Foo3
{
public function __construct(
){
}
}
class Baz3 extends Bar3 {
public string $prop;
}
should still be reported then
…ote it When an intermediate class overrides the constructor without a matching parameter for the promoted property, the ancestor walk now stops instead of incorrectly assuming the property is initialized. Uses constructor parameter matching as a heuristic to determine if parent::__construct is likely called. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Collaborator
Author
|
The background test suite also completed successfully (all 11708 tests passed). The fix has been committed and pushed. |
Contributor
VincentLanglet
left a comment
There was a problem hiding this comment.
class Foo
{
public function __construct(
private string $prop,
){
}
}
class Bar extends Foo {
public string $prop;
}
should still report an error
…hild 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>
Collaborator
Author
|
|
VincentLanglet
approved these changes
Apr 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a child class redeclares a property from a parent class to change its visibility (e.g.
protectedtopublic), and the parent class initializes that property via constructor promotion, PHPStan incorrectly reported the property as uninitialized. This fix recognizes that inherited constructors with promoted properties properly initialize the redeclared property.Changes
src/Node/ClassPropertiesNode.php: Added a check ingetUninitializedProperties()that detects when a property is promoted in the inherited constructor's declaring classtests/PHPStan/Rules/Properties/data/bug-13380.php: Regression test data with two scenariostestBug13380()intests/PHPStan/Rules/Properties/UninitializedPropertyRuleTest.phpRoot cause
In
ClassPropertiesNode::getUninitializedProperties(), the initialization check only looked at whether the property was promoted in the current class ($property->isPromoted()). When a child class redeclares the property (to widen visibility), the property node in the child class is not promoted — it's a plain property declaration. The parent's constructor body isn't in the child'sreturnStatementNodes, socollectUninitializedProperties()also couldn't detect the initialization.The fix adds a check: if the class has a constructor inherited from a parent class, and that parent class promotes the property, then the property is considered initialized.
Test
The regression test covers:
Bar extends Foo— redeclares$propas public (no own constructor) → should have no errorBaz extends Foo— redeclares$propas public with own empty constructor → should report uninitializedFixes phpstan/phpstan#13380