Skip to content

Commit c2957af

Browse files
committed
Deprecate Token::getType()
1 parent c542deb commit c2957af

File tree

7 files changed

+38
-23
lines changed

7 files changed

+38
-23
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# 3.19.0 (2025-XX-XX)
22

3+
* Deprecate `Token::getType()`, use `Token::test()` instead
4+
* Add `Token::toEnglish()`
35
* Add `ForElseNode`
46
* Deprecate `Twig\ExpressionParser::parseOnlyArguments()` and
57
`Twig\ExpressionParser::parseArguments()` (use

doc/deprecated.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ Lexer
236236
* Not passing a ``Source`` instance to ``Twig\TokenStream`` constructor is
237237
deprecated as of Twig 3.16.
238238

239+
* The ``Token::getType()`` method is deprecated as of Twig 3.19, use
240+
``Token::test()`` instead.
241+
239242
Templates
240243
---------
241244

src/ExpressionParser.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,8 @@ private function isBinary(Token $token): bool
297297
public function parsePrimaryExpression()
298298
{
299299
$token = $this->parser->getCurrentToken();
300-
switch ($token->getType()) {
301-
case Token::NAME_TYPE:
300+
switch (true) {
301+
case $token->test(Token::NAME_TYPE):
302302
$this->parser->getStream()->next();
303303
switch ($token->getValue()) {
304304
case 'true':
@@ -327,25 +327,25 @@ public function parsePrimaryExpression()
327327
}
328328
break;
329329

330-
case Token::NUMBER_TYPE:
330+
case $token->test(Token::NUMBER_TYPE):
331331
$this->parser->getStream()->next();
332332
$node = new ConstantExpression($token->getValue(), $token->getLine());
333333
break;
334334

335-
case Token::STRING_TYPE:
336-
case Token::INTERPOLATION_START_TYPE:
335+
case $token->test(Token::STRING_TYPE):
336+
case $token->test(Token::INTERPOLATION_START_TYPE) :
337337
$node = $this->parseStringExpression();
338338
break;
339339

340-
case Token::PUNCTUATION_TYPE:
340+
case $token->test(Token::PUNCTUATION_TYPE):
341341
$node = match ($token->getValue()) {
342342
'[' => $this->parseSequenceExpression(),
343343
'{' => $this->parseMappingExpression(),
344-
default => throw new SyntaxError(\sprintf('Unexpected token "%s" of value "%s".', Token::typeToEnglish($token->getType()), $token->getValue()), $token->getLine(), $this->parser->getStream()->getSourceContext()),
344+
default => throw new SyntaxError(\sprintf('Unexpected token "%s" of value "%s".', $token->toEnglish(), $token->getValue()), $token->getLine(), $this->parser->getStream()->getSourceContext()),
345345
};
346346
break;
347347

348-
case Token::OPERATOR_TYPE:
348+
case $token->test(Token::OPERATOR_TYPE):
349349
if (preg_match(Lexer::REGEX_NAME, $token->getValue(), $matches) && $matches[0] == $token->getValue()) {
350350
// in this context, string operators are variable names
351351
$this->parser->getStream()->next();
@@ -359,7 +359,7 @@ public function parsePrimaryExpression()
359359

360360
// no break
361361
default:
362-
throw new SyntaxError(\sprintf('Unexpected token "%s" of value "%s".', Token::typeToEnglish($token->getType()), $token->getValue()), $token->getLine(), $this->parser->getStream()->getSourceContext());
362+
throw new SyntaxError(\sprintf('Unexpected token "%s" of value "%s".', $token->toEnglish(), $token->getValue()), $token->getLine(), $this->parser->getStream()->getSourceContext());
363363
}
364364

365365
return $this->parsePostfixExpression($node);
@@ -491,7 +491,7 @@ public function parseMappingExpression()
491491
} else {
492492
$current = $stream->getCurrent();
493493

494-
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());
494+
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());
495495
}
496496

497497
$stream->expect(Token::PUNCTUATION_TYPE, ':', 'A mapping key must be followed by a colon (:)');
@@ -508,7 +508,7 @@ public function parsePostfixExpression($node)
508508
{
509509
while (true) {
510510
$token = $this->parser->getCurrentToken();
511-
if (Token::PUNCTUATION_TYPE == $token->getType()) {
511+
if ($token->test(Token::PUNCTUATION_TYPE)) {
512512
if ('.' == $token->getValue() || '[' == $token->getValue()) {
513513
$node = $this->parseSubscriptExpression($node);
514514
} elseif ('|' == $token->getValue()) {
@@ -944,13 +944,13 @@ private function parseSubscriptExpressionDot(Node $node): AbstractExpression
944944
} else {
945945
$token = $stream->next();
946946
if (
947-
Token::NAME_TYPE == $token->getType()
948-
|| Token::NUMBER_TYPE == $token->getType()
949-
|| (Token::OPERATOR_TYPE == $token->getType() && preg_match(Lexer::REGEX_NAME, $token->getValue()))
947+
$token->test(Token::NAME_TYPE)
948+
|| $token->test(Token::NUMBER_TYPE)
949+
|| ($token->test(Token::OPERATOR_TYPE) && preg_match(Lexer::REGEX_NAME, $token->getValue()))
950950
) {
951951
$attribute = new ConstantExpression($token->getValue(), $token->getLine());
952952
} else {
953-
throw new SyntaxError(\sprintf('Expected name or number, got value "%s" of type %s.', $token->getValue(), Token::typeToEnglish($token->getType())), $token->getLine(), $stream->getSourceContext());
953+
throw new SyntaxError(\sprintf('Expected name or number, got value "%s" of type %s.', $token->getValue(), $token->toEnglish()), $token->getLine(), $stream->getSourceContext());
954954
}
955955
}
956956

src/Parser.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,24 +147,24 @@ public function subparse($test, bool $dropNeedle = false): Node
147147
$lineno = $this->getCurrentToken()->getLine();
148148
$rv = [];
149149
while (!$this->stream->isEOF()) {
150-
switch ($this->getCurrentToken()->getType()) {
151-
case Token::TEXT_TYPE:
150+
switch (true) {
151+
case $this->stream->getCurrent()->test(Token::TEXT_TYPE):
152152
$token = $this->stream->next();
153153
$rv[] = new TextNode($token->getValue(), $token->getLine());
154154
break;
155155

156-
case Token::VAR_START_TYPE:
156+
case $this->stream->getCurrent()->test(Token::VAR_START_TYPE):
157157
$token = $this->stream->next();
158158
$expr = $this->expressionParser->parseExpression();
159159
$this->stream->expect(Token::VAR_END_TYPE);
160160
$rv[] = new PrintNode($expr, $token->getLine());
161161
break;
162162

163-
case Token::BLOCK_START_TYPE:
163+
case $this->stream->getCurrent()->test(Token::BLOCK_START_TYPE):
164164
$this->stream->next();
165165
$token = $this->getCurrentToken();
166166

167-
if (Token::NAME_TYPE !== $token->getType()) {
167+
if (!$token->test(Token::NAME_TYPE)) {
168168
throw new SyntaxError('A block must start with a tag name.', $token->getLine(), $this->stream->getSourceContext());
169169
}
170170

src/Token.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,13 @@ public function getLine(): int
7575
return $this->lineno;
7676
}
7777

78+
/**
79+
* @deprecated since 3.19
80+
*/
7881
public function getType(): int
7982
{
83+
trigger_deprecation('twig/twig', '3.19', sprintf('The "%s" method is deprecated.', __METHOD__));
84+
8085
return $this->type;
8186
}
8287

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

96+
public function toEnglish(): string
97+
{
98+
return self::typeToEnglish($this->type);
99+
}
100+
91101
public static function typeToString(int $type, bool $short = false): string
92102
{
93103
switch ($type) {

src/TokenStream.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function expect($type, $value = null, ?string $message = null): Token
7979
$line = $token->getLine();
8080
throw new SyntaxError(\sprintf('%sUnexpected token "%s"%s ("%s" expected%s).',
8181
$message ? $message.'. ' : '',
82-
Token::typeToEnglish($token->getType()),
82+
$token->toEnglish(),
8383
$token->getValue() ? \sprintf(' of value "%s"', $token->getValue()) : '',
8484
Token::typeToEnglish($type), $value ? \sprintf(' with value "%s"', $value) : ''),
8585
$line,
@@ -116,7 +116,7 @@ public function test($primary, $secondary = null): bool
116116
*/
117117
public function isEOF(): bool
118118
{
119-
return Token::EOF_TYPE === $this->tokens[$this->current]->getType();
119+
return $this->tokens[$this->current]->test(Token::EOF_TYPE);
120120
}
121121

122122
public function getCurrent(): Token

tests/LexerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected function countToken($template, $type, $value = null)
7272
$count = 0;
7373
while (!$stream->isEOF()) {
7474
$token = $stream->next();
75-
if ($type === $token->getType()) {
75+
if ($token->test($type)) {
7676
if (null === $value || $value === $token->getValue()) {
7777
++$count;
7878
}

0 commit comments

Comments
 (0)