From 6d0c5d2ab6fc5ede68c122589d1362fcc3d88bca Mon Sep 17 00:00:00 2001 From: Alexander Bolshakov Date: Tue, 6 Dec 2022 18:01:29 +0300 Subject: [PATCH 1/4] Moved global functions under namespaces. --- src/Extension/CoreExtension.php | 82 +++++++++---------- src/Extension/DebugExtension.php | 4 +- src/Extension/EscaperExtension.php | 8 +- src/Extension/StringLoaderExtension.php | 4 +- src/Node/Expression/Binary/EqualBinary.php | 2 +- src/Node/Expression/Binary/GreaterBinary.php | 2 +- .../Expression/Binary/GreaterEqualBinary.php | 2 +- src/Node/Expression/Binary/InBinary.php | 2 +- src/Node/Expression/Binary/LessBinary.php | 2 +- .../Expression/Binary/LessEqualBinary.php | 2 +- src/Node/Expression/Binary/NotEqualBinary.php | 2 +- src/Node/Expression/Binary/NotInBinary.php | 2 +- src/Node/Expression/FunctionExpression.php | 2 +- src/Node/Expression/GetAttrExpression.php | 2 +- src/Node/Expression/MethodCallExpression.php | 2 +- src/Node/ForNode.php | 2 +- src/Node/IncludeNode.php | 4 +- src/Node/WithNode.php | 4 +- src/Test/NodeTestCase.php | 2 +- tests/Extension/CoreTest.php | 9 ++ tests/Extension/EscaperTest.php | 2 + tests/Extension/StringLoaderExtensionTest.php | 3 + tests/IntegrationTest.php | 2 + tests/TemplateTest.php | 2 + 24 files changed, 84 insertions(+), 66 deletions(-) diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index b7798585987..56576e154cb 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -175,50 +175,50 @@ public function getFilters(): array { return [ // formatting filters - new TwigFilter('date', 'twig_date_format_filter', ['needs_environment' => true]), - new TwigFilter('date_modify', 'twig_date_modify_filter', ['needs_environment' => true]), - new TwigFilter('format', 'twig_sprintf'), - new TwigFilter('replace', 'twig_replace_filter'), - new TwigFilter('number_format', 'twig_number_format_filter', ['needs_environment' => true]), + new TwigFilter('date', '\Twig\Extension\Core\Functions\twig_date_format_filter', ['needs_environment' => true]), + new TwigFilter('date_modify', '\Twig\Extension\Core\Functions\twig_date_modify_filter', ['needs_environment' => true]), + new TwigFilter('format', '\Twig\Extension\Core\Functions\twig_sprintf'), + new TwigFilter('replace', '\Twig\Extension\Core\Functions\twig_replace_filter'), + new TwigFilter('number_format', '\Twig\Extension\Core\Functions\twig_number_format_filter', ['needs_environment' => true]), new TwigFilter('abs', 'abs'), - new TwigFilter('round', 'twig_round'), + new TwigFilter('round', '\Twig\Extension\Core\Functions\twig_round'), // encoding - new TwigFilter('url_encode', 'twig_urlencode_filter'), + new TwigFilter('url_encode', '\Twig\Extension\Core\Functions\twig_urlencode_filter'), new TwigFilter('json_encode', 'json_encode'), - new TwigFilter('convert_encoding', 'twig_convert_encoding'), + new TwigFilter('convert_encoding', '\Twig\Extension\Core\Functions\twig_convert_encoding'), // string filters - new TwigFilter('title', 'twig_title_string_filter', ['needs_environment' => true]), - new TwigFilter('capitalize', 'twig_capitalize_string_filter', ['needs_environment' => true]), - new TwigFilter('upper', 'twig_upper_filter', ['needs_environment' => true]), - new TwigFilter('lower', 'twig_lower_filter', ['needs_environment' => true]), - new TwigFilter('striptags', 'twig_striptags'), - new TwigFilter('trim', 'twig_trim_filter'), - new TwigFilter('nl2br', 'twig_nl2br', ['pre_escape' => 'html', 'is_safe' => ['html']]), - new TwigFilter('spaceless', 'twig_spaceless', ['is_safe' => ['html']]), + new TwigFilter('title', '\Twig\Extension\Core\Functions\twig_title_string_filter', ['needs_environment' => true]), + new TwigFilter('capitalize', '\Twig\Extension\Core\Functions\twig_capitalize_string_filter', ['needs_environment' => true]), + new TwigFilter('upper', '\Twig\Extension\Core\Functions\twig_upper_filter', ['needs_environment' => true]), + new TwigFilter('lower', '\Twig\Extension\Core\Functions\twig_lower_filter', ['needs_environment' => true]), + new TwigFilter('striptags', '\Twig\Extension\Core\Functions\twig_striptags'), + new TwigFilter('trim', '\Twig\Extension\Core\Functions\twig_trim_filter'), + new TwigFilter('nl2br', '\Twig\Extension\Core\Functions\twig_nl2br', ['pre_escape' => 'html', 'is_safe' => ['html']]), + new TwigFilter('spaceless', '\Twig\Extension\Core\Functions\twig_spaceless', ['is_safe' => ['html']]), // array helpers - new TwigFilter('join', 'twig_join_filter'), - new TwigFilter('split', 'twig_split_filter', ['needs_environment' => true]), - new TwigFilter('sort', 'twig_sort_filter', ['needs_environment' => true]), - new TwigFilter('merge', 'twig_array_merge'), - new TwigFilter('batch', 'twig_array_batch'), - new TwigFilter('column', 'twig_array_column'), - new TwigFilter('filter', 'twig_array_filter', ['needs_environment' => true]), - new TwigFilter('map', 'twig_array_map', ['needs_environment' => true]), - new TwigFilter('reduce', 'twig_array_reduce', ['needs_environment' => true]), + new TwigFilter('join', '\Twig\Extension\Core\Functions\twig_join_filter'), + new TwigFilter('split', '\Twig\Extension\Core\Functions\twig_split_filter', ['needs_environment' => true]), + new TwigFilter('sort', '\Twig\Extension\Core\Functions\twig_sort_filter', ['needs_environment' => true]), + new TwigFilter('merge', '\Twig\Extension\Core\Functions\twig_array_merge'), + new TwigFilter('batch', '\Twig\Extension\Core\Functions\twig_array_batch'), + new TwigFilter('column', '\Twig\Extension\Core\Functions\twig_array_column'), + new TwigFilter('filter', '\Twig\Extension\Core\Functions\twig_array_filter', ['needs_environment' => true]), + new TwigFilter('map', '\Twig\Extension\Core\Functions\twig_array_map', ['needs_environment' => true]), + new TwigFilter('reduce', '\Twig\Extension\Core\Functions\twig_array_reduce', ['needs_environment' => true]), // string/array filters - new TwigFilter('reverse', 'twig_reverse_filter', ['needs_environment' => true]), - new TwigFilter('length', 'twig_length_filter', ['needs_environment' => true]), - new TwigFilter('slice', 'twig_slice', ['needs_environment' => true]), - new TwigFilter('first', 'twig_first', ['needs_environment' => true]), - new TwigFilter('last', 'twig_last', ['needs_environment' => true]), + new TwigFilter('reverse', '\Twig\Extension\Core\Functions\twig_reverse_filter', ['needs_environment' => true]), + new TwigFilter('length', '\Twig\Extension\Core\Functions\twig_length_filter', ['needs_environment' => true]), + new TwigFilter('slice', '\Twig\Extension\Core\Functions\twig_slice', ['needs_environment' => true]), + new TwigFilter('first', '\Twig\Extension\Core\Functions\twig_first', ['needs_environment' => true]), + new TwigFilter('last', '\Twig\Extension\Core\Functions\twig_last', ['needs_environment' => true]), // iteration and runtime - new TwigFilter('default', '_twig_default_filter', ['node_class' => DefaultFilter::class]), - new TwigFilter('keys', 'twig_get_array_keys_filter'), + new TwigFilter('default', '\Twig\Extension\Core\Functions\_twig_default_filter', ['node_class' => DefaultFilter::class]), + new TwigFilter('keys', '\Twig\Extension\Core\Functions\twig_get_array_keys_filter'), ]; } @@ -228,12 +228,12 @@ public function getFunctions(): array new TwigFunction('max', 'max'), new TwigFunction('min', 'min'), new TwigFunction('range', 'range'), - new TwigFunction('constant', 'twig_constant'), - new TwigFunction('cycle', 'twig_cycle'), - new TwigFunction('random', 'twig_random', ['needs_environment' => true]), - new TwigFunction('date', 'twig_date_converter', ['needs_environment' => true]), - new TwigFunction('include', 'twig_include', ['needs_environment' => true, 'needs_context' => true, 'is_safe' => ['all']]), - new TwigFunction('source', 'twig_source', ['needs_environment' => true, 'is_safe' => ['all']]), + new TwigFunction('constant', '\Twig\Extension\Core\Functions\twig_constant'), + new TwigFunction('cycle', '\Twig\Extension\Core\Functions\twig_cycle'), + new TwigFunction('random', '\Twig\Extension\Core\Functions\twig_random', ['needs_environment' => true]), + new TwigFunction('date', '\Twig\Extension\Core\Functions\twig_date_converter', ['needs_environment' => true]), + new TwigFunction('include', '\Twig\Extension\Core\Functions\twig_include', ['needs_environment' => true, 'needs_context' => true, 'is_safe' => ['all']]), + new TwigFunction('source', '\Twig\Extension\Core\Functions\twig_source', ['needs_environment' => true, 'is_safe' => ['all']]), ]; } @@ -248,8 +248,8 @@ public function getTests(): array new TwigTest('null', null, ['node_class' => NullTest::class]), new TwigTest('divisible by', null, ['node_class' => DivisiblebyTest::class, 'one_mandatory_argument' => true]), new TwigTest('constant', null, ['node_class' => ConstantTest::class]), - new TwigTest('empty', 'twig_test_empty'), - new TwigTest('iterable', 'twig_test_iterable'), + new TwigTest('empty', '\Twig\Extension\Core\Functions\twig_test_empty'), + new TwigTest('iterable', '\Twig\Extension\Core\Functions\twig_test_iterable'), ]; } @@ -302,7 +302,7 @@ public function getOperators(): array } } -namespace { +namespace Twig\Extension\Core\Functions { use Twig\Environment; use Twig\Error\LoaderError; use Twig\Error\RuntimeError; diff --git a/src/Extension/DebugExtension.php b/src/Extension/DebugExtension.php index bfb23d7bd4f..1236a036ca0 100644 --- a/src/Extension/DebugExtension.php +++ b/src/Extension/DebugExtension.php @@ -27,13 +27,13 @@ public function getFunctions(): array ; return [ - new TwigFunction('dump', 'twig_var_dump', ['is_safe' => $isDumpOutputHtmlSafe ? ['html'] : [], 'needs_context' => true, 'needs_environment' => true, 'is_variadic' => true]), + new TwigFunction('dump', '\Twig\Extension\Debug\Functions\twig_var_dump', ['is_safe' => $isDumpOutputHtmlSafe ? ['html'] : [], 'needs_context' => true, 'needs_environment' => true, 'is_variadic' => true]), ]; } } } -namespace { +namespace Twig\Extension\Debug\Functions { use Twig\Environment; use Twig\Template; use Twig\TemplateWrapper; diff --git a/src/Extension/EscaperExtension.php b/src/Extension/EscaperExtension.php index 9d2251dc6e1..47eb4339901 100644 --- a/src/Extension/EscaperExtension.php +++ b/src/Extension/EscaperExtension.php @@ -49,9 +49,9 @@ public function getNodeVisitors(): array public function getFilters(): array { return [ - new TwigFilter('escape', 'twig_escape_filter', ['needs_environment' => true, 'is_safe_callback' => 'twig_escape_filter_is_safe']), - new TwigFilter('e', 'twig_escape_filter', ['needs_environment' => true, 'is_safe_callback' => 'twig_escape_filter_is_safe']), - new TwigFilter('raw', 'twig_raw_filter', ['is_safe' => ['all']]), + new TwigFilter('escape', '\Twig\Extension\Escaper\Functions\twig_escape_filter', ['needs_environment' => true, 'is_safe_callback' => '\Twig\Extension\Escaper\Functions\twig_escape_filter_is_safe']), + new TwigFilter('e', '\Twig\Extension\Escaper\Functions\twig_escape_filter', ['needs_environment' => true, 'is_safe_callback' => '\Twig\Extension\Escaper\Functions\twig_escape_filter_is_safe']), + new TwigFilter('raw', '\Twig\Extension\Escaper\Functions\twig_raw_filter', ['is_safe' => ['all']]), ]; } @@ -135,7 +135,7 @@ public function addSafeClass(string $class, array $strategies) } } -namespace { +namespace Twig\Extension\Escaper\Functions { use Twig\Environment; use Twig\Error\RuntimeError; use Twig\Extension\EscaperExtension; diff --git a/src/Extension/StringLoaderExtension.php b/src/Extension/StringLoaderExtension.php index 7b451471007..6ed197e6b2c 100644 --- a/src/Extension/StringLoaderExtension.php +++ b/src/Extension/StringLoaderExtension.php @@ -17,13 +17,13 @@ final class StringLoaderExtension extends AbstractExtension public function getFunctions(): array { return [ - new TwigFunction('template_from_string', 'twig_template_from_string', ['needs_environment' => true]), + new TwigFunction('template_from_string', '\Twig\Extension\StringLoader\Functions\twig_template_from_string', ['needs_environment' => true]), ]; } } } -namespace { +namespace Twig\Extension\StringLoader\Functions { use Twig\Environment; use Twig\TemplateWrapper; diff --git a/src/Node/Expression/Binary/EqualBinary.php b/src/Node/Expression/Binary/EqualBinary.php index 6b48549ef26..90f887675e7 100644 --- a/src/Node/Expression/Binary/EqualBinary.php +++ b/src/Node/Expression/Binary/EqualBinary.php @@ -24,7 +24,7 @@ public function compile(Compiler $compiler): void } $compiler - ->raw('(0 === twig_compare(') + ->raw('(0 === \Twig\Extension\Core\Functions\twig_compare(') ->subcompile($this->getNode('left')) ->raw(', ') ->subcompile($this->getNode('right')) diff --git a/src/Node/Expression/Binary/GreaterBinary.php b/src/Node/Expression/Binary/GreaterBinary.php index e1dd06780b7..758f846701f 100644 --- a/src/Node/Expression/Binary/GreaterBinary.php +++ b/src/Node/Expression/Binary/GreaterBinary.php @@ -24,7 +24,7 @@ public function compile(Compiler $compiler): void } $compiler - ->raw('(1 === twig_compare(') + ->raw('(1 === \Twig\Extension\Core\Functions\twig_compare(') ->subcompile($this->getNode('left')) ->raw(', ') ->subcompile($this->getNode('right')) diff --git a/src/Node/Expression/Binary/GreaterEqualBinary.php b/src/Node/Expression/Binary/GreaterEqualBinary.php index df9bfcfbf9d..8f6d7ae2050 100644 --- a/src/Node/Expression/Binary/GreaterEqualBinary.php +++ b/src/Node/Expression/Binary/GreaterEqualBinary.php @@ -24,7 +24,7 @@ public function compile(Compiler $compiler): void } $compiler - ->raw('(0 <= twig_compare(') + ->raw('(0 <= \Twig\Extension\Core\Functions\twig_compare(') ->subcompile($this->getNode('left')) ->raw(', ') ->subcompile($this->getNode('right')) diff --git a/src/Node/Expression/Binary/InBinary.php b/src/Node/Expression/Binary/InBinary.php index 6dbfa97f05c..c65bc1161d7 100644 --- a/src/Node/Expression/Binary/InBinary.php +++ b/src/Node/Expression/Binary/InBinary.php @@ -18,7 +18,7 @@ class InBinary extends AbstractBinary public function compile(Compiler $compiler): void { $compiler - ->raw('twig_in_filter(') + ->raw('\Twig\Extension\Core\Functions\twig_in_filter(') ->subcompile($this->getNode('left')) ->raw(', ') ->subcompile($this->getNode('right')) diff --git a/src/Node/Expression/Binary/LessBinary.php b/src/Node/Expression/Binary/LessBinary.php index 598e629134b..fc69277b6cf 100644 --- a/src/Node/Expression/Binary/LessBinary.php +++ b/src/Node/Expression/Binary/LessBinary.php @@ -24,7 +24,7 @@ public function compile(Compiler $compiler): void } $compiler - ->raw('(-1 === twig_compare(') + ->raw('(-1 === \Twig\Extension\Core\Functions\twig_compare(') ->subcompile($this->getNode('left')) ->raw(', ') ->subcompile($this->getNode('right')) diff --git a/src/Node/Expression/Binary/LessEqualBinary.php b/src/Node/Expression/Binary/LessEqualBinary.php index e3c4af58d4c..ad123350289 100644 --- a/src/Node/Expression/Binary/LessEqualBinary.php +++ b/src/Node/Expression/Binary/LessEqualBinary.php @@ -24,7 +24,7 @@ public function compile(Compiler $compiler): void } $compiler - ->raw('(0 >= twig_compare(') + ->raw('(0 >= \Twig\Extension\Core\Functions\twig_compare(') ->subcompile($this->getNode('left')) ->raw(', ') ->subcompile($this->getNode('right')) diff --git a/src/Node/Expression/Binary/NotEqualBinary.php b/src/Node/Expression/Binary/NotEqualBinary.php index db47a289050..1fc344a1797 100644 --- a/src/Node/Expression/Binary/NotEqualBinary.php +++ b/src/Node/Expression/Binary/NotEqualBinary.php @@ -24,7 +24,7 @@ public function compile(Compiler $compiler): void } $compiler - ->raw('(0 !== twig_compare(') + ->raw('(0 !== \Twig\Extension\Core\Functions\twig_compare(') ->subcompile($this->getNode('left')) ->raw(', ') ->subcompile($this->getNode('right')) diff --git a/src/Node/Expression/Binary/NotInBinary.php b/src/Node/Expression/Binary/NotInBinary.php index fcba6cca1cb..77d41c5e555 100644 --- a/src/Node/Expression/Binary/NotInBinary.php +++ b/src/Node/Expression/Binary/NotInBinary.php @@ -18,7 +18,7 @@ class NotInBinary extends AbstractBinary public function compile(Compiler $compiler): void { $compiler - ->raw('!twig_in_filter(') + ->raw('!\Twig\Extension\Core\Functions\twig_in_filter(') ->subcompile($this->getNode('left')) ->raw(', ') ->subcompile($this->getNode('right')) diff --git a/src/Node/Expression/FunctionExpression.php b/src/Node/Expression/FunctionExpression.php index 71269775c3d..24893be7c56 100644 --- a/src/Node/Expression/FunctionExpression.php +++ b/src/Node/Expression/FunctionExpression.php @@ -33,7 +33,7 @@ public function compile(Compiler $compiler) $this->setAttribute('arguments', $function->getArguments()); $callable = $function->getCallable(); if ('constant' === $name && $this->getAttribute('is_defined_test')) { - $callable = 'twig_constant_is_defined'; + $callable = '\Twig\Extension\Core\Functions\twig_constant_is_defined'; } $this->setAttribute('callable', $callable); $this->setAttribute('is_variadic', $function->isVariadic()); diff --git a/src/Node/Expression/GetAttrExpression.php b/src/Node/Expression/GetAttrExpression.php index e6a75ce9404..8242a3f259d 100644 --- a/src/Node/Expression/GetAttrExpression.php +++ b/src/Node/Expression/GetAttrExpression.php @@ -57,7 +57,7 @@ public function compile(Compiler $compiler): void return; } - $compiler->raw('twig_get_attribute($this->env, $this->source, '); + $compiler->raw('\Twig\Extension\Core\Functions\twig_get_attribute($this->env, $this->source, '); if ($this->getAttribute('ignore_strict_check')) { $this->getNode('node')->setAttribute('ignore_strict_check', true); diff --git a/src/Node/Expression/MethodCallExpression.php b/src/Node/Expression/MethodCallExpression.php index d5ec0b6efcb..4fa988f5ff5 100644 --- a/src/Node/Expression/MethodCallExpression.php +++ b/src/Node/Expression/MethodCallExpression.php @@ -39,7 +39,7 @@ public function compile(Compiler $compiler): void } $compiler - ->raw('twig_call_macro($macros[') + ->raw('\Twig\Extension\Core\Functions\twig_call_macro($macros[') ->repr($this->getNode('node')->getAttribute('name')) ->raw('], ') ->repr($this->getAttribute('method')) diff --git a/src/Node/ForNode.php b/src/Node/ForNode.php index 04addfbfe58..1dba91369e9 100644 --- a/src/Node/ForNode.php +++ b/src/Node/ForNode.php @@ -42,7 +42,7 @@ public function compile(Compiler $compiler): void $compiler ->addDebugInfo($this) ->write("\$context['_parent'] = \$context;\n") - ->write("\$context['_seq'] = twig_ensure_traversable(") + ->write("\$context['_seq'] = \Twig\Extension\Core\Functions\\twig_ensure_traversable(") ->subcompile($this->getNode('seq')) ->raw(");\n") ; diff --git a/src/Node/IncludeNode.php b/src/Node/IncludeNode.php index d540d6b23bf..ff24dba90eb 100644 --- a/src/Node/IncludeNode.php +++ b/src/Node/IncludeNode.php @@ -93,12 +93,12 @@ protected function addTemplateArguments(Compiler $compiler) $compiler->raw(false === $this->getAttribute('only') ? '$context' : '[]'); } elseif (false === $this->getAttribute('only')) { $compiler - ->raw('twig_array_merge($context, ') + ->raw('\Twig\Extension\Core\Functions\twig_array_merge($context, ') ->subcompile($this->getNode('variables')) ->raw(')') ; } else { - $compiler->raw('twig_to_array('); + $compiler->raw('\Twig\Extension\Core\Functions\twig_to_array('); $compiler->subcompile($this->getNode('variables')); $compiler->raw(')'); } diff --git a/src/Node/WithNode.php b/src/Node/WithNode.php index 56a334496e9..a977939deb8 100644 --- a/src/Node/WithNode.php +++ b/src/Node/WithNode.php @@ -45,14 +45,14 @@ public function compile(Compiler $compiler): void ->write(sprintf('$%s = ', $varsName)) ->subcompile($node) ->raw(";\n") - ->write(sprintf("if (!twig_test_iterable(\$%s)) {\n", $varsName)) + ->write(sprintf("if (!\Twig\Extension\Core\Functions\\twig_test_iterable(\$%s)) {\n", $varsName)) ->indent() ->write("throw new RuntimeError('Variables passed to the \"with\" tag must be a hash.', ") ->repr($node->getTemplateLine()) ->raw(", \$this->getSourceContext());\n") ->outdent() ->write("}\n") - ->write(sprintf("\$%s = twig_to_array(\$%s);\n", $varsName, $varsName)) + ->write(sprintf("\$%s = \Twig\Extension\Core\Functions\\twig_to_array(\$%s);\n", $varsName, $varsName)) ; if ($this->getAttribute('only')) { diff --git a/src/Test/NodeTestCase.php b/src/Test/NodeTestCase.php index 3b8b2c86c67..4f155c3a8e8 100644 --- a/src/Test/NodeTestCase.php +++ b/src/Test/NodeTestCase.php @@ -60,6 +60,6 @@ protected function getVariableGetter($name, $line = false) protected function getAttributeGetter() { - return 'twig_get_attribute($this->env, $this->source, '; + return '\Twig\Extension\Core\Functions\twig_get_attribute($this->env, $this->source, '; } } diff --git a/tests/Extension/CoreTest.php b/tests/Extension/CoreTest.php index 29a799b8103..ec6ced7ab6e 100644 --- a/tests/Extension/CoreTest.php +++ b/tests/Extension/CoreTest.php @@ -16,6 +16,15 @@ use Twig\Error\RuntimeError; use Twig\Loader\LoaderInterface; +use function Twig\Extension\Core\Functions\twig_compare; +use function Twig\Extension\Core\Functions\twig_first; +use function Twig\Extension\Core\Functions\twig_get_array_keys_filter; +use function Twig\Extension\Core\Functions\twig_in_filter; +use function Twig\Extension\Core\Functions\twig_last; +use function Twig\Extension\Core\Functions\twig_random; +use function Twig\Extension\Core\Functions\twig_reverse_filter; +use function Twig\Extension\Core\Functions\twig_slice; + class CoreTest extends TestCase { /** diff --git a/tests/Extension/EscaperTest.php b/tests/Extension/EscaperTest.php index 9804feaa5c7..bffe31b9959 100644 --- a/tests/Extension/EscaperTest.php +++ b/tests/Extension/EscaperTest.php @@ -17,6 +17,8 @@ use Twig\Extension\EscaperExtension; use Twig\Loader\LoaderInterface; +use function Twig\Extension\Escaper\Functions\twig_escape_filter; + class Twig_Tests_Extension_EscaperTest extends TestCase { /** diff --git a/tests/Extension/StringLoaderExtensionTest.php b/tests/Extension/StringLoaderExtensionTest.php index 363a0825ecb..1e9f64bf0cc 100644 --- a/tests/Extension/StringLoaderExtensionTest.php +++ b/tests/Extension/StringLoaderExtensionTest.php @@ -15,6 +15,9 @@ use Twig\Environment; use Twig\Extension\StringLoaderExtension; +use function Twig\Extension\Core\Functions\twig_include; +use function Twig\Extension\StringLoader\Functions\twig_template_from_string; + class StringLoaderExtensionTest extends TestCase { public function testIncludeWithTemplateStringAndNoSandbox() diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index 893bda345e8..8874b71309d 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -26,6 +26,8 @@ use Twig\TwigFunction; use Twig\TwigTest; +use function Twig\Extension\Escaper\Functions\twig_escape_filter; + // This function is defined to check that escaping strategies // like html works even if a function with the same name is defined. function html() diff --git a/tests/TemplateTest.php b/tests/TemplateTest.php index bd26e9d95b7..d02b3cff4ad 100644 --- a/tests/TemplateTest.php +++ b/tests/TemplateTest.php @@ -22,6 +22,8 @@ use Twig\Source; use Twig\Template; +use function Twig\Extension\Core\Functions\twig_get_attribute; + class TemplateTest extends TestCase { public function testDisplayBlocksAcceptTemplateOnlyAsBlocks() From 124d9c93494f3a9db45c0a40d5e4de16232c3cc5 Mon Sep 17 00:00:00 2001 From: Alexander Bolshakov Date: Tue, 6 Dec 2022 18:05:48 +0300 Subject: [PATCH 2/4] Moved global functions under namespaces. --- extra/intl-extra/IntlExtension.php | 4 +++- src/Extension/EscaperExtension.php | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/extra/intl-extra/IntlExtension.php b/extra/intl-extra/IntlExtension.php index 1fce0c7888d..9e857b14d8a 100644 --- a/extra/intl-extra/IntlExtension.php +++ b/extra/intl-extra/IntlExtension.php @@ -23,6 +23,8 @@ use Twig\TwigFilter; use Twig\TwigFunction; +use function Twig\Extension\Core\Functions\twig_date_converter; + final class IntlExtension extends AbstractExtension { private const DATE_FORMATS = [ @@ -279,7 +281,7 @@ public function formatNumberStyle(string $style, $number, array $attrs = [], str */ public function formatDateTime(Environment $env, $date, ?string $dateFormat = 'medium', ?string $timeFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', string $locale = null): string { - $date = \twig_date_converter($env, $date, $timezone); + $date = twig_date_converter($env, $date, $timezone); $formatter = $this->createDateFormatter($locale, $dateFormat, $timeFormat, $pattern, $date->getTimezone(), $calendar); if (false === $ret = $formatter->format($date)) { diff --git a/src/Extension/EscaperExtension.php b/src/Extension/EscaperExtension.php index 47eb4339901..8653443209f 100644 --- a/src/Extension/EscaperExtension.php +++ b/src/Extension/EscaperExtension.php @@ -143,7 +143,9 @@ public function addSafeClass(string $class, array $strategies) use Twig\Node\Expression\ConstantExpression; use Twig\Node\Node; -/** + use function Twig\Extension\Core\Functions\twig_convert_encoding; + + /** * Marks a variable as being safe. * * @param string $string A PHP variable From d2fd5aeaa65ea8fb382101e79144998d46c41c37 Mon Sep 17 00:00:00 2001 From: Felipe Marcos Date: Fri, 3 Oct 2025 16:47:50 -0300 Subject: [PATCH 3/4] fix: deprecated params --- src/Environment.php | 6 +++--- src/Loader/FilesystemLoader.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Environment.php b/src/Environment.php index 12db4bb2375..15e4d8b4fee 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -259,7 +259,7 @@ public function setCache($cache) * * @internal */ - public function getTemplateClass(string $name, int $index = null): string + public function getTemplateClass(string $name, ?int $index = null): string { $key = $this->getLoader()->getCacheKey($name).$this->optionsHash; @@ -327,7 +327,7 @@ public function load($name): TemplateWrapper * * @internal */ - public function loadTemplate(string $cls, string $name, int $index = null): Template + public function loadTemplate(string $cls, string $name, ?int $index = null): Template { $mainCls = $cls; if (null !== $index) { @@ -383,7 +383,7 @@ public function loadTemplate(string $cls, string $name, int $index = null): Temp * @throws LoaderError When the template cannot be found * @throws SyntaxError When an error occurred during compilation */ - public function createTemplate(string $template, string $name = null): TemplateWrapper + public function createTemplate(string $template, ?string $name = null): TemplateWrapper { $hash = hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', $template, false); if (null !== $name) { diff --git a/src/Loader/FilesystemLoader.php b/src/Loader/FilesystemLoader.php index 62267a11c89..a100f37ea44 100644 --- a/src/Loader/FilesystemLoader.php +++ b/src/Loader/FilesystemLoader.php @@ -34,7 +34,7 @@ class FilesystemLoader implements LoaderInterface * @param string|array $paths A path or an array of paths where to look for templates * @param string|null $rootPath The root path common to all relative paths (null for getcwd()) */ - public function __construct($paths = [], string $rootPath = null) + public function __construct($paths = [], ?string $rootPath = null) { $this->rootPath = (null === $rootPath ? getcwd() : $rootPath).\DIRECTORY_SEPARATOR; if (null !== $rootPath && false !== ($realPath = realpath($rootPath))) { From abc5960f36852160283d7d86201ebb813f08eb69 Mon Sep 17 00:00:00 2001 From: Felipe Marcos Date: Fri, 6 Mar 2026 13:49:15 -0300 Subject: [PATCH 4/4] fix: deprecated nullable param --- src/Error/Error.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Error/Error.php b/src/Error/Error.php index a68be65f203..d4b733ab834 100644 --- a/src/Error/Error.php +++ b/src/Error/Error.php @@ -53,7 +53,7 @@ class Error extends \Exception * @param int $lineno The template line where the error occurred * @param Source|null $source The source context where the error occurred */ - public function __construct(string $message, int $lineno = -1, Source $source = null, \Exception $previous = null) + public function __construct(string $message, int $lineno = -1, ?Source $source = null, ?\Exception $previous = null) { parent::__construct('', 0, $previous);