Skip to content

Commit

Permalink
Deprecate not passing a Source to TokenStream
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Nov 23, 2024
1 parent 74aff6b commit 7c789b3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 3.16.0 (2024-XX-XX)

* n/a
* Deprecate not passing a `Source` instance to `TokenStream`

# 3.15.0 (2024-11-17)

Expand Down
6 changes: 6 additions & 0 deletions doc/deprecated.rst
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ Parser
* Passing ``null`` to ``Twig\Parser::setParent()`` is deprecated as of Twig
3.12.

Lexer
-----

* Not passing a ``Source`` instance to ``Twig\Lexer`` constructor is deprecated
as of Twig 3.16.

Templates
---------

Expand Down
11 changes: 5 additions & 6 deletions src/TokenStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public function __construct(
private array $tokens,
private ?Source $source = null,
) {
$this->source = $source ?: new Source('', '');
if (null === $this->source) {
trigger_deprecation('twig/twig', '3.16', \sprintf('Not passing a "%s" object to "%s" constructor is deprecated.', Source::class, __CLASS__));

$this->source = new Source('', '');
}
}

public function __toString()
Expand Down Expand Up @@ -117,11 +121,6 @@ public function getCurrent(): Token
return $this->tokens[$this->current];
}

/**
* Gets the source associated with this stream.
*
* @internal
*/
public function getSourceContext(): Source
{
return $this->source;
Expand Down
10 changes: 5 additions & 5 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testUnknownTag()
new Token(Token::NAME_TYPE, 'foo', 1),
new Token(Token::BLOCK_END_TYPE, '', 1),
new Token(Token::EOF_TYPE, '', 1),
]);
], new Source('', ''));
$parser = new Parser(new Environment(new ArrayLoader()));

$this->expectException(SyntaxError::class);
Expand All @@ -52,7 +52,7 @@ public function testUnknownTagWithoutSuggestions()
new Token(Token::NAME_TYPE, 'foobar', 1),
new Token(Token::BLOCK_END_TYPE, '', 1),
new Token(Token::EOF_TYPE, '', 1),
]);
], new Source('', ''));
$parser = new Parser(new Environment(new ArrayLoader()));

$this->expectException(SyntaxError::class);
Expand Down Expand Up @@ -153,7 +153,7 @@ public function testParseIsReentrant()
new Token(Token::NAME_TYPE, 'foo', 1),
new Token(Token::VAR_END_TYPE, '', 1),
new Token(Token::EOF_TYPE, '', 1),
]));
], new Source('', '')));

$p = new \ReflectionProperty($parser, 'parent');
$p->setAccessible(true);
Expand Down Expand Up @@ -208,7 +208,7 @@ protected function getParser()

$p = new \ReflectionProperty($parser, 'stream');
$p->setAccessible(true);
$p->setValue($parser, new TokenStream([]));
$p->setValue($parser, new TokenStream([], new Source('', '')));

return $parser;
}
Expand All @@ -225,7 +225,7 @@ public function parse(Token $token): Node
new Token(Token::STRING_TYPE, 'base', 1),
new Token(Token::BLOCK_END_TYPE, '', 1),
new Token(Token::EOF_TYPE, '', 1),
]));
], new Source('', '')));

$this->parser->getStream()->expect(Token::BLOCK_END_TYPE);

Expand Down
7 changes: 4 additions & 3 deletions tests/TokenStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Twig\Error\SyntaxError;
use Twig\Source;
use Twig\Token;
use Twig\TokenStream;

Expand All @@ -36,7 +37,7 @@ protected function setUp(): void

public function testNext()
{
$stream = new TokenStream(self::$tokens);
$stream = new TokenStream(self::$tokens, new Source('', ''));
$repr = [];
while (!$stream->isEOF()) {
$token = $stream->next();
Expand All @@ -50,7 +51,7 @@ public function testEndOfTemplateNext()
{
$stream = new TokenStream([
new Token(Token::BLOCK_START_TYPE, 1, 1),
]);
], new Source('', ''));

$this->expectException(SyntaxError::class);
$this->expectExceptionMessage('Unexpected end of template');
Expand All @@ -64,7 +65,7 @@ public function testEndOfTemplateLook()
{
$stream = new TokenStream([
new Token(Token::BLOCK_START_TYPE, 1, 1),
]);
], new Source('', ''));

$this->expectException(SyntaxError::class);
$this->expectExceptionMessage('Unexpected end of template');
Expand Down

0 comments on commit 7c789b3

Please sign in to comment.