Skip to content

[Php74] Skip non-nullable typed property on IfToNullCoalescingAssignRector - #8387

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-null-coalescing-assign-non-nullable-property
Aug 27, 2026
Merged

[Php74] Skip non-nullable typed property on IfToNullCoalescingAssignRector#8387
TomasVotruba merged 1 commit into
mainfrom
fix-null-coalescing-assign-non-nullable-property

Conversation

@TomasVotruba

@TomasVotruba TomasVotruba commented Aug 27, 2026

Copy link
Copy Markdown
Member

IfToNullCoalescingAssignRector rewrites a null guard if into ??= without checking the target is nullable. When the target is a typed non-nullable property, the ??= left side can never be null, so PHPStan flags it:

Static property Foo::$parameters (array) on left side of ??= is not nullable — nullCoalesce.property

Before (rule fires, produces invalid code)

class TokenHelper
{
    private static array $parameters;

    public function get(): array
    {
        // rule turns this...
        if (! isset(self::$parameters)) {
            self::$parameters = ['default'];
        }

        return self::$parameters;
    }
}
// ...into this — PHPStan: left side of ??= is not nullable
self::$parameters ??= ['default'];

Fix

Skip when the guarded target is a PropertyFetch / StaticPropertyFetch with a declared non-nullable type. Untyped properties (MixedType) still convert as before.

After

// typed non-nullable property left unchanged
if (! isset(self::$parameters)) {
    self::$parameters = ['default'];
}

@TomasVotruba
TomasVotruba marked this pull request as ready for review August 27, 2026 10:24
@TomasVotruba
TomasVotruba merged commit 19b140a into main Aug 27, 2026
44 checks passed
@TomasVotruba
TomasVotruba deleted the fix-null-coalescing-assign-non-nullable-property branch August 27, 2026 10:24
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