Fix phpstan/phpstan#13380: Promoting protected property to public in subclass generates uninitialized property#5353
Open
phpstan-bot wants to merge 1 commit 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)
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