Skip to content

[LiveComponent] Throw clear error when using union types for LiveProps #856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2023

Conversation

sneakyvv
Copy link
Contributor

@sneakyvv sneakyvv commented May 8, 2023

Q A
Bug fix? yes
New feature? no
Tickets Fix #853
License MIT

As mentioned in #853 (comment), supporting union types for LiveProps will be quite tricky. Given that, at least a clear error should be thrown.

Copy link
Member

@kbond kbond left a comment

Choose a reason for hiding this comment

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

Does this problem exist for intersection types as well? (\ReflectionIntersectionType)

@sneakyvv
Copy link
Contributor Author

sneakyvv commented May 9, 2023

I considered that, but I wrongly assumed that intersection types are only used for method argument types, to enforce an argument to implement two interfaces, and thus not for property types, which does not make much sense. However, I checked now, and it is allowed (although the use case might be less common imo).

I updated the code, since it is possible for properties as well.

@@ -71,6 +71,9 @@ public function createPropMetadatas(\ReflectionClass $class): array
}

$type = $property->getType();
if ($type instanceof \ReflectionUnionType || $type instanceof \ReflectionIntersectionType) {
throw new \LogicException(sprintf('Union nor intersection types are not supported for LiveProps. You may want to change the type of property %s in %s.', $property->getName(), $property->getDeclaringClass()->getName()));
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
throw new \LogicException(sprintf('Union nor intersection types are not supported for LiveProps. You may want to change the type of property %s in %s.', $property->getName(), $property->getDeclaringClass()->getName()));
throw new \LogicException(sprintf('Union or intersection types are not supported for LiveProps. You may want to change the type of property %s in %s.', $property->getName(), $property->getDeclaringClass()->getName()));

Just to avoid the double-negative of nor and not supported.

Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

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

One tiny wording tweak - but I think this is the correct approach. And we could always revisit later 👍

@sneakyvv
Copy link
Contributor Author

sneakyvv commented May 9, 2023

@weaverryan that's indeed a double negative 🙈
Fixed it!

@weaverryan
Copy link
Member

Thanks @sneakyvv!

@weaverryan weaverryan merged commit 9fc90d4 into symfony:2.x May 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[LiveComponent] Support UnionTypes for LiveProps
3 participants