Skip to content

DRAFT [DeadCode] Skip RemoveReturnTagIncompatibleWithNativeTypeRector on a type alias nested in a union - #8319

Merged
TomasVotruba merged 3 commits into
rectorphp:mainfrom
Philosoft:fix/respect-union-of-aliases
Aug 18, 2026
Merged

DRAFT [DeadCode] Skip RemoveReturnTagIncompatibleWithNativeTypeRector on a type alias nested in a union#8319
TomasVotruba merged 3 commits into
rectorphp:mainfrom
Philosoft:fix/respect-union-of-aliases

Conversation

@Philosoft

@Philosoft Philosoft commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

A @phpstan-type alias name is not resolved to the type it stands for, it becomes a NonExistingObjectType. So "@return ConfigArray|CustomConfig" over a native "array" looked like a contradiction and the tag was removed.

The alias guard only matched when the whole @return type was a single IdentifierTypeNode. Look the name up in every identifier of the type node instead, which covers unions, nullables and intersections as well.

draft fix for rectorphp/rector#9846

@Philosoft
Philosoft marked this pull request as draft August 7, 2026 19:29
@TomasVotruba
TomasVotruba marked this pull request as ready for review August 10, 2026 20:33
…type alias nested in a union

A @phpstan-type alias name is not resolved to the type it stands for, it
becomes a NonExistingObjectType. So "@return ConfigArray|CustomConfig" over a
native "array" looked like a contradiction and the tag was removed.

The alias guard only matched when the whole @return type was a single
IdentifierTypeNode. Look the name up in every identifier of the type node
instead, which covers unions, nullables and intersections as well.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@TomasVotruba
TomasVotruba force-pushed the fix/respect-union-of-aliases branch from f2d468d to d395e98 Compare August 10, 2026 20:33
&$hasTypeAliasName
): ?int {
if ($astNode instanceof IdentifierTypeNode && isset($typeAliases[$astNode->name])) {
$hasTypeAliasName = true;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can return stop traversal early as inner traversal:

Suggested change
$hasTypeAliasName = true;
$hasTypeAliasName = true;
return \Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser::STOP_TRAVERSAL;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied in a16c2d1 — returns PhpDocNodeTraverser::STOP_TRAVERSAL once an alias name is found. Thanks!

@samsonasik

Copy link
Copy Markdown
Member

@Philosoft could you update the PR per my suggestion above? #8319 (review) Thank you.

@TomasVotruba
TomasVotruba merged commit 1cd96ae into rectorphp:main Aug 18, 2026
51 checks passed
@TomasVotruba

Copy link
Copy Markdown
Member

Let's give it a go... thank you 👍

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.

3 participants