From fa1ca16c5ebc48639be5ac5e04cc0ce588995bb1 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Thu, 22 Aug 2024 18:27:36 +0200 Subject: [PATCH] Improve twig/twig lower constraint support (#306) --- composer.json | 2 +- src/Runner/Linter.php | 10 +++++++++- tests/Environment/StubbedEnvironmentTest.php | 4 ---- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 829bcbcd..ef3bee69 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "symfony/filesystem": "^5.4 || ^6.4 || ^7.0", "symfony/finder": "^5.4 || ^6.4 || ^7.0", "symfony/string": "^5.4.42 || ^6.4.10 || ~7.0.10 || ^7.1.3", - "twig/twig": "^3.10", + "twig/twig": "^3.4", "webmozart/assert": "^1.10" }, "require-dev": { diff --git a/src/Runner/Linter.php b/src/Runner/Linter.php index 8ea219de..d80da7b4 100644 --- a/src/Runner/Linter.php +++ b/src/Runner/Linter.php @@ -148,7 +148,7 @@ private function parseTemplate(string $content, string $filePath, Report $report try { $twigSource = new Source($content, $filePath); - return $this->env->parse($this->env->tokenize($twigSource)); + $node = $this->env->parse($this->env->tokenize($twigSource)); } catch (Error $error) { $violation = new Violation( Violation::LEVEL_FATAL, @@ -162,6 +162,14 @@ private function parseTemplate(string $content, string $filePath, Report $report return null; } + + // BC fix for twig/twig < 3.10. + $sourceContext = $node->getSourceContext(); + if (null !== $sourceContext) { + $node->setSourceContext($sourceContext); + } + + return $node; } private function setErrorHandler(Report $report, string $file): void diff --git a/tests/Environment/StubbedEnvironmentTest.php b/tests/Environment/StubbedEnvironmentTest.php index da26763b..2ed94a0e 100644 --- a/tests/Environment/StubbedEnvironmentTest.php +++ b/tests/Environment/StubbedEnvironmentTest.php @@ -126,10 +126,6 @@ public function testParse(): void public function testParseCacheTag(): void { - if (!InstalledVersions::satisfies(new VersionParser(), 'twig/twig', '>=3.2.0')) { - static::markTestSkipped('twig/twig ^3.2.0 is required.'); - } - $content = file_get_contents(__DIR__.'/Fixtures/cache_tag.html.twig'); static::assertNotFalse($content);