Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c7bd705

Browse filesBrowse files
committed
Move error_handler in validate method
1 parent a79dfb6 commit c7bd705
Copy full SHA for c7bd705

File tree

1 file changed

+40
-60
lines changed
Filter options

1 file changed

+40
-60
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Command/LintCommand.php
+40-60Lines changed: 40 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -88,39 +88,8 @@ 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-
11491
if (['-'] === $filenames) {
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);
92+
return $this->display($input, $output, $io, [$this->validate(file_get_contents('php://stdin'), 'Standard Input', $showDeprecations)]);
12493
}
12594

12695
if (!$filenames) {
@@ -138,23 +107,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
138107
}
139108
}
140109

141-
try {
142-
$filesInfo = $this->getFilesInfo($filenames);
143-
} finally {
144-
if ($showDeprecations) {
145-
restore_error_handler();
146-
}
147-
}
148-
149-
return $this->display($input, $output, $io, $filesInfo, $deprecations);
110+
return $this->display($input, $output, $io, $this->getFilesInfo($filenames, $showDeprecations));
150111
}
151112

152-
private function getFilesInfo(array $filenames): array
113+
private function getFilesInfo(array $filenames, bool $showDeprecations): array
153114
{
154115
$filesInfo = [];
155116
foreach ($filenames as $filename) {
156117
foreach ($this->findFiles($filename) as $file) {
157-
$filesInfo[] = $this->validate(file_get_contents($file), $file);
118+
$filesInfo[] = $this->validate(file_get_contents($file), $file, $showDeprecations);
158119
}
159120
}
160121

@@ -172,8 +133,26 @@ protected function findFiles(string $filename): iterable
172133
throw new RuntimeException(\sprintf('File or directory "%s" is not readable.', $filename));
173134
}
174135

175-
private function validate(string $template, string $file): array
136+
private function validate(string $template, string $file, bool $collectDeprecation): array
176137
{
138+
$deprecations = [];
139+
if ($collectDeprecation) {
140+
$prevErrorHandler = set_error_handler(static function ($level, $message, $fileName, $line) use (&$prevErrorHandler, &$deprecations, $file) {
141+
if (\E_USER_DEPRECATED === $level) {
142+
$templateLine = 0;
143+
if (preg_match('/ at line (\d+)[ .]/', $message, $matches)) {
144+
$templateLine = $matches[1];
145+
}
146+
147+
$deprecations[] = ['message' => $message, 'file' => $file, 'line' => $templateLine];
148+
149+
return true;
150+
}
151+
152+
return $prevErrorHandler ? $prevErrorHandler($level, $message, $fileName, $line) : false;
153+
});
154+
}
155+
177156
$realLoader = $this->twig->getLoader();
178157
try {
179158
$temporaryLoader = new ArrayLoader([$file => $template]);
@@ -185,28 +164,33 @@ private function validate(string $template, string $file): array
185164
$this->twig->setLoader($realLoader);
186165

187166
return ['template' => $template, 'file' => $file, 'line' => $e->getTemplateLine(), 'valid' => false, 'exception' => $e];
167+
} finally {
168+
if ($collectDeprecation) {
169+
restore_error_handler();
170+
}
188171
}
189172

190-
return ['template' => $template, 'file' => $file, 'valid' => true];
173+
return ['template' => $template, 'file' => $file, 'deprecations' => $deprecations, 'valid' => true];
191174
}
192175

193-
private function display(InputInterface $input, OutputInterface $output, SymfonyStyle $io, array $files, array $deprecations): int
176+
private function display(InputInterface $input, OutputInterface $output, SymfonyStyle $io, array $files): int
194177
{
195178
return match ($this->format) {
196-
'txt' => $this->displayTxt($output, $io, $files, $deprecations),
197-
'json' => $this->displayJson($output, $files, $deprecations),
198-
'github' => $this->displayTxt($output, $io, $files, $deprecations, true),
179+
'txt' => $this->displayTxt($output, $io, $files),
180+
'json' => $this->displayJson($output, $files),
181+
'github' => $this->displayTxt($output, $io, $files, true),
199182
default => throw new InvalidArgumentException(\sprintf('Supported formats are "%s".', implode('", "', $this->getAvailableFormatOptions()))),
200183
};
201184
}
202185

203-
private function displayTxt(OutputInterface $output, SymfonyStyle $io, array $filesInfo, array $deprecations, bool $errorAsGithubAnnotations = false): int
186+
private function displayTxt(OutputInterface $output, SymfonyStyle $io, array $filesInfo, bool $errorAsGithubAnnotations = false): int
204187
{
205188
$errors = 0;
206189
$githubReporter = $errorAsGithubAnnotations ? new GithubActionReporter($output) : null;
190+
$deprecations = array_merge(...array_column($filesInfo, 'deprecations'));
207191

208192
foreach ($deprecations as $deprecation) {
209-
$this->renderDeprecation($io, $deprecation['exception'], $deprecation['file'], $githubReporter);
193+
$this->renderDeprecation($io, $deprecation['line'], $deprecation['message'], $deprecation['file'], $githubReporter);
210194
}
211195

212196
foreach ($filesInfo as $info) {
@@ -224,15 +208,13 @@ private function displayTxt(OutputInterface $output, SymfonyStyle $io, array $fi
224208
$io->warning(\sprintf('%d Twig files have valid syntax and %d contain errors.', \count($filesInfo) - $errors, $errors));
225209
}
226210

227-
return 0 === count($deprecations) ? min($errors, 1) : 1;
211+
return (empty($deprecations) && 0 === $errors) ? 0 : 1;
228212
}
229213

230-
private function displayJson(OutputInterface $output, array $filesInfo, array $deprecations): int
214+
private function displayJson(OutputInterface $output, array $filesInfo): int
231215
{
232216
$errors = 0;
233217

234-
$filesInfo = array_merge($filesInfo, $deprecations);
235-
236218
array_walk($filesInfo, function (&$v) use (&$errors) {
237219
$v['file'] = (string) $v['file'];
238220
unset($v['template']);
@@ -248,19 +230,17 @@ private function displayJson(OutputInterface $output, array $filesInfo, array $d
248230
return min($errors, 1);
249231
}
250232

251-
private function renderDeprecation(SymfonyStyle $output, Error $exception, string $file, ?GithubActionReporter $githubReporter): void
233+
private function renderDeprecation(SymfonyStyle $output, int $line, string $message, string $file, ?GithubActionReporter $githubReporter): void
252234
{
253-
$line = $exception->getTemplateLine();
254-
255-
$githubReporter?->error($exception->getRawMessage(), $file, $line <= 0 ? null : $line);
235+
$githubReporter?->error($message, $file, $line <= 0 ? null : $line);
256236

257237
if ($file) {
258238
$output->text(\sprintf('<info> DEPRECATION </info> in %s (line %s)', $file, $line));
259239
} else {
260240
$output->text(\sprintf('<info> DEPRECATION </info> (line %s)', $line));
261241
}
262242

263-
$output->text(\sprintf('<info> >> %s</info> ', $exception->getRawMessage()));
243+
$output->text(\sprintf('<info> >> %s</info> ', $message));
264244
}
265245

266246
private function renderException(SymfonyStyle $output, string $template, Error $exception, ?string $file = null, ?GithubActionReporter $githubReporter = null): void

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.