Skip to content

Commit c7f20ab

Browse files
committed
Add AsTwigExtension attribute and make function name required
1 parent e64a54b commit c7f20ab

File tree

6 files changed

+53
-45
lines changed

6 files changed

+53
-45
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Twig\Extension\Attribute;
4+
5+
use Twig\TwigFilter;
6+
7+
/**
8+
* Identifies a class that uses PHP attributes to define filters, functions, or tests.
9+
*/
10+
#[\Attribute(\Attribute::TARGET_CLASS)]
11+
class AsTwigExtension
12+
{
13+
}

src/Extension/Attribute/AsTwigFilter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ class AsTwigFilter
1414
{
1515
public function __construct(
1616
/**
17-
* The name of the filter in Twig (defaults to the method name).
17+
* The name of the filter in Twig
1818
*
19-
* @var non-empty-string|null $name
19+
* @var non-empty-string $name
2020
*/
21-
public ?string $name = null,
21+
public string $name,
2222
public ?array $isSafe = null,
2323
public ?string $isSafeCallback = null,
2424
public ?string $preEscape = null,

src/Extension/Attribute/AsTwigFunction.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ class AsTwigFunction
1414
{
1515
public function __construct(
1616
/**
17-
* The name of the function in Twig (defaults to the method name).
17+
* The name of the function in Twig
1818
*
19-
* @var non-empty-string|null $name
19+
* @var non-empty-string $name
2020
*/
21-
public ?string $name = null,
21+
public string $name,
2222
public ?array $isSafe = null,
2323
public ?string $isSafeCallback = null,
2424
public bool|string $deprecated = false,

src/Extension/Attribute/AsTwigTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ class AsTwigTest
1414
{
1515
public function __construct(
1616
/**
17-
* The name of the filter in Twig (defaults to the method name).
17+
* The name of the filter in Twig.
1818
*
19-
* @var non-empty-string|null $name
19+
* @var non-empty-string $name
2020
*/
21-
public ?string $name = null,
21+
public string $name,
2222
public bool|string $deprecated = false,
2323
public ?string $alternative = null,
2424
) {

tests/Extension/AttributeExtensionTest.php

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@ public function testFilter(string $name, string $method, array $options)
3434

3535
public static function provideFilters()
3636
{
37-
yield 'basic' => ['fooFilter', 'fooFilter', []];
3837
yield 'with name' => ['foo', 'fooFilter', []];
39-
yield 'with env' => ['withEnvFilter', 'withEnvFilter', ['needs_environment' => true]];
40-
yield 'with context' => ['withContextFilter', 'withContextFilter', ['needs_context' => true]];
41-
yield 'with env and context' => ['withEnvAndContextFilter', 'withEnvAndContextFilter', ['needs_environment' => true, 'needs_context' => true]];
42-
yield 'no argument' => ['noArgFilter', 'noArgFilter', []];
43-
yield 'variadic' => ['variadicFilter', 'variadicFilter', ['is_variadic' => true]];
44-
yield 'deprecated' => ['deprecatedFilter', 'deprecatedFilter', ['deprecated' => true, 'alternative' => 'bar']];
38+
yield 'with env' => ['with_env_filter', 'withEnvFilter', ['needs_environment' => true]];
39+
yield 'with context' => ['with_context_filter', 'withContextFilter', ['needs_context' => true]];
40+
yield 'with env and context' => ['with_env_and_context_filter', 'withEnvAndContextFilter', ['needs_environment' => true, 'needs_context' => true]];
41+
yield 'no argument' => ['no_arg_filter', 'noArgFilter', []];
42+
yield 'variadic' => ['variadic_filter', 'variadicFilter', ['is_variadic' => true]];
43+
yield 'deprecated' => ['deprecated_filter', 'deprecatedFilter', ['deprecated' => true, 'alternative' => 'bar']];
4544
}
4645

4746
/**
@@ -64,14 +63,13 @@ public function testFunction(string $name, string $method, array $options)
6463

6564
public static function provideFunctions()
6665
{
67-
yield 'basic' => ['fooFunction', 'fooFunction', []];
6866
yield 'with name' => ['foo', 'fooFunction', []];
69-
yield 'with env' => ['withEnvFunction', 'withEnvFunction', ['needs_environment' => true]];
70-
yield 'with context' => ['withContextFunction', 'withContextFunction', ['needs_context' => true]];
71-
yield 'with env and context' => ['withEnvAndContextFunction', 'withEnvAndContextFunction', ['needs_environment' => true, 'needs_context' => true]];
72-
yield 'no argument' => ['noArgFunction', 'noArgFunction', []];
73-
yield 'variadic' => ['variadicFunction', 'variadicFunction', ['is_variadic' => true]];
74-
yield 'deprecated' => ['deprecatedFunction', 'deprecatedFunction', ['deprecated' => true, 'alternative' => 'bar']];
67+
yield 'with env' => ['with_env_function', 'withEnvFunction', ['needs_environment' => true]];
68+
yield 'with context' => ['with_context_function', 'withContextFunction', ['needs_context' => true]];
69+
yield 'with env and context' => ['with_env_and_context_function', 'withEnvAndContextFunction', ['needs_environment' => true, 'needs_context' => true]];
70+
yield 'no argument' => ['no_arg_function', 'noArgFunction', []];
71+
yield 'variadic' => ['variadic_function', 'variadicFunction', ['is_variadic' => true]];
72+
yield 'deprecated' => ['deprecated_function', 'deprecatedFunction', ['deprecated' => true, 'alternative' => 'bar']];
7573
}
7674

7775
/**
@@ -94,10 +92,9 @@ public function testTest(string $name, string $method, array $options)
9492

9593
public static function provideTests()
9694
{
97-
yield 'basic' => ['fooTest', 'fooTest', []];
9895
yield 'with name' => ['foo', 'fooTest', []];
99-
yield 'variadic' => ['variadicTest', 'variadicTest', ['is_variadic' => true]];
100-
yield 'deprecated' => ['deprecatedTest', 'deprecatedTest', ['deprecated' => true, 'alternative' => 'bar']];
96+
yield 'variadic' => ['variadic_test', 'variadicTest', ['is_variadic' => true]];
97+
yield 'deprecated' => ['deprecated_test', 'deprecatedTest', ['deprecated' => true, 'alternative' => 'bar']];
10198
}
10299

103100
public function testRuntimeExtension()

tests/Extension/Fixtures/ExtensionWithAttributes.php

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,97 +3,95 @@
33
namespace Twig\Tests\Extension\Fixtures;
44

55
use Twig\Environment;
6+
use Twig\Extension\Attribute\AsTwigExtension;
67
use Twig\Extension\Attribute\AsTwigFilter;
78
use Twig\Extension\Attribute\AsTwigFunction;
89
use Twig\Extension\Attribute\AsTwigTest;
9-
use Twig\Extension\RuntimeExtensionInterface;
1010

11-
class ExtensionWithAttributes implements RuntimeExtensionInterface
11+
#[AsTwigExtension]
12+
class ExtensionWithAttributes
1213
{
13-
#[AsTwigFilter]
1414
#[AsTwigFilter(name: 'foo')]
1515
public function fooFilter(string $string)
1616
{
1717
}
1818

19-
#[AsTwigFilter]
19+
#[AsTwigFilter('with_context_filter')]
2020
public function withContextFilter(array $context, string $string)
2121
{
2222
}
2323

24-
#[AsTwigFilter]
24+
#[AsTwigFilter('with_env_filter')]
2525
public function withEnvFilter(Environment $env, string $string)
2626
{
2727
}
2828

29-
#[AsTwigFilter]
29+
#[AsTwigFilter('with_env_and_context_filter')]
3030
public function withEnvAndContextFilter(Environment $env, array $context, string $string)
3131
{
3232
}
3333

34-
#[AsTwigFilter]
34+
#[AsTwigFilter('no_arg_filter')]
3535
public function noArgFilter()
3636
{
3737
}
3838

39-
#[AsTwigFilter]
39+
#[AsTwigFilter('variadic_filter')]
4040
public function variadicFilter(string ...$strings)
4141
{
4242
}
4343

44-
#[AsTwigFilter(deprecated: true, alternative: 'bar')]
44+
#[AsTwigFilter('deprecated_filter', deprecated: true, alternative: 'bar')]
4545
public function deprecatedFilter(string $string)
4646
{
4747
}
4848

49-
#[AsTwigFunction]
5049
#[AsTwigFunction(name: 'foo')]
5150
public function fooFunction(string $string)
5251
{
5352
}
5453

55-
#[AsTwigFunction]
54+
#[AsTwigFunction('with_context_function')]
5655
public function withContextFunction(array $context, string $string)
5756
{
5857
}
5958

60-
#[AsTwigFunction]
59+
#[AsTwigFunction('with_env_function')]
6160
public function withEnvFunction(Environment $env, string $string)
6261
{
6362
}
6463

65-
#[AsTwigFunction]
64+
#[AsTwigFunction('with_env_and_context_function')]
6665
public function withEnvAndContextFunction(Environment $env, array $context, string $string)
6766
{
6867
}
6968

70-
#[AsTwigFunction]
69+
#[AsTwigFunction('no_arg_function')]
7170
public function noArgFunction()
7271
{
7372
}
7473

75-
#[AsTwigFunction]
74+
#[AsTwigFunction('variadic_function')]
7675
public function variadicFunction(string ...$strings)
7776
{
7877
}
7978

80-
#[AsTwigFunction(deprecated: true, alternative: 'bar')]
79+
#[AsTwigFunction('deprecated_function', deprecated: true, alternative: 'bar')]
8180
public function deprecatedFunction(string $string)
8281
{
8382
}
8483

85-
#[AsTwigTest]
8684
#[AsTwigTest(name: 'foo')]
8785
public function fooTest(string $string)
8886
{
8987
}
9088

91-
#[AsTwigTest]
89+
#[AsTwigTest('variadic_test')]
9290
public function variadicTest(string ...$strings)
9391
{
9492
}
9593

96-
#[AsTwigTest(deprecated: true, alternative: 'bar')]
94+
#[AsTwigTest('deprecated_test', deprecated: true, alternative: 'bar')]
9795
public function deprecatedTest(string $string)
9896
{
9997
}

0 commit comments

Comments
 (0)