Skip to content

Commit 8af2726

Browse files
committed
bug #1745 Allow debuging components with union typed props (alucas-campings)
This PR was merged into the 2.x branch. Discussion ---------- Allow debuging components with union typed props | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Issues | N.A. | License | MIT When a component has a property with an union type, the debug command crash with `Attempted to call an undefined method named "getName" of class "ReflectionUnionType"`. This fix the issue. Commits ------- 8b2719c Allow debuging components with union typed props
2 parents c4e13f1 + 8b2719c commit 8af2726

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/TwigComponent/src/Command/TwigComponentDebugCommand.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,14 @@ private function getComponentProperties(ComponentMetadata $metadata): array
255255
$propertyName = $property->getName();
256256

257257
if ($metadata->isPublicPropsExposed() && $property->isPublic()) {
258-
$visibility = $property->getType()?->getName();
258+
$type = $property->getType();
259+
if ($type instanceof \ReflectionNamedType) {
260+
$typeName = $type->getName();
261+
} else {
262+
$typeName = (string) $type;
263+
}
259264
$value = $property->getDefaultValue();
260-
$propertyDisplay = $visibility.' $'.$propertyName.(null !== $value ? ' = '.json_encode($value) : '');
265+
$propertyDisplay = $typeName.' $'.$propertyName.(null !== $value ? ' = '.json_encode($value) : '');
261266
$properties[$property->name] = $propertyDisplay;
262267
}
263268

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Symfony\UX\TwigComponent\Tests\Fixtures\Component;
4+
5+
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
6+
7+
#[AsTwigComponent(name: 'union_type_props', exposePublicProps: true)]
8+
final class UnionTypeProps
9+
{
10+
public string|bool $prop1;
11+
}

src/TwigComponent/tests/Integration/Command/TwigComponentDebugCommandTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function testWithAnonymousComponent(): void
141141
$this->assertStringContainsString('primary = true', $display);
142142
}
143143

144-
public function testWithoutPublicPros(): void
144+
public function testWithoutPublicProps(): void
145145
{
146146
$commandTester = $this->createCommandTester();
147147
$commandTester->execute(['name' => 'no_public_props']);
@@ -173,6 +173,20 @@ public function testWithExposedVariables(): void
173173
$this->assertStringNotContainsString('prop3', $display);
174174
}
175175

176+
public function testWithUnionTypeProps(): void
177+
{
178+
$commandTester = $this->createCommandTester();
179+
$commandTester->execute(['name' => 'union_type_props']);
180+
181+
$commandTester->assertCommandIsSuccessful();
182+
183+
$display = $commandTester->getDisplay();
184+
185+
$this->tableDisplayCheck($display);
186+
$this->assertStringContainsString('string|bool', $display);
187+
$this->assertStringContainsString('prop1', $display);
188+
}
189+
176190
private function createCommandTester(): CommandTester
177191
{
178192
$kernel = self::bootKernel();

0 commit comments

Comments
 (0)