Skip to content

Commit 3933533

Browse files
authored
Symfony: Add support for AsTwigComponent(template: new FromMethod(...)) (#341)
1 parent 49cbae5 commit 3933533

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

src/Provider/SymfonyUsageProvider.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use ShipMonk\PHPStan\DeadCode\Graph\UsageOrigin;
3737
use SimpleXMLElement;
3838
use SplFileInfo;
39+
use Symfony\UX\TwigComponent\Attribute\FromMethod;
3940
use UnexpectedValueException;
4041
use function array_filter;
4142
use function array_key_first;
@@ -417,6 +418,21 @@ private function getMethodUsagesFromReflection(InClassNode $node): array
417418
}
418419
}
419420

421+
foreach (['Symfony\UX\TwigComponent\Attribute\AsTwigComponent', 'Symfony\UX\LiveComponent\Attribute\AsLiveComponent'] as $twigComponentAttributeClass) {
422+
foreach ($nativeReflection->getAttributes($twigComponentAttributeClass) as $twigComponentAttribute) {
423+
$twigComponentArguments = $twigComponentAttribute->getArguments();
424+
$template = $twigComponentArguments['template'] ?? $twigComponentArguments[1] ?? null;
425+
426+
if ($template instanceof FromMethod) {
427+
$templateMethodName = $template->method;
428+
429+
if ($classReflection->hasNativeMethod($templateMethodName)) {
430+
$usages[] = $this->createUsage($classReflection->getNativeMethod($templateMethodName), 'Twig component template method via FromMethod');
431+
}
432+
}
433+
}
434+
}
435+
420436
return $usages;
421437
}
422438

tests/Rule/data/providers/symfony-ux.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,18 @@ public function getFieldName(): string { return 'field'; }
107107

108108
public function deadMethod(): void {} // error: Unused SymfonyUx\CustomActionComponent::deadMethod
109109
}
110+
111+
#[AsTwigComponent(template: new \Symfony\UX\TwigComponent\Attribute\FromMethod('getTemplatePath'))]
112+
class DynamicTemplateComponent
113+
{
114+
public function __construct() {}
115+
116+
public function mount(): void {}
117+
118+
public function getTemplatePath(): string
119+
{
120+
return 'components/dynamic.html.twig';
121+
}
122+
123+
public function deadMethod(): void {} // error: Unused SymfonyUx\DynamicTemplateComponent::deadMethod
124+
}

0 commit comments

Comments
 (0)