8000 Collect all deprecations with lint:twig command · symfony/symfony@aa668e0 · GitHub
[go: up one dir, main page]

Skip to content

Commit aa668e0

Browse files
committed
Collect all deprecations with lint:twig command
1 parent d9e5036 commit aa668e0

File tree

3 files changed

+76
-25
lines changed

3 files changed

+76
-25
lines changed

src/Symfony/Bridge/Twig/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Add `is_granted_for_user()` Twig function
88
* Add `field_id()` Twig form helper function
9+
* Make `lint:twig` collect all deprecations instead of stopping at the first one
910

1011
7.2
1112
---

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

+61-24
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,39 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8888
$this->excludes = $input->getOption('excludes');
8989
$this->format = $input->getOption('format') ?? (GithubActionReporter::isGithubActionEnvironment() ? 'github' : 'txt');
9090

91+
$deprecations = [];
92+
if ($showDeprecations) {
93+
$prevErrorHandler = set_error_handler(static function ($level, $message, $file, $line) use (&$prevErrorHandler, &$deprecations) {
94+
if (\E_USER_DEPRECATED === $level) {
95+
$templateLine = 0;
96+
if (preg_match('/ at line (\d+)[ .]/', $message, $matches)) {
97+
$templateLine = $matches[1];
98+
}
99+
100+
$templateFile = 'UNKNOWN';
101+
if (preg_match('/ in (.+) at/', $message, $matches)) {
102+
$templateFile = $matches[1];
103+
}
104+
105+
$deprecations[] = ['template' => $templateFile, 'message' => $message, 'file' => $templateFile, 'line' => $templateLine, 'valid' => false, 'exception' => new Error($message, $templateLine)];
106+
107+
return true;
108+
}
109+
110+
return $prevErrorHandler ? $prevErrorHandler($level, $message, $file, $line) : false;
111+
});
112+
}
113+
91114
if (['-'] === $filenames) {
92-
return $this->display($input, $output, $io, [$this->validate(file_get_contents('php://stdin'), 'Standard Input')]);
115+
try {
116+
$error = $this->validate(file_get_contents('php://stdin'), 'Standard Input');
117+
} finally {
118+
if ($showDeprecations) {
119+
restore_error_handler();
120+
}
121+
}
122+
123+
return $this->display($input, $output, $io, [$error], $deprecations);
93124
}
94125

95126
if (!$filenames) {
@@ -107,21 +138,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
107138
}
108139
}
109140

110-
if ($showDeprecations) {
111-
$prevErrorHandler = set_error_handler(static function ($level, $message, $file, $line) use (&$prevErrorHandler) {
112-
if (\E_USER_DEPRECATED === $level) {
113-
$templateLine = 0;
114-
if (preg_match('/ at line (\d+)[ .]/', $message, $matches)) {
115-
$templateLine = $matches[1];
116-
}
117-
118-
throw new Error($message, $templateLine);
119-
}
120-
121-
return $prevErrorHandler ? $prevErrorHandler($level, $message, $file, $line) : false;
122-
});
123-
}
124-
125141
try {
126142
$filesInfo = $this->getFilesInfo($filenames);
127143
} finally {
@@ -130,7 +146,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
130146
}
131147
}
132148

133-
return $this->display($input, $output, $io, $filesInfo);
149+
return $this->display($input, $output, $io, $filesInfo, $deprecations);
134150
}
135151

136152
private function getFilesInfo(array $filenames): array
@@ -174,21 +190,25 @@ private function validate(string $template, string $file): array
174190
return ['template' => $template, 'file' => $file, 'valid' => true];
175191
}
176192

177-
private function display(InputInterface $input, OutputInterface $output, SymfonyStyle $io, array $files): int
193+
private function display(InputInterface $input, OutputInterface $output, SymfonyStyle $io, array $files, array $deprecations): int
178194
{
179195
return match ($this->format) {
180-
'txt' => $this->displayTxt($output, $io, $files),
181-
'json' => $this->displayJson($output, $files),
182-
'github' => $this->displayTxt($output, $io, $files, true),
196+
'txt' => $this->displayTxt($output, $io, $files, $deprecations),
197+
'json' => $this->displayJson($output, $files, $deprecations),
198+
'github' => $this->displayTxt($output, $io, $files, $deprecations, true),
183199
default => throw new InvalidArgumentException(\sprintf('Supported formats are "%s".', implode('", "', $this->getAvailableFormatOptions()))),
184200
};
185201
}
186202

