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