@@ -107,15 +107,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
107
107
}
108
108
}
109
109
110
+ $ deprecations = [];
110
111
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 ) {
112
113
if (\E_USER_DEPRECATED === $ level ) {
113
114
$ templateLine = 0 ;
114
115
if (preg_match ('/ at line (\d+)[ .]/ ' , $ message , $ matches )) {
115
116
$ templateLine = $ matches [1 ];
116
117
}
117
118
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 ;
119
127
}
120
128
121
129
return $ prevErrorHandler ? $ prevErrorHandler ($ level , $ message , $ file , $ line ) : false ;
@@ -130,7 +138,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
130
138
}
131
139
}
132
140
133
- return $ this ->display ($ input , $ output , $ io , $ filesInfo );
141
+ return $ this ->display ($ input , $ output , $ io , $ filesInfo, $ deprecations );
134
142
}
135
143
136
144
private function getFilesInfo (array $ filenames ): array
@@ -174,21 +182,25 @@ private function validate(string $template, string $file): array
174
182
return ['template ' => $ template , 'file ' => $ file , 'valid ' => true ];
175
183
}
176
184
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
178
186
{
179
187
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 ),
183
191
default => throw new InvalidArgumentException (\sprintf ('Supported formats are "%s". ' , implode ('", " ' , $ this ->getAvailableFormatOptions ()))),
184
192
};
185
193
}
186
194
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
188
196
{
189
197
$ errors = 0 ;
190
198
$ githubReporter = $ errorAsGithubAnnotations ? new GithubActionReporter ($ output ) : null ;
191
199
200
+ foreach ($ deprecations as $ deprecation ) {
201
+ $ this ->renderDeprecation ($ io , $ deprecation ['exception ' ], $ deprecation ['file ' ]);
202
+ }
203
+
192
204
foreach ($ filesInfo as $ info ) {
193
205
if ($ info ['valid ' ] && $ output ->isVerbose ()) {
194
206
$ 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
204
216
$ io ->warning (\sprintf ('%d Twig files have valid syntax and %d contain errors. ' , \count ($ filesInfo ) - $ errors , $ errors ));
205
217
}
206
218
207
- return min ($ errors , 1 );
219
+ return 0 === count ( $ deprecations ) ? min ($ errors , 1 ) : 1 ;
208
220
}
209
221
210
- private function displayJson (OutputInterface $ output , array $ filesInfo ): int
222
+ private function displayJson (OutputInterface $ output , array $ filesInfo, array $ deprecations = [] ): int
211
223
{
212
224
$ errors = 0 ;
213
225
226
+ $ filesInfo = array_merge ($ filesInfo , $ deprecations );
227
+
214
228
array_walk ($ filesInfo , function (&$ v ) use (&$ errors ) {
215
229
$ v ['file ' ] = (string ) $ v ['file ' ];
216
230
unset($ v ['template ' ]);
@@ -226,6 +240,21 @@ private function displayJson(OutputInterface $output, array $filesInfo): int
226
240
return min ($ errors , 1 );
227
241
}
228
242
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
+
229
258
private function renderException (SymfonyStyle $ output , string $ template , Error $ exception , ?string $ file = null , ?GithubActionReporter $ githubReporter = null ): void
230
259
{
231
260
$ line = $ exception ->getTemplateLine ();
0 commit comments