@@ -107,15 +107,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
107107 }
108108 }
109109
110+ $ deprecations = [];
110111 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 ) {
112113 if (\E_USER_DEPRECATED === $ level ) {
113114 $ templateLine = 0 ;
114115 if (preg_match ('/ at line (\d+)[ .]/ ' , $ message , $ matches )) {
115116 $ templateLine = $ matches [1 ];
116117 }
117118
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 ;
119127 }
120128
121129 return $ prevErrorHandler ? $ prevErrorHandler ($ level , $ message , $ file , $ line ) : false ;
@@ -130,7 +138,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
130138 }
131139 }
132140
133- return $ this ->display ($ input , $ output , $ io , $ filesInfo );
141+ return $ this ->display ($ input , $ output , $ io , $ filesInfo, $ deprecations );
134142 }
135143
136144 private function getFilesInfo (array $ filenames ): array
@@ -174,21 +182,25 @@ private function validate(string $template, string $file): array
174182 return ['template ' => $ template , 'file ' => $ file , 'valid ' => true ];
175183 }
176184
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
178186 {
179187 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 ),
183191 default => throw new InvalidArgumentException (\sprintf ('Supported formats are "%s". ' , implode ('", " ' , $ this ->getAvailableFormatOptions ()))),
184192 };
185193 }
186194
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
188196 {
189197 $ errors = 0 ;
190198 $ githubReporter = $ errorAsGithubAnnotations ? new GithubActionReporter ($ output ) : null ;
191199
200+ foreach ($ deprecations as $ deprecation ) {
201+ $ this ->renderDeprecation ($ io , $ deprecation ['exception ' ], $ deprecation ['file ' ]);
202+ }
203+
192204 foreach ($ filesInfo as $ info ) {
193205 if ($ info ['valid ' ] && $ output ->isVerbose ()) {
194206 $ 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
204216 $ io ->warning (\sprintf ('%d Twig files have valid syntax and %d contain errors. ' , \count ($ filesInfo ) - $ errors , $ errors ));
205217 }
206218
207- return min ($ errors , 1 );
219+ return 0 === count ( $ deprecations ) ? min ($ errors , 1 ) : 1 ;
208220 }
209221
210- private function displayJson (OutputInterface $ output , array $ filesInfo ): int
222+ private function displayJson (OutputInterface $ output , array $ filesInfo, array $ deprecations = [] ): int
211223 {
212224 $ errors = 0 ;
213225
226+ $ filesInfo = array_merge ($ filesInfo , $ deprecations );
227+
214228 array_walk ($ filesInfo , function (&$ v ) use (&$ errors ) {
215229 $ v ['file ' ] = (string ) $ v ['file ' ];
216230 unset($ v ['template ' ]);
@@ -226,6 +240,21 @@ private function displayJson(OutputInterface $output, array $filesInfo): int
226240 return min ($ errors , 1 );
227241 }
228242
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+
229258 private function renderException (SymfonyStyle $ output , string $ template , Error $ exception , ?string $ file = null , ?GithubActionReporter $ githubReporter = null ): void
230259 {
231260 $ line = $ exception ->getTemplateLine ();
0 commit comments