Skip to content

Commit

Permalink
Deprecate Token::getType()
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jan 24, 2025
1 parent c542deb commit ade8bce
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 25 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions doc/deprecated.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------

Expand Down
30 changes: 15 additions & 15 deletions src/ExpressionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ private function isBinary(Token $token): bool
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':
Expand Down Expand Up @@ -327,25 +327,25 @@ public function parsePrimaryExpression()
}
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();
Expand All @@ -359,7 +359,7 @@ public function parsePrimaryExpression()

// 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);
Expand Down Expand Up @@ -491,7 +491,7 @@ public function parseMappingExpression()
} 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 (:)');
Expand All @@ -508,7 +508,7 @@ public function parsePostfixExpression($node)
{
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()) {
Expand Down Expand Up @@ -944,13 +944,13 @@ private function parseSubscriptExpressionDot(Node $node): AbstractExpression
} 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());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/NodeVisitor/AbstractNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @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
{
Expand Down
10 changes: 5 additions & 5 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,24 @@ public function subparse($test, bool $dropNeedle = false): Node
$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());
}

Expand Down
12 changes: 11 additions & 1 deletion src/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(

public function __toString(): string
{
return \sprintf('%s(%s)', self::typeToString($this->type, true), $this->value);
return \sprintf('%s(%s)', $this->toEnglish(), $this->value);
}

/**
Expand Down Expand Up @@ -75,8 +75,13 @@ public function getLine(): int
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;
}

Expand All @@ -88,6 +93,11 @@ public function getValue()
return $this->value;
}

public function toEnglish(): string
{
return self::typeToEnglish($this->type);
}

public static function typeToString(int $type, bool $short = false): string
{
switch ($type) {
Expand Down
4 changes: 2 additions & 2 deletions src/TokenStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function expect($type, $value = null, ?string $message = null): Token
$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,
Expand Down Expand Up @@ -116,7 +116,7 @@ public function test($primary, $secondary = null): bool
*/
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
Expand Down
2 changes: 1 addition & 1 deletion tests/LexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected function countToken($template, $type, $value = null)
$count = 0;
while (!$stream->isEOF()) {
$token = $stream->next();
if ($type === $token->getType()) {
if ($token->test($type)) {
if (null === $value || $value === $token->getValue()) {
++$count;
}
Expand Down

0 comments on commit ade8bce

Please sign in to comment.