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

Skip to content

Commit c329ca7

Browse files
stofnicolas-grekas
authored andcommitted
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 -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 c329ca7

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-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: 35 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,34 @@ 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+
*
73+
* @group legacy
74+
*/
75+
public function testLintFileWithNotReportedDeprecation()
76+
{
77+
$tester = $this->createCommandTester();
78+
$filename = $this->createFile('{{ foo|deprecated_filter }}');
79+
80+
$ret = $tester->execute(['filename' => [$filename]], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]);
81+
82+
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
83+
$this->assertStringContainsString('OK in', trim($tester->getDisplay()));
84+
}
85+
86+
public function testLintFileWithReportedDeprecation()
87+
{
88+
$tester = $this->createCommandTester();
89+
$filename = $this->createFile('{{ foo|deprecated_filter }}');
90+
91+
$ret = $tester->execute(['filename' => [$filename], '--show-deprecations' => true], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]);
92+
93+
$this->assertEquals(1, $ret, 'Returns 1 in case of error');
94+
$this->assertRegExp('/ERROR in \S+ \(line 1\)/', trim($tester->getDisplay()));
95+
$this->assertStringContainsString('Filter "deprecated_filter" is deprecated', trim($tester->getDisplay()));
96+
}
97+
6998
/**
7099
* @group tty
71100
*/
@@ -80,7 +109,12 @@ public function testLintDefaultPaths()
80109

81110
private function createCommandTester(): CommandTester
82111
{
83-
$command = new LintCommand(new Environment(new FilesystemLoader(\dirname(__DIR__).'/Fixtures/templates/')));
112+
$environment = new Environment(new FilesystemLoader(\dirname(__DIR__).'/Fixtures/templates/'));
113+
$environment->addFilter(new TwigFilter('deprecated_filter', function ($v) {
114+
return $v;
115+
}, ['deprecated' => true]));
116+
117+
$command = new LintCommand($environment);
84118

85119
$application = new Application();
86120
$application->add($command);

0 commit comments

Comments
 (0)
0