Skip to content

[Php86] Add ConstructorReadonlyAssignToDefaultRector for readonly property defaults - #8502

Merged
TomasVotruba merged 1 commit into
mainfrom
worktree-php86-readonly-defaults
Sep 16, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
worktree-php86-readonly-defaults

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

PHP 8.6 allows default values on readonly properties.

This rule moves a constant constructor assignment of a readonly property to a property default value, and drops the now-redundant assignment.

 final class SomeClass
 {
-    public readonly int $number;
+    public readonly int $number = 100;

     public function __construct()
     {
-        $this->number = 100;
     }
 }

Narrow by design - only fires when:

  • the property is readonly, non-static, single, without an existing default;
  • the constructor has a top-level assignment of a constant expression (scalar, const/class-const fetch, unary +/- of those);
  • variable/param/new-object assignments and conditional (nested) assignments are skipped.

@TomasVotruba
TomasVotruba merged commit 7dee71d into main Sep 16, 2026
45 checks passed
@TomasVotruba
TomasVotruba deleted the worktree-php86-readonly-defaults branch September 16, 2026 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant