Skip to content

[CodeQuality] Skip uninitialized typed array property in SimplifyEmptyCheckOnEmptyArrayRector - #9060

Merged
TomasVotruba merged 1 commit into
mainfrom
skip-uninitialized-array-property-empty-check
Nov 24, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
skip-uninitialized-array-property-empty-check

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

empty($this->items) returns true on an uninitialized typed property, while $this->items === [] throws Error: must not be accessed before initialization:

final class SomeClass
{
    private array $items;

    public function setItems(array $items): void
    {
        $this->items = $items;
    }

    public function isEmpty(): bool
    {
        // was changed to "$this->items === []", which throws until setItems() runs
        return empty($this->items);
    }
}

A native array property now has to be initialized: by a default value, by constructor promotion, or by an assign in the constructor (ConstructorAssignDetector). Fixtures cover the skip and the constructor assign.

@TomasVotruba
TomasVotruba merged commit 6396211 into main Nov 24, 2026
51 checks passed
@TomasVotruba
TomasVotruba deleted the skip-uninitialized-array-property-empty-check branch November 24, 2026 16:59
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