Skip to content

[TypeDeclaration] Handle Union with array type on ReturnUnionTypeRector#5144

Merged
samsonasik merged 2 commits intomainfrom
handle-null-array
Oct 8, 2023
Merged

[TypeDeclaration] Handle Union with array type on ReturnUnionTypeRector#5144
samsonasik merged 2 commits intomainfrom
handle-null-array

Conversation

@samsonasik
Copy link
Copy Markdown
Member

Given the following code:

final class UnionArray
{
    public function run($a, $b)
    {
        if ($a) {
            return null;
        }

        if ($b) {
            return new DateTime('now');
        }

        return [];
    }
}

It currently produce:

-    public function run($a, $b)
+    public function run($a, $b): ?\DateTime

which array should be included.

@samsonasik
Copy link
Copy Markdown
Member Author

The bug seems on TypeNormalizer::normalizeArrayTypeAndArrayNever()

// re-create new union types
if ($countTraversedTypes !== $countCollectedTypes && $countTraversedTypes > 2) {
return $this->typeFactory->createMixedPassedOrUnionType($collectedTypes);
}

@samsonasik
Copy link
Copy Markdown
Member Author

Fixed 🎉

Comment on lines -90 to -101
if ($traversedType instanceof UnionType) {
$traversedTypeTypes = $traversedType->getTypes();
$countTraversedTypes = count($traversedTypeTypes);

$collectedTypes = $this->getCollectedTypes($traversedTypeTypes);
$countCollectedTypes = count($collectedTypes);

// re-create new union types
if ($countTraversedTypes !== $countCollectedTypes && $countTraversedTypes > 2) {
return $this->typeFactory->createMixedPassedOrUnionType($collectedTypes);
}
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this was used by ReturnTypeDeclarationRector which read docblock 20153a9

which now we use native type for return type inferer so it should safe to remove

@samsonasik
Copy link
Copy Markdown
Member Author

All checks have passed 🎉 @TomasVotruba I am merging it ;)

@samsonasik samsonasik merged commit 3891c8d into main Oct 8, 2023
@samsonasik samsonasik deleted the handle-null-array branch October 8, 2023 10:36
samsonasik added a commit that referenced this pull request Oct 10, 2023
…or (#5144)

* [TypeDeclaration] Handle Union with array type on ReturnUnionTypeRector

* Fixed 🎉
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