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

Skip to content

Commit 2ba99c3

Browse files
committed
Collect all deprecations with lint:yaml command
1 parent f803dc1 commit 2ba99c3

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
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

+39-10
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
107107
}
108108
}
109109

110+
$deprecations = [];
110111
if ($showDeprecations) {
111-
$prevErrorHandler = set_error_handler(static function ($level, $message, $file, $line) use (&$prevErrorHandler) {
112+
$prevErrorHandler = set_error_handler(static function ($level, $message, $file, $line) use (&$prevErrorHandler, &$deprecations) {
112113
if (\E_USER_DEPRECATED === $level) {
113114
$templateLine = 0;
114115
if (preg_match('/ at line (\d+)[ .]/', $message, $matches)) {
115116
$templateLine = $matches[1];
116117
}
117118

118-
throw new Error($message, $templateLine);
119+
$templateFile = 'UNKNOWN';
120+
if (preg_match('/ in (.*) at/', $message, $matches)) {
121+
$templateFile = $matches[1];
122+
}
123+
124+
$deprecations[] = ['template' => $templateFile, 'message' => $message, 'file' => $templateFile, 'line' => $templateLine, 'valid' => false, 'exception' => new Error($message, $templateLine)];
125+
126+
return true;
119127
}
120128

121129
return $prevErrorHandler ? $prevErrorHandler($level, $message, $file, $line) : false;
@@ -130,7 +138,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
130138
}
131139
}
132140

133-
return $this->display($input, $output, $io, $filesInfo);
141+
return $this->display($input, $output, $io, $filesInfo, $deprecations);
134142
}
135143

136144
private function getFilesInfo(array $filenames): array
@@ -174,21 +182,25 @@ private function validate(string $template, string $file): array
174182
return ['template' => $template, 'file' => $file, 'valid' => true];
175183
}
176184

177-
private function display(InputInterface $input, OutputInterface $output, SymfonyStyle $io, array $files): int
185+
private function display(InputInterface $input, OutputInterface $output, SymfonyStyle $io, array $files, array $deprecations = []): int
178186
{
179187
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),
188+
'txt' => $this->displayTxt($output, $io, $files, $deprecations),
189+
'json' => $this->displayJson($output, $files, $deprecations),
190+
'github' => $this->displayTxt($output, $io, $files, $deprecations, true),
183191
default => throw new InvalidArgumentException(\sprintf('Supported formats are "%s".', implode('", "', $this->getAvailableFormatOptions()))),
184192
};
185193
}
186194

187-
private function displayTxt(OutputInterface $output, SymfonyStyle $io, array $filesInfo, bool $errorAsGithubAnnotations = false): int
195+
private function displayTxt(OutputInterface $output, SymfonyStyle $io, array $filesInfo, array $deprecations = [], bool $errorAsGithubAnnotations = false): int
188196
{
189197
$errors = 0;
190198
$githubReporter = $errorAsGithubAnnotations ? new GithubActionReporter($output) : null;
191199

200+
foreach ($deprecations as $deprecation) {
201+
$this->renderDeprecation($io, $deprecation['exception'], $deprecation['file']);
202+
}
203+
192204
foreach ($filesInfo as $info) {
193205
if ($info['valid'] && $output->isVerbose()) {
194206
$io->comment('<info>OK</info>'.($info['file'] ? \sprintf(' in %s', $info['file']) : ''));
@@ -204,13 +216,15 @@ private function displayTxt(OutputInterface $output, SymfonyStyle $io, array $fi
204216
$io->warning(\sprintf('%d Twig files have valid syntax and %d contain errors.', \count($filesInfo) - $errors, $errors));
205217
}
206218

207-
return min($errors, 1);
219+
return 0 === count($deprecations) ? min($errors, 1) : 1;
208220
}
209221

210-
private function displayJson(OutputInterface $output, array $filesInfo): int
222+
private function displayJson(OutputInterface $output, array $filesInfo, array $deprecations = []): int
211223
{
212224
$errors = 0;
213225

226+
$filesInfo = array_merge($filesInfo, $deprecations);
227+
214228
array_walk($filesInfo, function (&$v) use (&$errors) {
215229
$v['file'] = (string) $v['file'];
216230
unset($v['template']);
@@ -226,6 +240,21 @@ private function displayJson(OutputInterface $output, array $filesInfo): int
226240
return min($errors, 1);
227241
}
228242

243+
private function renderDeprecation(SymfonyStyle $output, Error $exception, ?string $file = null, ?GithubActionReporter $githubReporter = null): void
244+
{
245+
$line = $exception->getTemplateLine();
246+
247+
$githubReporter?->error($exception->getRawMessage(), $file, $line <= 0 ? null : $line);
248+
249+
if ($file) {
250+
$output->text(\sprintf('<info> DEPRECATION </info> in %s (line %s)', $file, $line));
251+
} else {
252+
$output->text(\sprintf('<info> DEPRECATION </info> (line %s)', $line));
253+
}
254+
255+
$output->text(\sprintf('<info> >> %s</info> ', $exception->getRawMessage()));
256+
}
257+
229258
private function renderException(SymfonyStyle $output, string $template, Error $exception, ?string $file = null, ?GithubActionReporter $githubReporter = null): void
230259
{
231260
$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