Deprecate Token::getType()

This commit is contained in:
Fabien Potencier
2025-01-20 10:25:07 +01:00
parent c542debe58
commit ade8bce516
8 changed files with 40 additions and 25 deletions
+2
View File
@@ -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
+3
View File
@@ -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
---------
+15 -15
View File
@@ -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());
}
}
+1 -1
View File
@@ -19,7 +19,7 @@ use Twig\Node\Node;
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @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
{
+5 -5
View File
@@ -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());
}
+11 -1
View File
@@ -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) {
+2 -2
View File
@@ -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
+1 -1
View File
@@ -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;
}