@@ -88,8 +88,39 @@ 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 (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+
91114 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 );
93124 }
94125
95126 if (!$ filenames ) {
@@ -107,21 +138,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
107138 }
108139 }
109140
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-
125141 try {
126142 $ filesInfo = $ this ->getFilesInfo ($ filenames );
127143 } finally {
@@ -130,7 +146,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
130146 }
131147 }
132148
133- return $ this ->display ($ input , $ output , $ io , $ filesInfo );
149+ return $ this ->display ($ input , $ output , $ io , $ filesInfo, $ deprecations );
134150 }
135151
136152 private function getFilesInfo (array $ filenames ): array
@@ -174,21 +190,25 @@ private function validate(string $template, string $file): array
174190 return ['template ' => $ template , 'file ' => $ file , 'valid ' => true ];
175191 }
176192
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
178194 {
179195 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 ),
183199 default => throw new InvalidArgumentException (\sprintf ('Supported formats are "%s". ' , implode ('", " ' , $ this ->getAvailableFormatOptions ()))),
184200 };
185201 }
186202
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
188204 {
189205 $ errors = 0 ;
190206 $ githubReporter = $ errorAsGithubAnnotations ? new GithubActionReporter ($ output ) : null ;
191207
208+ foreach ($ deprecations as $ deprecation ) {
209+ $ this ->renderDeprecation ($ io , $ deprecation ['exception ' ], $ deprecation ['file ' ], $ githubReporter );
210+ }
211+
192212 foreach ($ filesInfo as $ info ) {
193213 if ($ info ['valid ' ] && $ output ->isVerbose ()) {
194214 $ 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
204224 $ io ->warning (\sprintf ('%d Twig files have valid syntax and %d contain errors. ' , \count ($ filesInfo ) - $ errors , $ errors ));
205225 }
206226
207- return min ($ errors , 1 );
227+ return 0 === count ( $ deprecations ) ? min ($ errors , 1 ) : 1 ;
208228 }
209229
210- private function displayJson (OutputInterface $ output , array $ filesInfo ): int
230+ private function displayJson (OutputInterface $ output , array $ filesInfo, array $ deprecations = [] ): int
211231 {
212232 $ errors = 0 ;
213233
234+ $ filesInfo = array_merge ($ filesInfo , $ deprecations );
235+
214236 array_walk ($ filesInfo , function (&$ v ) use (&$ errors ) {
215237 $ v ['file ' ] = (string ) $ v ['file ' ];
216238 unset($ v ['template ' ]);
@@ -226,6 +248,21 @@ private function displayJson(OutputInterface $output, array $filesInfo): int
226248 return min ($ errors , 1 );
227249 }
228250
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+
229266 private function renderException (SymfonyStyle $ output , string $ template , Error $ exception , ?string $ file = null , ?GithubActionReporter $ githubReporter = null ): void
230267 {
231268 $ line = $ exception ->getTemplateLine ();
0 commit comments