8000 Fix the reporting of deprecations in twig:lint · symfony/symfony@8910be7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8910be7

Browse files
committed
Fix the reporting of deprecations in twig:lint
- ensure that the message is rendered when the line detection fails and we end up with 0 as line number (the implementation also deals with 8000 -1 which is sometimes used by Twig for errors when it does not know the line, even though this should not happen for compile-time errors). - fix the detection of the line number when the number is at the end of the sentence, which happens for the deprecation of filters for instance.
1 parent 78c0bcb commit 8910be7

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/Symfony/Bridge/Twig/Command/LintCommand.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
110110
$prevErrorHandler = set_error_handler(static function ($level, $message, $file, $line) use (&$prevErrorHandler) {
111111
if (E_USER_DEPRECATED === $level) {
112112
$templateLine = 0;
113-
if (preg_match('/ at line (\d+) /', $message, $matches)) {
113+
if (preg_match('/ at line (\d+)[ .]/', $message, $matches)) {
114114
$templateLine = $matches[1];
115115
}
116116

@@ -236,6 +236,14 @@ private function renderException(OutputInterface $output, string $template, Erro
236236
$output->text(sprintf('<error> ERROR </error> (line %s)', $line));
237237
}
238238

239+
// If the line is not known (this might happen for deprecations if we fail at detecting the line for instance),
240+
// we render the message without context, to ensure the message is displayed..
241+
if ($line <= 0) {
242+
$output->text(sprintf('<error> >> %s</error> ', $exception->getRawMessage()));
243+
244+
return;
245+
}
246+
239247
foreach ($this->getContext($template, $line) as $lineNumber => $code) {
240248
$output->text(sprintf(
241249
'%s %-6s %s',

src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Console\Tester\CommandTester;
1919
use Twig\Environment;
2020
use Twig\Loader\FilesystemLoader;
21+
use Twig\TwigFilter;
2122

2223
class LintCommandTest extends TestCase
2324
{
@@ -66,6 +67,33 @@ public function testLintFileCompileTimeException()
6667
$this->assertRegExp('/ERROR in \S+ \(line /', trim($tester->getDisplay()));
6768
}
6869

70+
/**
71+
* When deprecations are not reported by the command, the testsuite reporter will catch them so we need to mark the test as legacy.
72+
* @group legacy
73+
*/
74+
public function testLintFileWithNotReportedDeprecation()
75+
{
76+
$tester = $this->createCommandTester();
77+
$filename = $this->createFile('{{ foo|deprecated_filter }}');
78+
79+
$ret = $tester->execute(['filename' => [$filename]], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]);
80+
81+
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
82+
$this->assertStringContainsString('OK in', trim($tester->getDisplay()));
83+
}
84+
85+
public function testLintFileWithReportedDeprecation()
86+
{
87+
$tester = $this->createCommandTester();
88+
$filename = $this->createFile('{{ foo|deprecated_filter }}');
89+
90+
$ret = $tester->execute(['filename' => [$filename], '--show-deprecations' => true], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]);
91+
92+
$this->assertEquals(1, $ret, 'Returns 1 in case of error');
93+
$this->assertRegExp('/ERROR in \S+ \(line 1\)/', trim($tester->getDisplay()));
94+
$this->assertStringContainsString('Filter "deprecated_filter" is deprecated', trim($tester->getDisplay()));
95+
}
96+
6997
/**
7098
* @group tty
7199
*/
@ 93A8 @ -80,7 +108,12 @@ public function testLintDefaultPaths()
80108

81109
private function createCommandTester(): CommandTester
82110
{
83-
$command = new LintCommand(new Environment(new FilesystemLoader(\dirname(__DIR__).'/Fixtures/templates/')));
111+
$environment = new Environment(new FilesystemLoader(\dirname(__DIR__).'/Fixtures/templates/'));
112+
$environment->addFilter(new TwigFilter('deprecated_filter', function ($v) {
113+
return $v;
114+
}, array('deprecated' => true)));
115+
116+
$command = new LintCommand($environment);
84117

85118
$application = new Application();
86119
$application->add($command);

0 commit comments

Comments
 (0)
0