Skip to content

Fix phpstan/phpstan#12871: Allow readonly property init in child classes on PHP 8.4+#5513

Open
predictor2718 wants to merge 1 commit intophpstan:2.1.xfrom
predictor2718:fix-12871
Open

Fix phpstan/phpstan#12871: Allow readonly property init in child classes on PHP 8.4+#5513
predictor2718 wants to merge 1 commit intophpstan:2.1.xfrom
predictor2718:fix-12871

Conversation

@predictor2718
Copy link
Copy Markdown

Summary

Since PHP 8.4, readonly properties are implicitly protected(set), so a child class may initialize the parent's readonly property in its own constructor. PHPStan still reported property.readOnlyAssignOutOfClass for such assignments.

Changes

  • src/Rules/Properties/ReadOnlyPropertyAssignRule.php: allow the "scope != declaring class" case when PhpVersion::supportsAsymmetricVisibility() is true, the property is not private(set), and the scope is a subclass of the declaring class. The existing constructor check still applies in the subclass's scope.
  • tests/PHPStan/Rules/Properties/ReadOnlyPropertyAssignRuleTest.php: inject PhpVersion, adjust existing testRule expectations for PHP 8.4+ (subclass-constructor writes to protected/public readonly are no longer reported; subclass non-constructor write now reports "outside of the constructor" instead of "outside of its declaring class").
  • Added regression test testBug12871 with data file data/bug-12871.php covering the original snippet plus private(set) and non-constructor edge cases.

Test

vendor/bin/phpunit tests/PHPStan/Rules/Properties/ — 331 tests pass. Self-analysis [OK] No errors.

Fixes phpstan/phpstan#12871

@VincentLanglet VincentLanglet requested a review from staabm April 22, 2026 17:22
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.

2 participants