[LiveComponent] Fix BC break when using PropertyTypeExtractorInterface::getType() on a #[LiveProp] property x when getter getX exists - #2922
Conversation
ee49115 to
d071359
Compare
|
After some investigations, it happens because of When trying to resolve the type of |
This comment was marked as outdated.
This comment was marked as outdated.
LivePropPropertyTypeExtractorInterface::getType() on a #[LiveProp] property x when getter getX exists
| if (method_exists($this->propertyTypeExtractor, 'getType') && !$this->typeResolver) { | ||
| throw new \LogicException('Symfony TypeInfo is required to use LiveProps. Try running "composer require symfony/type-info".'); | ||
| } |
There was a problem hiding this comment.
Should not happens since PropertyTypeInfoExtractor::getType() exists since 7.1, which also ship TypeInfo, but just in case..
| $reflectionType = $property->getType(); | ||
| if ($reflectionType instanceof \ReflectionUnionType || $reflectionType instanceof \ReflectionIntersectionType) { | ||
| 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())); | ||
| } | ||
|
|
||
| if ($type instanceof UnionType && !$type instanceof NullableType || $type instanceof IntersectionType) { | ||
| 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".', $propertyName, $className)); | ||
| $infoType = $this->propertyTypeExtractor->getType($className, $property->getName()); | ||
|
|
||
| $collectionValueType = $infoType instanceof CollectionType ? $infoType->getCollectionValueType() : null; | ||
|
|
||
| if (null !== $collectionValueType && null !== $infoType) { | ||
| $type = $infoType; | ||
| } elseif (null !== $reflectionType) { | ||
| $type = $this->typeResolver->resolve($reflectionType); | ||
| } else { | ||
| $type = Type::mixed(); | ||
| } |
There was a problem hiding this comment.
I'm really not sure about this code, it feels fragile.
I tried to re-use the legacy logic and use ReflectionType or the result of PropertyTypeExtractorInterface::getType(), and now all tests pass, even the one added in this PR (which was previously breaking)
9cd665f to
30dca26
Compare
ccadf9a to
33e1c4c
Compare
|
I've added some comments to improve comprehensibility. Since the PR fixed the issue from issue's author (#2888 (comment)), and no tests are failing (either when using Thanks both of you :) |
…ce::getType()` on a `#[LiveProp]` property `x` when getter `getX` exists
33e1c4c to
de302ef
Compare

Prevent:
Given the LiveComponent:
Dumping
$propMetadatain\Symfony\UX\LiveComponent\LiveComponentHydrator::dehydrateValuegives:The class name is the
UserInterfaceand notUser🤔To tests it
sfcp req symfony/property-info:^6.4 -W && sfp vendor/bin/simple-phpunit tests/Functional/Form/ComponentWithFormTest.php --filter "testFormWithLivePropContainingAnEntityImplementingAnInterface"sfcp req 'symfony/property-info:7.2.*' 'symfony/type-info:7.2.*' && sfp vendor/bin/simple-phpunit tests/Functional/Form/ComponentWithFormTest.php --filter "testFormWithLivePropContainingAnEntityImplementingAnInterface"