Skip to content

Commit

Permalink
Fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Nov 27, 2023
1 parent 1cf4df3 commit e64a54b
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Extension/Attribute/AsTwigFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/Extension/Attribute/AsTwigFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions src/Extension/AttributeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions tests/Extension/AttributeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']];
}
Expand Down Expand Up @@ -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']];
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Extension/Fixtures/ExtensionWithAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public function withEnvAndContextFilter(Environment $env, array $context, string
{
}

#[AsTwigFilter]
public function noArgFilter()
{
}

#[AsTwigFilter]
public function variadicFilter(string ...$strings)
{
Expand Down Expand Up @@ -62,6 +67,11 @@ public function withEnvAndContextFunction(Environment $env, array $context, stri
{
}

#[AsTwigFunction]
public function noArgFunction()
{
}

#[AsTwigFunction]
public function variadicFunction(string ...$strings)
{
Expand Down
2 changes: 0 additions & 2 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/

use Twig\Extension\AbstractExtension;
use Twig\Extension\AttributeExtension;
use Twig\Extension\DebugExtension;
use Twig\Extension\SandboxExtension;
use Twig\Extension\StringLoaderExtension;
Expand Down Expand Up @@ -45,7 +44,6 @@ public function getExtensions()
new SandboxExtension($policy, false),
new StringLoaderExtension(),
new TwigTestExtension(),
new AttributeExtension(new \ArrayIterator(['']))
];
}

Expand Down

0 comments on commit e64a54b

Please sign in to comment.