Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate not passing a Source to TokenStream #4471

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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\TokenStream`` 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should change the property to a non-promoted one, so that the property type can be not-nullable

Copy link
Contributor Author

@fabpot fabpot Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do this in 4.0. As it's private, that doesn't change anything anyway, right (or maybe for static analyzers)?

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
Loading