187-
private function displayTxt(OutputInterface $output, SymfonyStyle $io, array $filesInfo, bool $errorAsGithubAnnotations = false): int
203+
private function displayTxt(OutputInterface $output, SymfonyStyle $io, array $filesInfo, array $deprecations, bool $errorAsGithubAnnotations = false): int
188204
{
189205
$errors = 0;
190206
$githubReporter = $errorAsGithubAnnotations ? new GithubActionReporter($output) : null;
191207

208+
foreach ($deprecations as $deprecation) {
209+
$this->renderDeprecation($io, $deprecation['exception'], $deprecation['file'], $githubReporter);
210+
}
211+
192212
foreach ($filesInfo as $info) {
193213
if ($info['valid'] && $output->isVerbose()) {
194214
$io->comment('<info>OK</info>'.($info['file'] ? \sprintf(' in %s', $info['file']) : ''));
@@ -204,13 +224,15 @@ private function displayTxt(OutputInterface $output, SymfonyStyle $io, array $fi
204224
$io->warning(\sprintf('%d Twig files have valid syntax and %d contain errors.', \count($filesInfo) - $errors, $errors));
205225
}
206226

207-
return min($errors, 1);
227+
return 0 === count($deprecations) ? min($errors, 1) : 1;
208228
}
209229

210-
private function displayJson(OutputInterface $output, array $filesInfo): int
230+
private function displayJson(OutputInterface $output, array $filesInfo, array $deprecations = []): int
211231
{
212232
$errors = 0;
213233

234+
$filesInfo = array_merge($filesInfo, $deprecations);
235+
214236
array_walk($filesInfo, function (&$v) use (&$errors) {
215237
$v['file 10000 '] = (string) $v['file'];
216238
unset($v['template']);
@@ -226,6 +248,21 @@ private function displayJson(OutputInterface $output, array $filesInfo): int
226248
return min($errors, 1);
227249
}
228250

251+
private function renderDeprecation(SymfonyStyle $output, Error $exception, string $file, ?GithubActionReporter $githubReporter): void
252+
{
253+
$line = $exception->getTemplateLine();
254+
255+
$githubReporter?->error($exception->getRawMessage(), $file, $line <= 0 ? null : $line);
256+
257+
if ($file) {
258+
$output->text(\sprintf('<info> DEPRECATION </info> in %s (line %s)', $file, $line));
259+
} else {
260+
$output->text(\sprintf('<info> DEPRECATION </info> (line %s)', $line));
261+
}
262+
263+
$output->text(\sprintf('<info> >> %s</info> ', $exception->getRawMessage()));
264+
}
265+
229266
private function renderException(SymfonyStyle $output, string $template, Error $exception, ?string $file = null, ?GithubActionReporter $githubReporter = null): void
230267
{
231268
$line = $exception->getTemplateLine();

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,20 @@ public function testLintFileWithReportedDeprecation()
9494
$ret = $tester->execute(['filename' => [$filename], '--show-deprecations' => true], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]);
9595

9696
$this->assertEquals(1, $ret, 'Returns 1 in case of error');
97-
$this->assertMatchesRegularExpression('/ERROR in \S+ \(line 1\)/', trim($tester->getDisplay()));
97+
$this->assertMatchesRegularExpression('/DEPRECATION in \S+ \(line 1\)/', trim($tester->getDisplay()));
98+
$this->assertStringContainsString('Filter "deprecated_filter" is deprecated', trim($tester->getDisplay()));
99+
}
100+
101+
public function testLintFileWithMultipleReportedDeprecation()
102+
{
103+
$tester = $this->createCommandTester();
104+
$filename = $this->createFile("{{ foo|deprecated_filter }}\n{{ bar|deprecated_filter }}");
105+
106+
$ret = $tester->execute(['filename' => [$filename], '--show-deprecations' => true], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]);
107+
108+
$this->assertEquals(1, $ret, 'Returns 1 in case of error');
109+
$this->assertMatchesRegularExpression('/DEPRECATION in \S+ \(line 1\)/', trim($tester->getDisplay()));
110+
$this->assertMatchesRegularExpression('/DEPRECATION in \S+ \(line 2\)/', trim($tester->getDisplay()));
98111
$this->assertStringContainsString('Filter "deprecated_filter" is deprecated', trim($tester->getDisplay()));
99112
}
100113

0 commit comments

Comments
 (0)
0