diff --git a/CHANGELOG b/CHANGELOG index 96db77d07..5eda0f3ec 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ # 3.19.0 (2025-XX-XX) + * Deprecate `Token::getType()`, use `Token::test()` instead + * Add `Token::toEnglish()` * Add `ForElseNode` * Deprecate `Twig\ExpressionParser::parseOnlyArguments()` and `Twig\ExpressionParser::parseArguments()` (use diff --git a/doc/deprecated.rst b/doc/deprecated.rst index a8a04ab85..b3397d8dd 100644 --- a/doc/deprecated.rst +++ b/doc/deprecated.rst @@ -236,6 +236,9 @@ Lexer * Not passing a ``Source`` instance to ``Twig\TokenStream`` constructor is deprecated as of Twig 3.16. +* The ``Token::getType()`` method is deprecated as of Twig 3.19, use + ``Token::test()`` instead. + Templates --------- diff --git a/src/ExpressionParser.php b/src/ExpressionParser.php index 2e409c9e4..49aa2295e 100644 --- a/src/ExpressionParser.php +++ b/src/ExpressionParser.php @@ -297,8 +297,8 @@ class ExpressionParser public function parsePrimaryExpression() { $token = $this->parser->getCurrentToken(); - switch ($token->getType()) { - case Token::NAME_TYPE: + switch (true) { + case $token->test(Token::NAME_TYPE): $this->parser->getStream()->next(); switch ($token->getValue()) { case 'true': @@ -327,25 +327,25 @@ class ExpressionParser } break; - case Token::NUMBER_TYPE: + case $token->test(Token::NUMBER_TYPE): $this->parser->getStream()->next(); $node = new ConstantExpression($token->getValue(), $token->getLine()); break; - case Token::STRING_TYPE: - case Token::INTERPOLATION_START_TYPE: + case $token->test(Token::STRING_TYPE): + case $token->test(Token::INTERPOLATION_START_TYPE) : $node = $this->parseStringExpression(); break; - case Token::PUNCTUATION_TYPE: + case $token->test(Token::PUNCTUATION_TYPE): $node = match ($token->getValue()) { '[' => $this->parseSequenceExpression(), '{' => $this->parseMappingExpression(), - default => throw new SyntaxError(\sprintf('Unexpected token "%s" of value "%s".', Token::typeToEnglish($token->getType()), $token->getValue()), $token->getLine(), $this->parser->getStream()->getSourceContext()), + default => throw new SyntaxError(\sprintf('Unexpected token "%s" of value "%s".', $token->toEnglish(), $token->getValue()), $token->getLine(), $this->parser->getStream()->getSourceContext()), }; break; - case Token::OPERATOR_TYPE: + case $token->test(Token::OPERATOR_TYPE): if (preg_match(Lexer::REGEX_NAME, $token->getValue(), $matches) && $matches[0] == $token->getValue()) { // in this context, string operators are variable names $this->parser->getStream()->next(); @@ -359,7 +359,7 @@ class ExpressionParser // no break default: - throw new SyntaxError(\sprintf('Unexpected token "%s" of value "%s".', Token::typeToEnglish($token->getType()), $token->getValue()), $token->getLine(), $this->parser->getStream()->getSourceContext()); + throw new SyntaxError(\sprintf('Unexpected token "%s" of value "%s".', $token->toEnglish(), $token->getValue()), $token->getLine(), $this->parser->getStream()->getSourceContext()); } return $this->parsePostfixExpression($node); @@ -491,7 +491,7 @@ class ExpressionParser } else { $current = $stream->getCurrent(); - throw new SyntaxError(\sprintf('A mapping key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "%s" of value "%s".', Token::typeToEnglish($current->getType()), $current->getValue()), $current->getLine(), $stream->getSourceContext()); + throw new SyntaxError(\sprintf('A mapping key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "%s" of value "%s".', $current->toEnglish(), $current->getValue()), $current->getLine(), $stream->getSourceContext()); } $stream->expect(Token::PUNCTUATION_TYPE, ':', 'A mapping key must be followed by a colon (:)'); @@ -508,7 +508,7 @@ class ExpressionParser { while (true) { $token = $this->parser->getCurrentToken(); - if (Token::PUNCTUATION_TYPE == $token->getType()) { + if ($token->test(Token::PUNCTUATION_TYPE)) { if ('.' == $token->getValue() || '[' == $token->getValue()) { $node = $this->parseSubscriptExpression($node); } elseif ('|' == $token->getValue()) { @@ -944,13 +944,13 @@ class ExpressionParser } else { $token = $stream->next(); if ( - Token::NAME_TYPE == $token->getType() - || Token::NUMBER_TYPE == $token->getType() - || (Token::OPERATOR_TYPE == $token->getType() && preg_match(Lexer::REGEX_NAME, $token->getValue())) + $token->test(Token::NAME_TYPE) + || $token->test(Token::NUMBER_TYPE) + || ($token->test(Token::OPERATOR_TYPE) && preg_match(Lexer::REGEX_NAME, $token->getValue())) ) { $attribute = new ConstantExpression($token->getValue(), $token->getLine()); } else { - throw new SyntaxError(\sprintf('Expected name or number, got value "%s" of type %s.', $token->getValue(), Token::typeToEnglish($token->getType())), $token->getLine(), $stream->getSourceContext()); + throw new SyntaxError(\sprintf('Expected name or number, got value "%s" of type %s.', $token->getValue(), $token->toEnglish()), $token->getLine(), $stream->getSourceContext()); } } diff --git a/src/NodeVisitor/AbstractNodeVisitor.php b/src/NodeVisitor/AbstractNodeVisitor.php index 5de35fd09..38b1ec9d0 100644 --- a/src/NodeVisitor/AbstractNodeVisitor.php +++ b/src/NodeVisitor/AbstractNodeVisitor.php @@ -19,7 +19,7 @@ use Twig\Node\Node; * * @author Fabien Potencier * - * @deprecated since 3.9 (to be removed in 4.0) + * @deprecated since Twig 3.9 (to be removed in 4.0) */ abstract class AbstractNodeVisitor implements NodeVisitorInterface { diff --git a/src/Parser.php b/src/Parser.php index 9a8f97e2e..ff1772c16 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -147,24 +147,24 @@ class Parser $lineno = $this->getCurrentToken()->getLine(); $rv = []; while (!$this->stream->isEOF()) { - switch ($this->getCurrentToken()->getType()) { - case Token::TEXT_TYPE: + switch (true) { + case $this->stream->getCurrent()->test(Token::TEXT_TYPE): $token = $this->stream->next(); $rv[] = new TextNode($token->getValue(), $token->getLine()); break; - case Token::VAR_START_TYPE: + case $this->stream->getCurrent()->test(Token::VAR_START_TYPE): $token = $this->stream->next(); $expr = $this->expressionParser->parseExpression(); $this->stream->expect(Token::VAR_END_TYPE); $rv[] = new PrintNode($expr, $token->getLine()); break; - case Token::BLOCK_START_TYPE: + case $this->stream->getCurrent()->test(Token::BLOCK_START_TYPE): $this->stream->next(); $token = $this->getCurrentToken(); - if (Token::NAME_TYPE !== $token->getType()) { + if (!$token->test(Token::NAME_TYPE)) { throw new SyntaxError('A block must start with a tag name.', $token->getLine(), $this->stream->getSourceContext()); } diff --git a/src/Token.php b/src/Token.php index c3c656f06..e14c886c3 100644 --- a/src/Token.php +++ b/src/Token.php @@ -42,7 +42,7 @@ final class Token public function __toString(): string { - return \sprintf('%s(%s)', self::typeToString($this->type, true), $this->value); + return \sprintf('%s(%s)', $this->toEnglish(), $this->value); } /** @@ -75,8 +75,13 @@ final class Token return $this->lineno; } + /** + * @deprecated since Twig 3.19 + */ public function getType(): int { + trigger_deprecation('twig/twig', '3.19', sprintf('The "%s" method is deprecated.', __METHOD__)); + return $this->type; } @@ -88,6 +93,11 @@ final class Token return $this->value; } + public function toEnglish(): string + { + return self::typeToEnglish($this->type); + } + public static function typeToString(int $type, bool $short = false): string { switch ($type) { diff --git a/src/TokenStream.php b/src/TokenStream.php index ec6ac9592..7ee7539f1 100644 --- a/src/TokenStream.php +++ b/src/TokenStream.php @@ -79,7 +79,7 @@ final class TokenStream $line = $token->getLine(); throw new SyntaxError(\sprintf('%sUnexpected token "%s"%s ("%s" expected%s).', $message ? $message.'. ' : '', - Token::typeToEnglish($token->getType()), + $token->toEnglish(), $token->getValue() ? \sprintf(' of value "%s"', $token->getValue()) : '', Token::typeToEnglish($type), $value ? \sprintf(' with value "%s"', $value) : ''), $line, @@ -116,7 +116,7 @@ final class TokenStream */ public function isEOF(): bool { - return Token::EOF_TYPE === $this->tokens[$this->current]->getType(); + return $this->tokens[$this->current]->test(Token::EOF_TYPE); } public function getCurrent(): Token diff --git a/tests/LexerTest.php b/tests/LexerTest.php index bfc7f9bb2..273e5b882 100644 --- a/tests/LexerTest.php +++ b/tests/LexerTest.php @@ -72,7 +72,7 @@ class LexerTest extends TestCase $count = 0; while (!$stream->isEOF()) { $token = $stream->next(); - if ($type === $token->getType()) { + if ($token->test($type)) { if (null === $value || $value === $token->getValue()) { ++$count; }