@@ -88,8 +88,39 @@ 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
if (['- ' ] === $ filenames ) {
92
- return $ this ->display ($ input , $ output , $ io , [$ this ->validate (file_get_contents ('php://stdin ' ), 'Standard Input ' )]);
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
124
}
94
125
95
126
if (!$ filenames ) {
@@ -107,21 +138,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
107
138
}
108
139
}
109
140
110
- if ($ showDeprecations ) {
111
- $ prevErrorHandler = set_error_handler (static function ($ level , $ message , $ file , $ line ) use (&$ prevErrorHandler ) {
112
- if (\E_USER_DEPRECATED === $ level ) {
113
- $ templateLine = 0 ;
114
- if (preg_match ('/ at line (\d+)[ .]/ ' , $ message , $ matches )) {
115
- $ templateLine = $ matches [1 ];
116
- }
117
-
118
- throw new Error ($ message , $ templateLine );
119
- }
120
-
121
- return $ prevErrorHandler ? $ prevErrorHandler ($ level , $ message , $ file , $ line ) : false ;
122
- });
123
- }
124
-
125
141
try {
126
142
$ filesInfo = $ this ->getFilesInfo ($ filenames );
127
143
} finally {
@@ -130,7 +146,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
130
146
}
131
147
}
132
148
133
- return $ this ->display ($ input , $ output , $ io , $ filesInfo );
149
+ return $ this ->display ($ input , $ output , $ io , $ filesInfo, $ deprecations );
134
150
}
135
151
136
152
private function getFilesInfo (array $ filenames ): array
@@ -174,21 +190,25 @@ private function validate(string $template, string $file): array
174
190
return ['template ' => $ template , 'file ' => $ file , 'valid ' => true ];
175
191
}
176
192
177
- private function display (InputInterface $ input , OutputInterface $ output , SymfonyStyle $ io , array $ files ): int
193
+ private function display (InputInterface $ input , OutputInterface $ output , SymfonyStyle $ io , array $ files, array $ deprecations ): int
178
194
{
179
195
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 ),
196
+ 'txt ' => $ this ->displayTxt ($ output , $ io , $ files, $ deprecations ),
197
+ 'json ' => $ this ->displayJson ($ output , $ files, $ deprecations ),
198
+ 'github ' => $ this ->displayTxt ($ output , $ io , $ files , $ deprecations , true ),
183
199
default => throw new InvalidArgumentException (\sprintf ('Supported formats are "%s". ' , implode ('", " ' , $ this ->getAvailableFormatOptions ()))),
184
200
};
185
201
}
186
202
187
- private function displayTxt (OutputInterface $ output , SymfonyStyle $ io , array $ filesInfo , bool $ errorAsGithubAnnotations = false ): int
203
+ private function displayTxt (OutputInterface $ output , SymfonyStyle $ io , array $ filesInfo , array $ deprecations , bool $ errorAsGithubAnnotations = false ): int
188
204
{
189
205
$ errors = 0 ;
190
206
$ githubReporter = $ errorAsGithubAnnotations ? new GithubActionReporter ($ output ) : null ;
191
207
208
+ foreach ($ deprecations as $ deprecation ) {
209
+ $ this ->renderDeprecation ($ io , $ deprecation ['exception ' ], $ deprecation ['file ' ], $ githubReporter );
210
+ }
211
+
192
212
foreach ($ filesInfo as $ info ) {
193
213
if ($ info ['valid ' ] && $ output ->isVerbose ()) {
194
214
$ io ->comment ('<info>OK</info> ' .($ info ['file ' ] ? \sprintf (' in %s ' , $ info ['file ' ]) : '' ));
@@ -204,13 +224,15 @@ private function displayTxt(OutputInterface $output, SymfonyStyle $io, array $fi
204
224
$ io ->warning (\sprintf ('%d Twig files have valid syntax and %d contain errors. ' , \count ($ filesInfo ) - $ errors , $ errors ));
205
225
}
206
226
207
- return min ($ errors , 1 );
227
+ return 0 === count ( $ deprecations ) ? min ($ errors , 1 ) : 1 ;
208
228
}
209
229
210
- private function displayJson (OutputInterface $ output , array $ filesInfo ): int
230
+ private function displayJson (OutputInterface $ output , array $ filesInfo, array $ deprecations = [] ): int
211
231
{
212
232
$ errors = 0 ;
213
233
234
+ $ filesInfo = array_merge ($ filesInfo , $ deprecations );
235
+
214
236
array_walk ($ filesInfo , function (&$ v ) use (&$ errors ) {
215
237
$ v ['file
10000
' ] = (string ) $ v ['file ' ];
216
238
unset($ v ['template ' ]);
@@ -226,6 +248,21 @@ private function displayJson(OutputInterface $output, array $filesInfo): int
226
248
return min ($ errors , 1 );
227
249
}
228
250
251
+ private function renderDeprecation (SymfonyStyle $ output , Error $ exception , string $ file , ?GithubActionReporter $ githubReporter ): void
252
+ {
253
+ $ line = $ exception ->getTemplateLine ();
254
+
255
+ $ githubReporter ?->error($ exception ->getRawMessage (), $ file , $ line <= 0 ? null : $ line );
256
+
257
+ if ($ file ) {
258
+ $ output ->text (\sprintf ('<info> DEPRECATION </info> in %s (line %s) ' , $ file , $ line ));
259
+ } else {
260
+ $ output ->text (\sprintf ('<info> DEPRECATION </info> (line %s) ' , $ line ));
261
+ }
262
+
263
+ $ output ->text (\sprintf ('<info> >> %s</info> ' , $ exception ->getRawMessage ()));
264
+ }
265
+
229
266
private function renderException (SymfonyStyle $ output , string $ template , Error $ exception , ?string $ file = null , ?GithubActionReporter $ githubReporter = null ): void
230
267
{
231
268
$ line = $ exception ->getTemplateLine ();
0 commit comments