Skip to content

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
phpstan-bot:create-pull-request/patch-55kt220
Open

Fix phpstan/phpstan#13380: Promoting protected property to public in subclass generates uninitialized property#5353
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-55kt220

Conversation

@phpstan-bot
Copy link
Copy Markdown
Collaborator

Summary

When a child class redeclares a property from a parent class to change its visibility (e.g. protected to public), 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

  • Modified src/Node/ClassPropertiesNode.php: Added a check in getUninitializedProperties() that detects when a property is promoted in the inherited constructor's declaring class
  • Added tests/PHPStan/Rules/Properties/data/bug-13380.php: Regression test data with two scenarios
  • Added testBug13380() in tests/PHPStan/Rules/Properties/UninitializedPropertyRuleTest.php

Root 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's returnStatementNodes, so collectUninitializedProperties() 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 $prop as public (no own constructor) → should have no error
  • Baz extends Foo — redeclares $prop as public with own empty constructor → should report uninitialized

Fixes phpstan/phpstan#13380

… 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant