@@ -89,7 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
89
89
$ this ->format = $ input ->getOption ('format ' ) ?? (GithubActionReporter::isGithubActionEnvironment () ? 'github ' : 'txt ' );
90
90
91
91
if (['- ' ] === $ filenames ) {
92
- return $ this ->display ($ input , $ output , $ io , [$ this ->validate (file_get_contents ('php://stdin ' ), 'Standard Input ' )]);
92
+ return $ this ->display ($ input , $ output , $ io , [$ this ->validate (file_get_contents ('php://stdin ' ), 'Standard Input ' , $ showDeprecations )]);
93
93
}
94
94
95
95
if (!$ filenames ) {
@@ -107,38 +107,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
107
107
}
108
108
}
109
109
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
- try {
126
- $ filesInfo = $ this ->getFilesInfo ($ filenames );
127
- } finally {
128
- if ($ showDeprecations ) {
129
- restore_error_handler ();
130
- }
131
- }
132
-
133
- return $ this ->display ($ input , $ output , $ io , $ filesInfo );
110
+ return $ this ->display ($ input , $ output , $ io , $ this ->getFilesInfo ($ filenames , $ showDeprecations ));
134
111
}
135
112
136
- private function getFilesInfo (array $ filenames ): array
113
+ private function getFilesInfo (array $ filenames, bool $ showDeprecations ): array
137
114
{
138
115
$ filesInfo = [];
139
116
foreach ($ filenames as $ filename ) {
140
117
foreach ($ this ->findFiles ($ filename ) as $ file ) {
141
- $ filesInfo [] = $ this ->validate (file_get_contents ($ file ), $ file );
118
+ $ filesInfo [] = $ this ->validate (file_get_contents ($ file ), $ file, $ showDeprecations );
142
119
}
143
120
}
144
121
@@ -156,8 +133,26 @@ protected function findFiles(string $filename): iterable
156
133
throw new RuntimeException (\sprintf ('File or directory "%s" is not readable. ' , $ filename ));
157
134
}
158
135
159
- private function validate (string $ template , string $ file ): array
136
+ private function validate (string $ template , string $ file, bool $ collectDeprecation ): array
160
137
{
138
+ $ deprecations = [];
139
+ if ($ collectDeprecation ) {
140
+ $ prevErrorHandler = set_error_handler (static function ($ level , $ message , $ fileName , $ line ) use (&$ prevErrorHandler , &$ deprecations , $ file ) {
141
+ if (\E_USER_DEPRECATED === $ level ) {
142
+ $ templateLine = 0 ;
143
+ if (preg_match ('/ at line (\d+)[ .]/ ' , $ message , $ matches )) {
144
+ $ templateLine = $ matches [1 ];
145
+ }
146
+
147
+ $ deprecations [] = ['message ' => $ message , 'file ' => $ file , 'line ' => $ templateLine ];
148
+
149
+ return true ;
150
+ }
151
+
152
+ return $ prevErrorHandler ? $ prevErrorHandler ($ level , $ message , $ fileName , $ line ) : false ;
153
+ });
154
+ }
155
+
161
156
$ realLoader = $ this ->twig ->getLoader ();
162
157
try {
163
158
$ temporaryLoader = new ArrayLoader ([$ file => $ template ]);
@@ -169,9 +164,13 @@ private function validate(string $template, string $file): array
169
164
$ this ->twig ->setLoader ($ realLoader );
170
165
171
166
return ['template ' => $ template , 'file ' => $ file , 'line ' => $ e ->getTemplateLine (), 'valid ' => false , 'exception ' => $ e ];
167
+ } finally {
168
+ if ($ collectDeprecation ) {
169
+ restore_error_handler ();
170
+ }
172
171
}
173
172
174
- return ['template ' => $ template , 'file ' => $ file , 'valid ' => true ];
173
+ return ['template ' => $ template , 'file ' => $ file , 'deprecations ' => $ deprecations , ' valid ' => true ];
175
174
}
176
175
177
176
private function display (InputInterface $ input , OutputInterface $ output , SymfonyStyle $ io , array $ files ): int
@@ -188,6 +187,11 @@ private function displayTxt(OutputInterface $output, SymfonyStyle $io, array $fi
188
187
{
189
188
$ errors = 0 ;
190
189
$ githubReporter = $ errorAsGithubAnnotations ? new GithubActionReporter ($ output ) : null ;
190
+ $ deprecations = array_merge (...array_column ($ filesInfo , 'deprecations ' ));
191
+
192
+ foreach ($ deprecations as $ deprecation ) {
193
+ $ this ->renderDeprecation ($ io , $ deprecation ['line ' ], $ deprecation ['message ' ], $ deprecation ['file ' ], $ githubReporter );
194
+ }
191
195
192
196
foreach ($ filesInfo as $ info ) {
193
197
if ($ info ['valid ' ] && $ output ->isVerbose ()) {
@@ -204,7 +208,7 @@ private function displayTxt(OutputInterface $output, SymfonyStyle $io, array $fi
204
208
$ io ->warning (\sprintf ('%d Twig files have valid syntax and %d contain errors. ' , \count ($ filesInfo ) - $ errors , $ errors ));
205
209
}
206
210
207
- return min ( $ errors, 1 ) ;
211
+ return ! $ deprecations && ! $ errors ? 0 : 1 ;
208
212
}
209
213
210
214
private function displayJson (OutputInterface $ output , array $ filesInfo ): int
@@ -226,6 +230,19 @@ private function displayJson(OutputInterface $output, array $filesInfo): int
226
230
return min ($ errors , 1 );
227
231
}
228
232
233
+ private function renderDeprecation (SymfonyStyle $ output , int $ line , string $ message , string $ file , ?GithubActionReporter $ githubReporter ): void
234
+ {
235
+ $ githubReporter ?->error($ message , $ file , $ line <= 0 ? null : $ line );
236
+
237
+ if ($ file ) {
238
+ $ output ->text (\sprintf ('<info> DEPRECATION </info> in %s (line %s) ' , $ file , $ line ));
239
+ } else {
240
+ $ output ->text (\sprintf ('<info> DEPRECATION </info> (line %s) ' , $ line ));
241
+ }
242
+
243
+ $ output ->text (\sprintf ('<info> >> %s</info> ' , $ message ));
244
+ }
245
+
229
246
private function renderException (SymfonyStyle $ output , string $ template , Error $ exception , ?string $ file = null , ?GithubActionReporter $ githubReporter = null ): void
230
247
{
231
248
$ line = $ exception ->getTemplateLine ();
0 commit comments