diff --git a/src/Lexer.php b/src/Lexer.php index 2e8f811223c..f58a0b02a4f 100644 --- a/src/Lexer.php +++ b/src/Lexer.php @@ -428,12 +428,7 @@ private function stripcslashes(string $str, string $quoteType): string if (isset(self::SPECIAL_CHARS[$nextChar])) { $result .= self::SPECIAL_CHARS[$nextChar]; - } elseif ($nextChar === '\\') { - $result .= $nextChar; - } elseif ($nextChar === "'" || $nextChar === '"') { - if ($nextChar !== $quoteType) { - trigger_deprecation('twig/twig', '3.12', 'Character "%s" at position %d does not need to be escaped anymore.', $nextChar, $i + 1); - } + } elseif ($nextChar === '\\' || $nextChar === $quoteType) { $result .= $nextChar; } elseif ($nextChar === '#' && $i + 1 < $length && $str[$i + 1] === '{') { $result .= '#{'; @@ -451,8 +446,7 @@ private function stripcslashes(string $str, string $quoteType): string } $result .= chr(octdec($octal)); } else { - trigger_deprecation('twig/twig', '3.12', sprintf('Character "%s" at position %d does not need to be escaped anymore.', $nextChar, $i + 1)); - $result .= $nextChar; + $result .= '\\' . $nextChar; } $i++; diff --git a/tests/LexerTest.php b/tests/LexerTest.php index 81690829a08..2865021f3c7 100644 --- a/tests/LexerTest.php +++ b/tests/LexerTest.php @@ -12,7 +12,6 @@ */ use PHPUnit\Framework\TestCase; -use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; use Twig\Environment; use Twig\Error\SyntaxError; use Twig\Lexer; @@ -22,8 +21,6 @@ class LexerTest extends TestCase { - use ExpectDeprecationTrait; - public function testNameLabelForTag() { $template = '{% ยง %}'; @@ -195,6 +192,18 @@ public function testStringWithEscapedDelimiter(string $template, string $expecte public function getStringWithEscapedDelimiter() { + yield '{{ \'App\Test\' }} => App\Test' => [ + '{{ \'App\\Test\' }}', + 'App\Test', + ]; + yield '{{ "foo \\\' bar" }} => foo \' bar' => [ + '{{ "foo \\\' bar" }}', + "foo \' bar", + ]; + yield '{{ \'foo \" bar\' }} => foo \" bar' => [ + '{{ \'foo \\" bar\' }}', + 'foo \" bar', + ]; yield '{{ \'\x6\' }} => \x6' => [ '{{ \'\x6\' }}', "\x6", @@ -229,43 +238,6 @@ public function getStringWithEscapedDelimiter() ]; } - /** - * @group legacy - * @dataProvider getStringWithEscapedDelimiterProducingDeprecation - */ - public function testStringWithEscapedDelimiterProducingDeprecation(string $template, string $expected, string $expectedDeprecation) - { - $this->expectDeprecation($expectedDeprecation); - - $lexer = new Lexer(new Environment(new ArrayLoader())); - $stream = $lexer->tokenize(new Source($template, 'index')); - $stream->expect(Token::VAR_START_TYPE); - $stream->expect(Token::STRING_TYPE, $expected); - - // add a dummy assertion here to satisfy PHPUnit, the only thing we want to test is that the code above - // can be executed without throwing any exceptions - $this->addToAssertionCount(1); - } - - public function getStringWithEscapedDelimiterProducingDeprecation() - { - yield '{{ \'App\Test\' }} => AppTest' => [ - '{{ \'App\\Test\' }}', - 'AppTest', - "Since twig/twig 3.12: Character \"T\" at position 5 does not need to be escaped anymore.", - ]; - yield '{{ "foo \\\' bar" }} => foo \' bar' => [ - '{{ "foo \\\' bar" }}', - 'foo \' bar', - "Since twig/twig 3.12: Character \"'\" at position 6 does not need to be escaped anymore.", - ]; - yield '{{ \'foo \" bar\' }} => foo " bar' => [ - '{{ \'foo \\" bar\' }}', - 'foo " bar', - 'Since twig/twig 3.12: Character """ at position 6 does not need to be escaped anymore.', - ]; - } - public function testStringWithInterpolation() { $template = 'foo {{ "bar #{ baz + 1 }" }}';