@@ -88,39 +88,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
88
88
$ this ->excludes = $ input ->getOption ('excludes ' );
89
89
$ this ->format = $ input ->getOption ('format ' ) ?? (GithubActionReporter::isGithubActionEnvironment () ? 'github ' : 'txt ' );
90
90
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
-
114
91
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 )]);
124
93
}
125
94
126
95
if (!$ filenames ) {
@@ -138,23 +107,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
138
107
}
139
108
}
140
109
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 ));
150
111
}
151
112
152
- private function getFilesInfo (array $ filenames ): array
113
+ private function getFilesInfo (array $ filenames, bool $ showDeprecations ): array
153
114
{
154
115
$ filesInfo = [];
155
116
foreach ($ filenames as $ filename ) {
156
117
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 );
158
119
}
159
120
}
160
121
@@ -172,8 +133,26 @@ protected function findFiles(string $filename): iterable
172
133
throw new RuntimeException (\sprintf ('File or directory "%s" is not readable. ' , $ filename ));
173
134
}
174
135
175
- private function validate (string $ template , string $ file ): array
136
+ private function validate (string $ template , string $ file, bool $ collectDeprecation ): array
176
137
{
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
+
177
156
$ realLoader = $ this ->twig ->getLoader ();
178
157
try {
179
158
$ temporaryLoader = new ArrayLoader ([$ file => $ template ]);
@@ -185,28 +164,33 @@ private function validate(string $template, string $file): array
185
164
$ this ->twig ->setLoader ($ realLoader );
186
165
187
166
return ['template ' => $ template , 'file ' => $ file , 'line ' => $ e ->getTemplateLine (), 'valid ' => false , 'exception ' => $ e ];
167
+ } finally {
168
+ if ($ collectDeprecation ) {
169
+ restore_error_handler ();
170
+ }
188
171
}
189
172
190
- return ['template ' => $ template , 'file ' => $ file , 'valid ' => true ];
173
+ return ['template ' => $ template , 'file ' => $ file , 'deprecations ' => $ deprecations , ' valid ' => true ];
191
174
}
192
175
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
194
177
{
195
178
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 ),
199
182
default => throw new InvalidArgumentException (\sprintf ('Supported formats are "%s". ' , implode ('", " ' , $ this ->getAvailableFormatOptions ()))),
200
183
};
201
184
}
202
185
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
204
187
{
205
188
$ errors = 0 ;
206
189
$ githubReporter = $ errorAsGithubAnnotations ? new GithubActionReporter ($ output ) : null ;
190
+ $ deprecations = array_merge (...array_column ($ filesInfo , 'deprecations ' ));
207
191
208
192
foreach ($ deprecations as $ deprecation ) {
209
- $ this ->renderDeprecation ($ io , $ deprecation ['exception ' ], $ deprecation ['file ' ], $ githubReporter );
193
+ $ this ->renderDeprecation ($ io , $ deprecation ['line ' ], $ deprecation [ ' message ' ], $ deprecation ['file ' ], $ githubReporter );
210
194
}
211
195
212
196
foreach ($ filesInfo as $ info ) {
@@ -224,15 +208,13 @@ private function displayTxt(OutputInterface $output, SymfonyStyle $io, array $fi
224
208
$ io ->warning (\sprintf ('%d Twig files have valid syntax and %d contain errors. ' , \count ($ filesInfo ) - $ errors , $ errors ));
225
209
}
226
210
227
- return 0 === count ( $ deprecations ) ? min ( $ errors , 1 ) : 1 ;
211
+ return ( empty ( $ deprecations ) && 0 === $ errors ) ? 0 : 1 ;
228
212
}
229
213
230
- private function displayJson (OutputInterface $ output , array $ filesInfo, array $ deprecations ): int
214
+ private function displayJson (OutputInterface $ output , array $ filesInfo ): int
231
215
{
232
216
$ errors = 0 ;
233
217
234
- $ filesInfo = array_merge ($ filesInfo , $ deprecations );
235
-
236
218
array_walk ($ filesInfo , function (&$ v ) use (&$ errors ) {
237
219
$ v ['file ' ] = (string ) $ v ['file ' ];
238
220
unset($ v ['template ' ]);
@@ -248,19 +230,17 @@ private function displayJson(OutputInterface $output, array $filesInfo, array $d
248
230
return min ($ errors , 1 );
249
231
}
250
232
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
252
234
{
253
- $ line = $ exception ->getTemplateLine ();
254
-
255
- $ githubReporter ?->error($ exception ->getRawMessage (), $ file , $ line <= 0 ? null : $ line );
235
+ $ githubReporter ?->error($ message , $ file , $ line <= 0 ? null : $ line );
256
236
257
237
if ($ file ) {
258
238
$ output ->text (\sprintf ('<info> DEPRECATION </info> in %s (line %s) ' , $ file , $ line ));
259
239
} else {
260
240
$ output ->text (\sprintf ('<info> DEPRECATION </info> (line %s) ' , $ line ));
261
241
}
262
242
263
- $ output ->text (\sprintf ('<info> >> %s</info> ' , $ exception -> getRawMessage () ));
243
+ $ output ->text (\sprintf ('<info> >> %s</info> ' , $ message ));
264
244
}
265
245
266
246
private function renderException (SymfonyStyle $ output , string $ template , Error $ exception , ?string $ file = null , ?GithubActionReporter $ githubReporter = null ): void
0 commit comments