1515use PHPUnit \Util \ErrorHandler ;
1616use Symfony \Bridge \PhpUnit \DeprecationErrorHandler \Configuration ;
1717use Symfony \Bridge \PhpUnit \DeprecationErrorHandler \Deprecation ;
18+ use Symfony \Bridge \PhpUnit \DeprecationErrorHandler \DeprecationGroup ;
1819use Symfony \Component \ErrorHandler \DebugClassLoader ;
1920
2021/**
@@ -30,24 +31,20 @@ class DeprecationErrorHandler
3031
3132 private $ mode ;
3233 private $ configuration ;
33- private $ deprecations = [
34- 'unsilencedCount ' => 0 ,
35- 'remaining selfCount ' => 0 ,
36- 'legacyCount ' => 0 ,
37- 'otherCount ' => 0 ,
38- 'remaining directCount ' => 0 ,
39- 'remaining indirectCount ' => 0 ,
40- 'unsilenced ' => [],
41- 'remaining self ' => [],
42- 'legacy ' => [],
43- 'other ' => [],
44- 'remaining direct ' => [],
45- 'remaining indirect ' => [],
46- ];
34+
35+ /**
36+ * @var DeprecationGroup[]
37+ */
38+ private $ deprecationGroups = [];
4739
4840 private static $ isRegistered = false ;
4941 private static $ isAtLeastPhpUnit83 ;
5042
43+ public function __construct ()
44+ {
45+ $ this ->resetDeprecationGroups ();
46+ }
47+
5148 /**
5249 * Registers and configures the deprecation handler.
5350 *
@@ -135,9 +132,9 @@ public function handleError($type, $msg, $file, $line, $context = [])
135132 $ group = 'legacy ' ;
136133 } else {
137134 $ group = [
138- Deprecation::TYPE_SELF => 'remaining self ' ,
139- Deprecation::TYPE_DIRECT => 'remaining direct ' ,
140- Deprecation::TYPE_INDIRECT => 'remaining indirect ' ,
135+ Deprecation::TYPE_SELF => 'self ' ,
136+ Deprecation::TYPE_DIRECT => 'direct ' ,
137+ Deprecation::TYPE_INDIRECT => 'indirect ' ,
141138 Deprecation::TYPE_UNDETERMINED => 'other ' ,
142139 ][$ deprecation ->getType ()];
143140 }
@@ -148,18 +145,14 @@ public function handleError($type, $msg, $file, $line, $context = [])
148145 exit (1 );
149146 }
150147 if ('legacy ' !== $ group ) {
151- $ ref = &$ this ->deprecations [$ group ][$ msg ]['count ' ];
152- ++$ ref ;
153- $ ref = &$ this ->deprecations [$ group ][$ msg ][$ class .':: ' .$ method ];
154- ++$ ref ;
148+ $ this ->deprecationGroups [$ group ]->addNoticeFromObject ($ msg , $ class , $ method );
149+ } else {
150+ $ this ->deprecationGroups [$ group ]->addNotice ();
155151 }
156152 } else {
157- $ ref = &$ this ->deprecations [$ group ][$ msg ]['count ' ];
158- ++$ ref ;
153+ $ this ->deprecationGroups [$ group ]->addNoticeFromProceduralCode ($ msg );
159154 }
160155
161- ++$ this ->deprecations [$ group .'Count ' ];
162-
163156 return null ;
164157 }
165158
@@ -184,34 +177,44 @@ public function shutdown()
184177 echo "\n" , self ::colorize ('THE ERROR HANDLER HAS CHANGED! ' , true ), "\n" ;
185178 }
186179
187- $ groups = ['unsilenced ' , 'remaining self ' , 'remaining direct ' , 'remaining indirect ' , 'legacy ' , 'other ' ];
188-
189- $ this ->displayDeprecations ($ groups , $ configuration );
180+ $ groups = array_keys ($ this ->deprecationGroups );
190181
191182 // store failing status
192- $ isFailing = !$ configuration ->tolerates ($ this ->deprecations );
183+ $ isFailing = !$ configuration ->tolerates ($ this ->deprecationGroups );
193184
194- // reset deprecations array
195- foreach ($ this ->deprecations as $ group => $ arrayOrInt ) {
196- $ this ->deprecations [$ group ] = \is_int ($ arrayOrInt ) ? 0 : [];
197- }
185+ $ this ->displayDeprecations ($ groups , $ configuration , $ isFailing );
186+
187+ $ this ->resetDeprecationGroups ();
198188
199189 register_shutdown_function (function () use ($ isFailing , $ groups , $ configuration ) {
200- foreach ($ this ->deprecations as $ group => $ arrayOrInt ) {
201- if (0 < ( \is_int ( $ arrayOrInt ) ? $ arrayOrInt : \ count ($ arrayOrInt )) ) {
190+ foreach ($ this ->deprecationGroups as $ group ) {
191+ if ($ group -> count () > 0 ) {
202192 echo "Shutdown-time deprecations: \n" ;
203193 break ;
204194 }
205195 }
206196
207- $ this ->displayDeprecations ($ groups , $ configuration );
197+ $ isFailingAtShutdown = !$ configuration ->tolerates ($ this ->deprecationGroups );
198+ $ this ->displayDeprecations ($ groups , $ configuration , $ isFailingAtShutdown );
208199
209- if ($ isFailing || ! $ configuration -> tolerates ( $ this -> deprecations ) ) {
200+ if ($ isFailing || $ isFailingAtShutdown ) {
210201 exit (1 );
211202 }
212203 });
213204 }
214205
206+ private function resetDeprecationGroups ()
207+ {
208+ $ this ->deprecationGroups = [
209+ 'unsilenced ' => new DeprecationGroup (),
210+ 'self ' => new DeprecationGroup (),
211+ 'direct ' => new DeprecationGroup (),
212+ 'indirect ' => new DeprecationGroup (),
213+ 'legacy ' => new DeprecationGroup (),
214+ 'other ' => new DeprecationGroup (),
215+ ];
216+ }
217+
215218 private function getConfiguration ()
216219 {
217220 if (null !== $ this ->configuration ) {
@@ -270,31 +273,38 @@ private static function colorize($str, $red)
270273 /**
271274 * @param string[] $groups
272275 * @param Configuration $configuration
276+ * @param bool $isFailing
273277 */
274- private function displayDeprecations ($ groups , $ configuration )
278+ private function displayDeprecations ($ groups , $ configuration, $ isFailing )
275279 {
276280 $ cmp = function ($ a , $ b ) {
277- return $ b[ ' count ' ] - $ a[ ' count ' ] ;
281+ return $ b-> count () - $ a-> count () ;
278282 };
279283
280284 foreach ($ groups as $ group ) {
281- if ($ this ->deprecations [$ group. ' Count ' ] ) {
285+ if ($ this ->deprecationGroups [$ group]-> count () ) {
282286 echo "\n" , self ::colorize (
283- sprintf ('%s deprecation notices (%d) ' , ucfirst ($ group ), $ this ->deprecations [$ group .'Count ' ]),
284- 'legacy ' !== $ group && 'remaining indirect ' !== $ group
287+ sprintf (
288+ '%s deprecation notices (%d) ' ,
289+ \in_array ($ group , ['direct ' , 'indirect ' , 'self ' ], true ) ? "Remaining $ group " : ucfirst ($ group ),
290+ $ this ->deprecationGroups [$ group ]->count ()
291+ ),
292+ 'legacy ' !== $ group && 'indirect ' !== $ group
285293 ), "\n" ;
286294
287- if (! $ configuration ->verboseOutput () ) {
295+ if (' legacy ' !== $ group && ! $ configuration ->verboseOutput ($ group ) && ! $ isFailing ) {
288296 continue ;
289297 }
290- uasort ($ this ->deprecations [$ group ], $ cmp );
298+ $ notices = $ this ->deprecationGroups [$ group ]->notices ();
299+ uasort ($ notices , $ cmp );
291300
292- foreach ($ this -> deprecations [ $ group ] as $ msg => $ notices ) {
293- echo "\n " , $ notices [ ' count ' ] , 'x: ' , $ msg , "\n" ;
301+ foreach ($ notices as $ msg => $ notice ) {
302+ echo "\n " , $ notice -> count () , 'x: ' , $ msg , "\n" ;
294303
295- arsort ($ notices );
304+ $ countsByCaller = $ notice ->getCountsByCaller ();
305+ arsort ($ countsByCaller );
296306
297- foreach ($ notices as $ method => $ count ) {
307+ foreach ($ countsByCaller as $ method => $ count ) {
298308 if ('count ' !== $ method ) {
299309 echo ' ' , $ count , 'x in ' , preg_replace ('/(.*) \\\\(.*?::.*?)$/ ' , '$2 from $1 ' , $ method ), "\n" ;
300310 }
0 commit comments