Skip to content

[DeadCode] Add RemoveDefaultValueFromAssignedPropertyRector#8207

Merged
TomasVotruba merged 2 commits into
mainfrom
remove-null-default-assigned-property
Jul 24, 2026
Merged

[DeadCode] Add RemoveDefaultValueFromAssignedPropertyRector#8207
TomasVotruba merged 2 commits into
mainfrom
remove-null-default-assigned-property

Conversation

@TomasVotruba

@TomasVotruba TomasVotruba commented Jul 24, 2026

Copy link
Copy Markdown
Member

Fixes rectorphp/rector#9822

A property with a redundant default value is never given readonly by ReadOnlyPropertyRector, because the default value blocks it. The default is dead: the property is always overwritten in the constructor.

This rule removes that redundant default from typed properties that are unconditionally assigned in the constructor, so downstream rules (like ReadOnlyPropertyRector) start working as expected.

 final class SomeClass
 {
-    private ?SomeType $someType = null;
+    private ?SomeType $someType;

     public function __construct(SomeType $someType)
     {
         $this->someType = $someType;
     }
 }

Any default is dead, not only = null:

 final class SomeClass
 {
-    private int $value = 5;
+    private int $value;

     public function __construct(int $value)
     {
         $this->value = $value;
     }
 }

Scope / guards:

  • Only typed properties. Untyped = null is already handled by RemoveNullPropertyInitializationRector.
  • Only when the property is unconditionally assigned in the constructor (conditional assignment is skipped, so an uninitialized-access error can't be introduced).
  • Properties with hooks are skipped.

@TomasVotruba
TomasVotruba force-pushed the remove-null-default-assigned-property branch from 7f3434a to a7d2850 Compare July 24, 2026 10:58
@TomasVotruba TomasVotruba changed the title [DeadCode] Add RemoveNullDefaultFromAssignedPropertyRector [DeadCode] Add RemoveDefaultValueFromAssignedPropertyRector Jul 24, 2026
@TomasVotruba
TomasVotruba merged commit 3b8b78a into main Jul 24, 2026
65 checks passed
@TomasVotruba
TomasVotruba deleted the remove-null-default-assigned-property branch July 24, 2026 11:03
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.

ReadOnlyPropertyRector misses to add "readonly" for property with default value

2 participants