Skip to content

Commit 9cd665f

Browse files
committed
[LiveComponent] Fix BC break when using PropertyTypeExtractorInterface::getType() on a #[LiveProp] property x when getter getX exists
1 parent d071359 commit 9cd665f

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/LiveComponent/src/DependencyInjection/LiveComponentExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ function (ChildDefinition $definition, AsLiveComponent $attribute) {
191191
->setArguments([
192192
new Reference('ux.twig_component.component_factory'),
193193
new Reference('property_info'),
194+
new Reference('type_info.resolver', ContainerInterface::NULL_ON_INVALID_REFERENCE),
194195
])
195196
->addTag('kernel.reset', ['method' => 'reset'])
196197
;

src/LiveComponent/src/Metadata/LiveComponentMetadataFactory.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
1515
use Symfony\Component\PropertyInfo\Type as LegacyType;
16-
use Symfony\Component\TypeInfo\Type\IntersectionType;
17-
use Symfony\Component\TypeInfo\Type\NullableType;
18-
use Symfony\Component\TypeInfo\Type\UnionType;
16+
use Symfony\Component\TypeInfo\Type;
17+
use Symfony\Component\TypeInfo\Type\CollectionType;
18+
use Symfony\Component\TypeInfo\TypeResolver\TypeResolver;
1919
use Symfony\Contracts\Service\ResetInterface;
2020
use Symfony\UX\LiveComponent\Attribute\LiveProp;
2121
use Symfony\UX\TwigComponent\ComponentFactory;
@@ -33,7 +33,11 @@ class LiveComponentMetadataFactory implements ResetInterface
3333
public function __construct(
3434
private ComponentFactory $componentFactory,
3535
private PropertyTypeExtractorInterface $propertyTypeExtractor,
36+
private ?TypeResolver $typeResolver = null,
3637
) {
38+
if (method_exists($this->propertyTypeExtractor, 'getType') && !$this->typeResolver) {
39+
throw new \LogicException('Symfony TypeInfo is required to use LiveProps. Try running "composer require symfony/type-info".');
40+
}
3741
}
3842

3943
public function getMetadata(string $name): LiveComponentMetadata
@@ -115,10 +119,21 @@ public function createLivePropMetadata(string $className, string $propertyName,
115119
$collectionValueType
116120
);
117121
} else {
118-
$type = $this->propertyTypeExtractor->getType($className, $property->getName());
122+
$reflectionType = $property->getType();
123+
if ($reflectionType instanceof \ReflectionUnionType || $reflectionType instanceof \ReflectionIntersectionType) {
124+
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()));
125+
}
119126

120-
if ($type instanceof UnionType && !$type instanceof NullableType || $type instanceof IntersectionType) {
121-
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));
127+
$infoType = $this->propertyTypeExtractor->getType($className, $property->getName());
128+
129+
$collectionValueType = $infoType instanceof CollectionType ? $infoType->getCollectionValueType() : null;
130+
131+
if (null !== $collectionValueType && null !== $infoType) {
132+
$type = $infoType;
133+
} elseif (null !== $reflectionType) {
134+
$type = $this->typeResolver->resolve($reflectionType);
135+
} else {
136+
$type = Type::mixed();
122137
}
123138

124139
return new LivePropMetadata($property->getName(), $liveProp, $type);

0 commit comments

Comments
 (0)