Skip to content

Commit 5c82319

Browse files
committed
ensure AsTwigComponent::forClass() returns or throws
1 parent 84e4928 commit 5c82319

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/TwigComponent/src/Attribute/AsTwigComponent.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ final public function getTemplate(): string
4040

4141
/**
4242
* @internal
43+
*
44+
* @return static
4345
*/
44-
final public static function forClass(string $class): ?self
46+
final public static function forClass(string $class): self
4547
{
4648
$class = new \ReflectionClass($class);
4749

48-
if (!$attribute = $class->getAttributes(self::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null) {
49-
return null;
50+
if (!$attribute = $class->getAttributes(static::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null) {
51+
throw new \InvalidArgumentException(sprintf('"%s" is not a Twig Component, did you forget to add the "%s" attribute?', $class, static::class));
5052
}
5153

5254
return $attribute->newInstance();

src/TwigComponent/src/ComponentRenderer.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ public function __construct(Environment $twig)
3131
public function render(object $component): string
3232
{
3333
// TODO: Self-Rendering components?
34-
if (!$attribute = AsTwigComponent::forClass($component::class)) {
35-
throw new \InvalidArgumentException(sprintf('"%s" is not a Twig Component, did you forget to add the AsTwigComponent attribute?', $component::class));
36-
}
34+
$attribute = AsTwigComponent::forClass($component::class);
3735

3836
return $this->twig->render($attribute->getTemplate(), ['this' => $component]);
3937
}

0 commit comments

Comments
 (0)