Skip to content

[TypeDeclaration] Skip anonymous class return in NarrowObjectReturnTypeRector#8131

Merged
TomasVotruba merged 3 commits into
mainfrom
tv-fix-anon
Jul 1, 2026
Merged

[TypeDeclaration] Skip anonymous class return in NarrowObjectReturnTypeRector#8131
TomasVotruba merged 3 commits into
mainfrom
tv-fix-anon

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Fixes narrowing a method return type to an anonymous class name.

When a method returns $this from inside an anonymous class, the rule narrowed the return type to the generated anonymous class name (e.g. \AnonymousClass62d65...), producing invalid, non-referenceable code.

private function createEmail(): Email
{
    return new class extends Email {
        public function setId(int $id): Email
        {
            $this->id = $id;

            return $this; // was narrowed to \AnonymousClass...
        }
    };
}

Now anonymous classes are skipped, keeping the original (correct) Email type.

@TomasVotruba TomasVotruba enabled auto-merge (squash) July 1, 2026 12:25
@TomasVotruba TomasVotruba merged commit dce1bcb into main Jul 1, 2026
65 checks passed
@TomasVotruba TomasVotruba deleted the tv-fix-anon branch July 1, 2026 12:27
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.

2 participants