Update Twig to v2.15.4

This commit is contained in:
slawkens
2023-02-02 10:37:45 +01:00
parent e552bcfe82
commit 130f7ba405
309 changed files with 3802 additions and 4005 deletions

View File

@@ -17,38 +17,18 @@ use Twig\Error\SyntaxError;
/**
* Represents a token stream.
*
* @final
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class TokenStream
final class TokenStream
{
protected $tokens;
protected $current = 0;
protected $filename;
private $tokens;
private $current = 0;
private $source;
/**
* @param array $tokens An array of tokens
* @param string|null $name The name of the template which tokens are associated with
* @param string|null $source The source code associated with the tokens
*/
public function __construct(array $tokens, $name = null, $source = null)
public function __construct(array $tokens, Source $source = null)
{
if (!$name instanceof Source) {
if (null !== $name || null !== $source) {
@trigger_error(sprintf('Passing a string as the $name argument of %s() is deprecated since version 1.27. Pass a \Twig\Source instance instead.', __METHOD__), E_USER_DEPRECATED);
}
$this->source = new Source($source, $name);
} else {
$this->source = $name;
}
$this->tokens = $tokens;
// deprecated, not used anymore, to be removed in 2.0
$this->filename = $this->source->getName();
$this->source = $source ?: new Source('', '');
}
public function __toString()
@@ -63,10 +43,8 @@ class TokenStream
/**
* Sets the pointer to the next token and returns the old one.
*
* @return Token
*/
public function next()
public function next(): Token
{
if (!isset($this->tokens[++$this->current])) {
throw new SyntaxError('Unexpected end of template.', $this->tokens[$this->current - 1]->getLine(), $this->source);
@@ -89,10 +67,8 @@ class TokenStream
/**
* Tests a token and returns it or throws a syntax error.
*
* @return Token
*/
public function expect($type, $value = null, $message = null)
public function expect($type, $value = null, string $message = null): Token
{
$token = $this->tokens[$this->current];
if (!$token->test($type, $value)) {
@@ -113,12 +89,8 @@ class TokenStream
/**
* Looks at the next token.
*
* @param int $number
*
* @return Token
*/
public function look($number = 1)
public function look(int $number = 1): Token
{
if (!isset($this->tokens[$this->current + $number])) {
throw new SyntaxError('Unexpected end of template.', $this->tokens[$this->current + $number - 1]->getLine(), $this->source);
@@ -129,70 +101,31 @@ class TokenStream
/**
* Tests the current token.
*
* @return bool
*/
public function test($primary, $secondary = null)
public function test($primary, $secondary = null): bool
{
return $this->tokens[$this->current]->test($primary, $secondary);
}
/**
* Checks if end of stream was reached.
*
* @return bool
*/
public function isEOF()
public function isEOF(): bool
{
return Token::EOF_TYPE === $this->tokens[$this->current]->getType();
return /* Token::EOF_TYPE */ -1 === $this->tokens[$this->current]->getType();
}
/**
* @return Token
*/
public function getCurrent()
public function getCurrent(): Token
{
return $this->tokens[$this->current];
}
/**
* Gets the name associated with this stream (null if not defined).
*
* @return string|null
*
* @deprecated since 1.27 (to be removed in 2.0)
*/
public function getFilename()
{
@trigger_error(sprintf('The %s() method is deprecated since version 1.27 and will be removed in 2.0. Use getSourceContext() instead.', __METHOD__), E_USER_DEPRECATED);
return $this->source->getName();
}
/**
* Gets the source code associated with this stream.
*
* @return string
*
* @internal Don't use this as it might be empty depending on the environment configuration
*
* @deprecated since 1.27 (to be removed in 2.0)
*/
public function getSource()
{
@trigger_error(sprintf('The %s() method is deprecated since version 1.27 and will be removed in 2.0. Use getSourceContext() instead.', __METHOD__), E_USER_DEPRECATED);
return $this->source->getCode();
}
/**
* Gets the source associated with this stream.
*
* @return Source
*
* @internal
*/
public function getSourceContext()
public function getSourceContext(): Source
{
return $this->source;
}