From 960f6762ed8d8b4210de5dc0769257b426e1c3c6 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 22 Jan 2025 20:59:41 +0100 Subject: [PATCH] Deprecate Twig\ExpressionParser::parseOnlyArguments() and Twig\ExpressionParser::parseArguments() --- CHANGELOG | 4 ++++ doc/deprecated.rst | 10 ++++++--- .../TokenParser/CacheTokenParser.php | 2 +- extra/cache-extra/composer.json | 2 +- src/ExpressionParser.php | 22 +++++++++++++++---- 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 2fafecb65..53f2d72e5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,9 @@ # 3.19.0 (2025-XX-XX) + * Deprecate `Twig\ExpressionParser::parseOnlyArguments()` and + `Twig\ExpressionParser::parseArguments()` (use + `Twig\ExpressionParser::parseNamedArguments()` instead) + * Fix `constant()` behavior when used with `??` * Add the `invoke` filter * Make `{}` optional for the `types` tag diff --git a/doc/deprecated.rst b/doc/deprecated.rst index c3d6c32ac..a8a04ab85 100644 --- a/doc/deprecated.rst +++ b/doc/deprecated.rst @@ -56,9 +56,6 @@ Nodes as of Twig 3.12 as the tag is now automatically set by the Parser when needed. -* Passing a second argument to "ExpressionParser::parseFilterExpressionRaw()" - is deprecated as of Twig 3.12. - * The following ``Twig\Node\Node`` methods will take a string or an integer (instead of just a string) in Twig 4.0 for their "name" argument: ``getNode()``, ``hasNode()``, ``setNode()``, ``removeNode()``, and @@ -213,6 +210,9 @@ Node Visitors Parser ------ +* Passing a second argument to ``ExpressionParser::parseFilterExpressionRaw()`` + is deprecated as of Twig 3.12. + * The following methods from ``Twig\Parser`` are deprecated as of Twig 3.12: ``getBlockStack()``, ``hasBlock()``, ``getBlock()``, ``hasMacro()``, ``hasTraits()``, ``getParent()``. @@ -226,6 +226,10 @@ Parser * Passing ``null`` to ``Twig\Parser::setParent()`` is deprecated as of Twig 3.12. +* The ``Twig\ExpressionParser::parseOnlyArguments()`` and + ``Twig\ExpressionParser::parseArguments()`` methods are deprecated, use + ``Twig\ExpressionParser::parseNamedArguments()`` instead. + Lexer ----- diff --git a/extra/cache-extra/TokenParser/CacheTokenParser.php b/extra/cache-extra/TokenParser/CacheTokenParser.php index a5ea4840c..b5fa3b87a 100644 --- a/extra/cache-extra/TokenParser/CacheTokenParser.php +++ b/extra/cache-extra/TokenParser/CacheTokenParser.php @@ -32,7 +32,7 @@ class CacheTokenParser extends AbstractTokenParser while ($stream->test(Token::NAME_TYPE)) { $k = $stream->getCurrent()->getValue(); $stream->next(); - $args = $expressionParser->parseArguments(); + $args = $expressionParser->parseNamedArguments(); switch ($k) { case 'ttl': diff --git a/extra/cache-extra/composer.json b/extra/cache-extra/composer.json index a36504995..8b704f34e 100644 --- a/extra/cache-extra/composer.json +++ b/extra/cache-extra/composer.json @@ -17,7 +17,7 @@ "require": { "php": ">=8.0.2", "symfony/cache": "^5.4|^6.4|^7.0", - "twig/twig": "^3.13|^4.0" + "twig/twig": "^3.19|^4.0" }, "require-dev": { "symfony/phpunit-bridge": "^6.4|^7.0" diff --git a/src/ExpressionParser.php b/src/ExpressionParser.php index 0f1b0ed36..f57edf37f 100644 --- a/src/ExpressionParser.php +++ b/src/ExpressionParser.php @@ -530,7 +530,7 @@ class ExpressionParser return new MacroReferenceExpression($alias['node']->getNode('var'), $alias['name'], $this->createArguments($line), $line); } - $args = $this->parseOnlyArguments(); + $args = $this->parseNamedArguments(); $function = $this->getFunction($name, $line); if ($function->getParserCallable()) { @@ -579,7 +579,7 @@ class ExpressionParser if (!$this->parser->getStream()->test(Token::PUNCTUATION_TYPE, '(')) { $arguments = new EmptyNode(); } else { - $arguments = $this->parseOnlyArguments(); + $arguments = $this->parseNamedArguments(); } $filter = $this->getFilter($token->getValue(), $token->getLine()); @@ -611,9 +611,13 @@ class ExpressionParser * @return Node * * @throws SyntaxError + * + * @deprecated since Twig 3.19 Use parseNamedArguments() instead */ public function parseArguments() { + trigger_deprecation('twig/twig', '3.19', \sprintf('The "%s()" method is deprecated, use "%s::parseNamedArguments()" instead.', __METHOD__, __CLASS__)); + $namedArguments = false; $definition = false; if (\func_num_args() > 1) { @@ -738,7 +742,7 @@ class ExpressionParser $arguments = null; if ($stream->test(Token::PUNCTUATION_TYPE, '(')) { - $arguments = $this->parseOnlyArguments(); + $arguments = $this->parseNamedArguments(); } elseif ($test->hasOneMandatoryArgument()) { $arguments = new Nodes([0 => $this->getPrimary()]); } @@ -864,14 +868,24 @@ class ExpressionParser private function createArguments(int $line): ArrayExpression { $arguments = new ArrayExpression([], $line); - foreach ($this->parseOnlyArguments() as $k => $n) { + foreach ($this->parseNamedArguments() as $k => $n) { $arguments->addElement($n, new LocalVariable($k, $line)); } return $arguments; } + /** + * @deprecated since Twig 3.19 Use parseNamedArguments() instead + */ public function parseOnlyArguments() + { + trigger_deprecation('twig/twig', '3.19', \sprintf('The "%s()" method is deprecated, use "%s::parseNamedArguments()" instead.', __METHOD__, __CLASS__)); + + return $this->parseNamedArguments(); + } + + public function parseNamedArguments(): Nodes { $args = []; $stream = $this->parser->getStream();