From d5053210f50ef80f4f8dfb3706614d3e1b176c8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Mon, 27 Nov 2023 02:19:51 +0100 Subject: [PATCH] Fix types --- src/Extension/Attribute/AsTwigFilter.php | 2 +- src/Extension/Attribute/AsTwigFunction.php | 2 +- src/Extension/AttributeExtension.php | 14 +++++++------- tests/Extension/AttributeExtensionTest.php | 2 ++ .../Extension/Fixtures/ExtensionWithAttributes.php | 10 ++++++++++ 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/Extension/Attribute/AsTwigFilter.php b/src/Extension/Attribute/AsTwigFilter.php index 137fe79c7ce..950a5d50dd4 100644 --- a/src/Extension/Attribute/AsTwigFilter.php +++ b/src/Extension/Attribute/AsTwigFilter.php @@ -19,7 +19,7 @@ public function __construct( * @var non-empty-string|null $name */ public ?string $name = null, - public bool $isSafe = false, + public ?array $isSafe = null, public ?string $isSafeCallback = null, public ?string $preEscape = null, public ?array $preservesSafety = null, diff --git a/src/Extension/Attribute/AsTwigFunction.php b/src/Extension/Attribute/AsTwigFunction.php index 825f1960c65..3a09e374e07 100644 --- a/src/Extension/Attribute/AsTwigFunction.php +++ b/src/Extension/Attribute/AsTwigFunction.php @@ -19,7 +19,7 @@ public function __construct( * @var non-empty-string|null $name */ public ?string $name = null, - public bool $isSafe = false, + public ?array $isSafe = null, public ?string $isSafeCallback = null, public bool|string $deprecated = false, public ?string $alternative = null, diff --git a/src/Extension/AttributeExtension.php b/src/Extension/AttributeExtension.php index e6ea06a1d73..5bd5cdfa5f0 100644 --- a/src/Extension/AttributeExtension.php +++ b/src/Extension/AttributeExtension.php @@ -104,11 +104,11 @@ private function initFromAttributes() } $parameters = $method->getParameters(); - $needsEnvironment = isset($parameters[0]) && 'env' === $parameters[0]->getName() && Environment::class === $parameters[0]->getType()->getName(); + $needsEnvironment = isset($parameters[0]) && Environment::class === $parameters[0]->getType()?->getName(); $firstParam = $needsEnvironment ? 1 : 0; - $needsContext = isset($parameters[$firstParam]) && 'context' === $parameters[$firstParam]->getName() && 'array' === $parameters[$firstParam]->getType()->getName(); + $needsContext = isset($parameters[$firstParam]) && 'context' === $parameters[$firstParam]->getName() && 'array' === $parameters[$firstParam]->getType()?->getName(); $firstParam += $needsContext ? 1 : 0; - $isVariadic = isset($parameters[$firstParam]) && $parameters[$firstParam]->isVariadic(); + $isVariadic = isset($parameters[$firstParam]) && end($parameters)->isVariadic(); $filters[$name] = new TwigFilter($name, [$objectOrClass, $method->getName()], [ 'needs_environment' => $needsEnvironment, @@ -133,11 +133,11 @@ private function initFromAttributes() } $parameters = $method->getParameters(); - $needsEnvironment = isset($parameters[0]) && Environment::class === $parameters[0]->getType()->getName(); + $needsEnvironment = isset($parameters[0]) && Environment::class === $parameters[0]->getType()?->getName(); $firstParam = $needsEnvironment ? 1 : 0; - $needsContext = isset($parameters[$firstParam]) && 'context' === $parameters[$firstParam]->getName() && 'array' === $parameters[$firstParam]->getType()->getName(); + $needsContext = isset($parameters[$firstParam]) && 'context' === $parameters[$firstParam]->getName() && 'array' === $parameters[$firstParam]->getType()?->getName(); $firstParam += $needsContext ? 1 : 0; - $isVariadic = isset($parameters[$firstParam]) && $parameters[$firstParam]->isVariadic(); + $isVariadic = isset($parameters[$firstParam]) && end($parameters)->isVariadic(); $functions[$name] = new TwigFunction($name, [$objectOrClass, $method->getName()], [ 'needs_environment' => $needsEnvironment, @@ -160,7 +160,7 @@ private function initFromAttributes() } $parameters = $method->getParameters(); - $isVariadic = isset($parameters[0]) && $parameters[0]->isVariadic(); + $isVariadic = isset($parameters[$firstParam]) && end($parameters)->isVariadic(); $tests[$name] = new TwigTest($name, [$objectOrClass, $method->getName()], [ 'is_variadic' => $isVariadic, diff --git a/tests/Extension/AttributeExtensionTest.php b/tests/Extension/AttributeExtensionTest.php index 5a0689ab46e..ebbd5e002a8 100644 --- a/tests/Extension/AttributeExtensionTest.php +++ b/tests/Extension/AttributeExtensionTest.php @@ -39,6 +39,7 @@ public static function provideFilters() yield 'with env' => ['withEnvFilter', 'withEnvFilter', ['needs_environment' => true]]; yield 'with context' => ['withContextFilter', 'withContextFilter', ['needs_context' => true]]; yield 'with env and context' => ['withEnvAndContextFilter', 'withEnvAndContextFilter', ['needs_environment' => true, 'needs_context' => true]]; + yield 'no argument' => ['noArgFilter', 'noArgFilter', []]; yield 'variadic' => ['variadicFilter', 'variadicFilter', ['is_variadic' => true]]; yield 'deprecated' => ['deprecatedFilter', 'deprecatedFilter', ['deprecated' => true, 'alternative' => 'bar']]; } @@ -68,6 +69,7 @@ public static function provideFunctions() yield 'with env' => ['withEnvFunction', 'withEnvFunction', ['needs_environment' => true]]; yield 'with context' => ['withContextFunction', 'withContextFunction', ['needs_context' => true]]; yield 'with env and context' => ['withEnvAndContextFunction', 'withEnvAndContextFunction', ['needs_environment' => true, 'needs_context' => true]]; + yield 'no argument' => ['noArgFunction', 'noArgFunction', []]; yield 'variadic' => ['variadicFunction', 'variadicFunction', ['is_variadic' => true]]; yield 'deprecated' => ['deprecatedFunction', 'deprecatedFunction', ['deprecated' => true, 'alternative' => 'bar']]; } diff --git a/tests/Extension/Fixtures/ExtensionWithAttributes.php b/tests/Extension/Fixtures/ExtensionWithAttributes.php index ca214a73382..0429e39e18b 100644 --- a/tests/Extension/Fixtures/ExtensionWithAttributes.php +++ b/tests/Extension/Fixtures/ExtensionWithAttributes.php @@ -31,6 +31,11 @@ public function withEnvAndContextFilter(Environment $env, array $context, string { } + #[AsTwigFilter] + public function noArgFilter() + { + } + #[AsTwigFilter] public function variadicFilter(string ...$strings) { @@ -62,6 +67,11 @@ public function withEnvAndContextFunction(Environment $env, array $context, stri { } + #[AsTwigFunction] + public function noArgFunction() + { + } + #[AsTwigFunction] public function variadicFunction(string ...$strings) {