@@ -88,39 +88,9 @@ 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- }
11391
11492 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 )]);
12494 }
12595
12696 if (!$ filenames ) {
@@ -138,23 +108,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
138108 }
139109 }
140110
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 ));
150112 }
151113
152- private function getFilesInfo (array $ filenames ): array
114+ private function getFilesInfo (array $ filenames, bool $ showDeprecations ): array
153115 {
154116 $ filesInfo = [];
155117 foreach ($ filenames as $ filename ) {
156118 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 );
158120 }
159121 }
160122
@@ -172,8 +134,26 @@ protected function findFiles(string $filename): iterable
172134 throw new RuntimeException (\sprintf ('File or directory "%s" is not readable. ' , $ filename ));
173135 }
174136
175- private function validate (string $ template , string $ file ): array
137+ private function validate (string $ template , string $ file, bool $ collectDeprecation ): array
176138 {
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+
177157 $ realLoader = $ this ->twig ->getLoader ();
178158 try {
179159 $ temporaryLoader = new ArrayLoader ([$ file => $ template ]);
@@ -185,28 +165,33 @@ private function validate(string $template, string $file): array
185165 $ this ->twig ->setLoader ($ realLoader );
186166
187167 return ['template ' => $ template , 'file ' => $ file , 'line ' => $ e ->getTemplateLine (), 'valid ' => false , 'exception ' => $ e ];
168+ } finally {
169+ if ($ collectDeprecation ) {
170+ restore_error_handler ();
171+ }
188172 }
189173
190- return ['template ' => $ template , 'file ' => $ file , 'valid ' => true ];
174+ return ['template ' => $ template , 'file ' => $ file , 'deprecations ' => $ deprecations , ' valid ' => true ];
191175 }
192176
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
194178 {
195179 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 ),
199183 default => throw new InvalidArgumentException (\sprintf ('Supported formats are "%s". ' , implode ('", " ' , $ this ->getAvailableFormatOptions ()))),
200184 };
201185 }
202186
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
204188 {
205189 $ errors = 0 ;
206190 $ githubReporter = $ errorAsGithubAnnotations ? new GithubActionReporter ($ output ) : null ;
191+ $ deprecations = array_merge (...array_column ($ filesInfo , 'deprecations ' ));
207192
208193 foreach ($ deprecations as $ deprecation ) {
209- $ this ->renderDeprecation ($ io , $ deprecation ['exception ' ], $ deprecation ['file ' ], $ githubReporter );
194+ $ this ->renderDeprecation ($ io , $ deprecation ['line ' ], $ deprecation [ ' message ' ], $ deprecation ['file ' ], $ githubReporter );
210195 }
211196
212197 foreach ($ filesInfo as $ info ) {
@@ -227,12 +212,10 @@ private function displayTxt(OutputInterface $output, SymfonyStyle $io, array $fi
227212 return 0 === count ($ deprecations ) ? min ($ errors , 1 ) : 1 ;
228213 }
229214
230- private function displayJson (OutputInterface $ output , array $ filesInfo, array $ deprecations ): int
215+ private function displayJson (OutputInterface $ output , array $ filesInfo ): int
231216 {
232217 $ errors = 0 ;
233218
234- $ filesInfo = array_merge ($ filesInfo , $ deprecations );
235-
236219 array_walk ($ filesInfo , function (&$ v ) use (&$ errors ) {
237220 $ v ['file ' ] = (string ) $ v ['file ' ];
238221 unset($ v ['template ' ]);
@@ -248,19 +231,17 @@ private function displayJson(OutputInterface $output, array $filesInfo, array $d
248231 return min ($ errors , 1 );
249232 }
250233
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
252235 {
253- $ line = $ exception ->getTemplateLine ();
254-
255- $ githubReporter ?->error($ exception ->getRawMessage (), $ file , $ line <= 0 ? null : $ line );
236+ $ githubReporter ?->error($ message , $ file , $ line <= 0 ? null : $ line );
256237
257238 if ($ file ) {
258239 $ output ->text (\sprintf ('<info> DEPRECATION </info> in %s (line %s) ' , $ file , $ line ));
259240 } else {
260241 $ output ->text (\sprintf ('<info> DEPRECATION </info> (line %s) ' , $ line ));
261242 }
262243
263- $ output ->text (\sprintf ('<info> >> %s</info> ' , $ exception -> getRawMessage () ));
244+ $ output ->text (\sprintf ('<info> >> %s</info> ' , $ message ));
264245 }
265246
266247 private function renderException (SymfonyStyle $ output , string $ template , Error $ exception , ?string $ file = null , ?GithubActionReporter $ githubReporter = null ): void
0 commit comments