Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

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

Sign up
Appearance settings

Commitc7bd705

Browse files
committed
Move error_handler in validate method
1 parenta79dfb6 commitc7bd705

File tree

1 file changed

+40
-60
lines changed

1 file changed

+40
-60
lines changed

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

Lines 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(staticfunction ($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' =>newError($message,$templateLine)];
106-
107-
returntrue;
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-
privatefunctiongetFilesInfo(array$filenames):array
113+
privatefunctiongetFilesInfo(array$filenames,bool$showDeprecations):array
153114
{
154115
$filesInfo = [];
155116
foreach ($filenamesas$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
thrownewRuntimeException(\sprintf('File or directory "%s" is not readable.',$filename));
173134
}
174135

175-
privatefunctionvalidate(string$template,string$file):array
136+
privatefunctionvalidate(string$template,string$file,bool$collectDeprecation):array
176137
{
138+
$deprecations = [];
139+
if ($collectDeprecation) {
140+
$prevErrorHandler =set_error_handler(staticfunction ($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+
returntrue;
150+
}
151+
152+
return$prevErrorHandler ?$prevErrorHandler($level,$message,$fileName,$line) :false;
153+
});
154+
}
155+
177156
$realLoader =$this->twig->getLoader();
178157
try {
179158
$temporaryLoader =newArrayLoader([$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-
privatefunctiondisplay(InputInterface$input,OutputInterface$output,SymfonyStyle$io,array$files,array$deprecations):int
176+
privatefunctiondisplay(InputInterface$input,OutputInterface$output,SymfonyStyle$io,array$files):int
194177
{
195178
returnmatch ($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 =>thrownewInvalidArgumentException(\sprintf('Supported formats are "%s".',implode('", "',$this->getAvailableFormatOptions()))),
200183
};
201184
}
202185

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

208192
foreach ($deprecationsas$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 ($filesInfoas$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-
return0 ===count($deprecations) ?min($errors,1) :1;
211+
return(empty($deprecations) &&0 ===$errors) ?0 :1;
228212
}
229213

230-
privatefunctiondisplayJson(OutputInterface$output,array$filesInfo,array$deprecations):int
214+
privatefunctiondisplayJson(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
returnmin($errors,1);
249231
}
250232

251-
privatefunctionrenderDeprecation(SymfonyStyle$output,Error$exception,string$file, ?GithubActionReporter$githubReporter):void
233+
privatefunctionrenderDeprecation(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
privatefunctionrenderException(SymfonyStyle$output,string$template,Error$exception, ?string$file =null, ?GithubActionReporter$githubReporter =null):void

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp