From d2cd4d9cfa779a633099b058b3a396e7fd1664f6 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 16 Apr 2019 17:25:44 +0200 Subject: [PATCH] fixed various small bugs --- src/Environment.php | 1 + src/Extension/CoreExtension.php | 2 +- src/Extension/StringLoaderExtension.php | 4 ++-- src/Lexer.php | 28 ++++++++++++------------- src/Test/IntegrationTestCase.php | 2 +- src/Test/NodeTestCase.php | 2 +- 6 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/Environment.php b/src/Environment.php index 7e89c661a27..2616533d930 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -470,6 +470,7 @@ public function loadClass($cls, $name, $index = null) $this->cache->load($key); } + $source = null; if (!class_exists($cls, false)) { $loader = $this->getLoader(); if (!$loader instanceof SourceContextLoaderInterface) { diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index 71ce3bf2222..37301dfd478 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -1658,7 +1658,7 @@ function twig_constant_is_defined($constant, $object = null) function twig_array_batch($items, $size, $fill = null, $preserveKeys = true) { if (!twig_test_iterable($items)) { - throw new RuntimeError(sprintf('The "batch" filter expects an array or "Traversable", got "%s".', \is_object($from) ? \get_class($from) : \gettype($from))); + throw new RuntimeError(sprintf('The "batch" filter expects an array or "Traversable", got "%s".', \is_object($items) ? \get_class($items) : \gettype($items))); } $size = ceil($size); diff --git a/src/Extension/StringLoaderExtension.php b/src/Extension/StringLoaderExtension.php index 295d5911ce1..baeba969e09 100644 --- a/src/Extension/StringLoaderExtension.php +++ b/src/Extension/StringLoaderExtension.php @@ -35,7 +35,7 @@ class_alias('Twig\Extension\StringLoaderExtension', 'Twig_Extension_StringLoader namespace { use Twig\Environment; -use Twig\Template; +use Twig\TemplateWrapper; /** * Loads a template from a string. @@ -45,7 +45,7 @@ class_alias('Twig\Extension\StringLoaderExtension', 'Twig_Extension_StringLoader * @param string $template A template as a string or object implementing __toString() * @param string $name An optional name of the template to be used in error messages * - * @return Template + * @return TemplateWrapper */ function twig_template_from_string(Environment $env, $template, $name = null) { diff --git a/src/Lexer.php b/src/Lexer.php index c103da0d92e..8cb63f388eb 100644 --- a/src/Lexer.php +++ b/src/Lexer.php @@ -277,11 +277,11 @@ protected function lexData() case $this->options['tag_block'][0]: // raw data? - if (preg_match($this->regexes['lex_block_raw'], $this->code, $match, null, $this->cursor)) { + if (preg_match($this->regexes['lex_block_raw'], $this->code, $match, 0, $this->cursor)) { $this->moveCursor($match[0]); $this->lexRawData($match[1]); // {% line \d+ %} - } elseif (preg_match($this->regexes['lex_block_line'], $this->code, $match, null, $this->cursor)) { + } elseif (preg_match($this->regexes['lex_block_line'], $this->code, $match, 0, $this->cursor)) { $this->moveCursor($match[0]); $this->lineno = (int) $match[1]; } else { @@ -301,7 +301,7 @@ protected function lexData() protected function lexBlock() { - if (empty($this->brackets) && preg_match($this->regexes['lex_block'], $this->code, $match, null, $this->cursor)) { + if (empty($this->brackets) && preg_match($this->regexes['lex_block'], $this->code, $match, 0, $this->cursor)) { $this->pushToken(Token::BLOCK_END_TYPE); $this->moveCursor($match[0]); $this->popState(); @@ -312,7 +312,7 @@ protected function lexBlock() protected function lexVar() { - if (empty($this->brackets) && preg_match($this->regexes['lex_var'], $this->code, $match, null, $this->cursor)) { + if (empty($this->brackets) && preg_match($this->regexes['lex_var'], $this->code, $match, 0, $this->cursor)) { $this->pushToken(Token::VAR_END_TYPE); $this->moveCursor($match[0]); $this->popState(); @@ -324,7 +324,7 @@ protected function lexVar() protected function lexExpression() { // whitespace - if (preg_match('/\s+/A', $this->code, $match, null, $this->cursor)) { + if (preg_match('/\s+/A', $this->code, $match, 0, $this->cursor)) { $this->moveCursor($match[0]); if ($this->cursor >= $this->end) { @@ -333,17 +333,17 @@ protected function lexExpression() } // operators - if (preg_match($this->regexes['operator'], $this->code, $match, null, $this->cursor)) { + if (preg_match($this->regexes['operator'], $this->code, $match, 0, $this->cursor)) { $this->pushToken(Token::OPERATOR_TYPE, preg_replace('/\s+/', ' ', $match[0])); $this->moveCursor($match[0]); } // names - elseif (preg_match(self::REGEX_NAME, $this->code, $match, null, $this->cursor)) { + elseif (preg_match(self::REGEX_NAME, $this->code, $match, 0, $this->cursor)) { $this->pushToken(Token::NAME_TYPE, $match[0]); $this->moveCursor($match[0]); } // numbers - elseif (preg_match(self::REGEX_NUMBER, $this->code, $match, null, $this->cursor)) { + elseif (preg_match(self::REGEX_NUMBER, $this->code, $match, 0, $this->cursor)) { $number = (float) $match[0]; // floats if (ctype_digit($match[0]) && $number <= PHP_INT_MAX) { $number = (int) $match[0]; // integers lower than the maximum @@ -373,12 +373,12 @@ protected function lexExpression() ++$this->cursor; } // strings - elseif (preg_match(self::REGEX_STRING, $this->code, $match, null, $this->cursor)) { + elseif (preg_match(self::REGEX_STRING, $this->code, $match, 0, $this->cursor)) { $this->pushToken(Token::STRING_TYPE, stripcslashes(substr($match[0], 1, -1))); $this->moveCursor($match[0]); } // opening double quoted string - elseif (preg_match(self::REGEX_DQ_STRING_DELIM, $this->code, $match, null, $this->cursor)) { + elseif (preg_match(self::REGEX_DQ_STRING_DELIM, $this->code, $match, 0, $this->cursor)) { $this->brackets[] = ['"', $this->lineno]; $this->pushState(self::STATE_STRING); $this->moveCursor($match[0]); @@ -428,15 +428,15 @@ protected function lexComment() protected function lexString() { - if (preg_match($this->regexes['interpolation_start'], $this->code, $match, null, $this->cursor)) { + if (preg_match($this->regexes['interpolation_start'], $this->code, $match, 0, $this->cursor)) { $this->brackets[] = [$this->options['interpolation'][0], $this->lineno]; $this->pushToken(Token::INTERPOLATION_START_TYPE); $this->moveCursor($match[0]); $this->pushState(self::STATE_INTERPOLATION); - } elseif (preg_match(self::REGEX_DQ_STRING_PART, $this->code, $match, null, $this->cursor) && \strlen($match[0]) > 0) { + } elseif (preg_match(self::REGEX_DQ_STRING_PART, $this->code, $match, 0, $this->cursor) && \strlen($match[0]) > 0) { $this->pushToken(Token::STRING_TYPE, stripcslashes($match[0])); $this->moveCursor($match[0]); - } elseif (preg_match(self::REGEX_DQ_STRING_DELIM, $this->code, $match, null, $this->cursor)) { + } elseif (preg_match(self::REGEX_DQ_STRING_DELIM, $this->code, $match, 0, $this->cursor)) { list($expect, $lineno) = array_pop($this->brackets); if ('"' != $this->code[$this->cursor]) { throw new SyntaxError(sprintf('Unclosed "%s".', $expect), $lineno, $this->source); @@ -453,7 +453,7 @@ protected function lexString() protected function lexInterpolation() { $bracket = end($this->brackets); - if ($this->options['interpolation'][0] === $bracket[0] && preg_match($this->regexes['interpolation_end'], $this->code, $match, null, $this->cursor)) { + if ($this->options['interpolation'][0] === $bracket[0] && preg_match($this->regexes['interpolation_end'], $this->code, $match, 0, $this->cursor)) { array_pop($this->brackets); $this->pushToken(Token::INTERPOLATION_END_TYPE); $this->moveCursor($match[0]); diff --git a/src/Test/IntegrationTestCase.php b/src/Test/IntegrationTestCase.php index d1b633ab947..36b36075867 100644 --- a/src/Test/IntegrationTestCase.php +++ b/src/Test/IntegrationTestCase.php @@ -194,7 +194,7 @@ protected function doIntegrationTest($file, $message, $condition, $templates, $e $message = $e->getMessage(); $this->assertSame(trim($exception), trim(sprintf('%s: %s', \get_class($e), $message))); $last = substr($message, \strlen($message) - 1); - $this->assertTrue('.' === $last || '?' === $last, $message, 'Exception message must end with a dot or a question mark.'); + $this->assertTrue('.' === $last || '?' === $last, 'Exception message must end with a dot or a question mark.'); return; } diff --git a/src/Test/NodeTestCase.php b/src/Test/NodeTestCase.php index b35cd21853c..f3358cb9a36 100644 --- a/src/Test/NodeTestCase.php +++ b/src/Test/NodeTestCase.php @@ -56,7 +56,7 @@ protected function getVariableGetter($name, $line = false) $line = $line > 0 ? "// line {$line}\n" : ''; if (\PHP_VERSION_ID >= 70000) { - return sprintf('%s($context["%s"] ?? null)', $line, $name, $name); + return sprintf('%s($context["%s"] ?? null)', $line, $name); } if (\PHP_VERSION_ID >= 50400) {