Skip to content

[DeadCode] Skip static property in RemoveDefaultValueFromAssignedPropertyRector - #8240

Merged
TomasVotruba merged 1 commit into
mainfrom
skip-static-property-default-value
Jul 31, 2026
Merged

[DeadCode] Skip static property in RemoveDefaultValueFromAssignedPropertyRector#8240
TomasVotruba merged 1 commit into
mainfrom
skip-static-property-default-value

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

A static property is shared per class, not per instance. Removing its default value makes it uninitialized until the first constructor call, so any read before that (or without ever instantiating the class) throws Error: Typed static property must not be accessed before initialization.

ConstructorAssignDetector matches StaticPropertyFetch as well as PropertyFetch, so self::$value = $value; in the constructor was treated as "always assigned" and the default was dropped.

 final class SkipStaticProperty
 {
-    private static ?int $value = null;
+    private static ?int $value;

     public function __construct(int $value)
     {
         self::$value = $value;
     }

     public static function getValue(): ?int
     {
         return self::$value;
     }
 }

SkipStaticProperty::getValue() throws after the change, without ever constructing the class.

Static properties are now skipped.

@TomasVotruba
TomasVotruba merged commit e296621 into main Jul 31, 2026
64 of 86 checks passed
@TomasVotruba
TomasVotruba deleted the skip-static-property-default-value branch July 31, 2026 13:35
@TomasVotruba

Copy link
Copy Markdown
Member Author

Packagist is down, but feature is correct 👍

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