19
19
class DeprecationErrorHandler
20
20
{
21
21
const MODE_WEAK = 'weak ' ;
22
+ const MODE_WEAK_VENDORS = 'weak_vendors ' ;
22
23
const MODE_DISABLED = 'disabled ' ;
23
24
24
25
private static $ isRegistered = false ;
@@ -28,6 +29,7 @@ class DeprecationErrorHandler
28
29
*
29
30
* The following reporting modes are supported:
30
31
* - use "weak" to hide the deprecation report but keep a global count;
32
+ * - use "weak_vendors" to act as "weak" but only for vendors;
31
33
* - use "/some-regexp/" to stop the test suite whenever a deprecation
32
34
* message matches the given regular expression;
33
35
* - use a number to define the upper bound of allowed deprecations,
@@ -52,13 +54,37 @@ public static function register($mode = 0)
52
54
if (false === $ mode ) {
53
55
$ mode = getenv ('SYMFONY_DEPRECATIONS_HELPER ' );
54
56
}
55
- if (DeprecationErrorHandler::MODE_WEAK !== $ mode && (!isset ($ mode [0 ]) || '/ ' !== $ mode [0 ])) {
57
+ if (DeprecationErrorHandler::MODE_WEAK !== $ mode && DeprecationErrorHandler:: MODE_WEAK_VENDORS !== $ mode && (!isset ($ mode [0 ]) || '/ ' !== $ mode [0 ])) {
56
58
$ mode = preg_match ('/^[1-9][0-9]*$/ ' , $ mode ) ? (int ) $ mode : 0 ;
57
59
}
58
60
59
61
return $ memoizedMode = $ mode ;
60
62
};
61
63
64
+ $ inVendors = function ($ path ) {
65
+ /** @var string[] absolute paths to vendor directories */
66
+ static $ vendors ;
67
+ if (null === $ vendors ) {
68
+ foreach (get_declared_classes () as $ class ) {
69
+ if ('C ' === $ class [0 ] && 0 === strpos ($ class , 'ComposerAutoloaderInit ' )) {
70
+ $ r = new \ReflectionClass ($ class );
71
+ $ v = dirname (dirname ($ r ->getFileName ()));
72
+ if (file_exists ($ v .'/composer/installed.json ' )) {
73
+ $ vendors [] = $ v ;
74
+ }
75
+ }
76
+ }
77
+ }
78
+ $ path = realpath ($ path ) ?: $ path ;
79
+ foreach ($ vendors as $ vendor ) {
80
+ if (0 === strpos ($ path , $ vendor ) && false !== strpbrk (substr ($ path , strlen ($ vendor ), 1 ), '/ ' .DIRECTORY_SEPARATOR )) {
81
+ return true ;
82
+ }
83
+ }
84
+
85
+ return false ;
86
+ };
87
+
62
88
$ deprecations = array (
63
89
'unsilencedCount ' => 0 ,
64
90
'remainingCount ' => 0 ,
@@ -69,7 +95,13 @@ public static function register($mode = 0)
69
95
'legacy ' => array (),
70
96
'other ' => array (),
71
97
);
72
- $ deprecationHandler = function ($ type , $ msg , $ file , $ line , $ context ) use (&$ deprecations , $ getMode , $ UtilPrefix ) {
98
+ if (self ::MODE_WEAK_VENDORS === $ mode ) {
99
+ $ deprecations += array (
100
+ 'remaining vendorCount ' => 0 ,
101
+ 'remaining vendor ' => array (),
102
+ );
103
+ }
104
+ $ deprecationHandler = function ($ type , $ msg , $ file , $ line , $ context ) use (&$ deprecations , $ getMode , $ UtilPrefix , $ inVendors ) {
73
105
$ mode = $ getMode ();
74
106
if ((E_USER_DEPRECATED !== $ type && E_DEPRECATED !== $ type ) || DeprecationErrorHandler::MODE_DISABLED === $ mode ) {
75
107
$ ErrorHandler = $ UtilPrefix .'ErrorHandler ' ;
@@ -80,6 +112,8 @@ public static function register($mode = 0)
80
112
$ trace = debug_backtrace (true );
81
113
$ group = 'other ' ;
82
114
115
+ $ isWeak = DeprecationErrorHandler::MODE_WEAK === $ mode || (DeprecationErrorHandler::MODE_WEAK_VENDORS === $ mode && $ isVendor = $ inVendors ($ file ));
116
+
83
117
$ i = count ($ trace );
84
118
while (1 < $ i && (!isset ($ trace [--$ i ]['class ' ]) || ('ReflectionMethod ' === $ trace [$ i ]['class ' ] || 0 === strpos ($ trace [$ i ]['class ' ], 'PHPUnit_ ' ) || 0 === strpos ($ trace [$ i ]['class ' ], 'PHPUnit \\' )))) {
85
119
// No-op
@@ -99,6 +133,8 @@ public static function register($mode = 0)
99
133
|| in_array ('legacy ' , $ Test ::getGroups ($ class , $ method ), true )
100
134
) {
101
135
$ group = 'legacy ' ;
136
+ } elseif (DeprecationErrorHandler::MODE_WEAK_VENDORS === $ mode && $ isVendor ) {
137
+ $ group = 'remaining vendor ' ;
102
138
} else {
103
139
$ group = 'remaining ' ;
104
140
}
@@ -117,13 +153,13 @@ public static function register($mode = 0)
117
153
118
154
exit (1 );
119
155
}
120
- if ('legacy ' !== $ group && DeprecationErrorHandler:: MODE_WEAK !== $ mode ) {
156
+ if ('legacy ' !== $ group && ! $ isWeak ) {
121
157
$ ref = &$ deprecations [$ group ][$ msg ]['count ' ];
122
158
++$ ref ;
123
159
$ ref = &$ deprecations [$ group ][$ msg ][$ class .':: ' .$ method ];
124
160
++$ ref ;
125
161
}
126
- } elseif (DeprecationErrorHandler:: MODE_WEAK !== $ mode ) {
162
+ } elseif (! $ isWeak ) {
127
163
$ ref = &$ deprecations [$ group ][$ msg ]['count ' ];
128
164
++$ ref ;
129
165
}
@@ -167,9 +203,18 @@ public static function register($mode = 0)
167
203
return $ b ['count ' ] - $ a ['count ' ];
168
204
};
169
205
170
- foreach (array ('unsilenced ' , 'remaining ' , 'legacy ' , 'other ' ) as $ group ) {
206
+ $ groups = array ('unsilenced ' , 'remaining ' );
207
+ if (DeprecationErrorHandler::MODE_WEAK_VENDORS === $ mode ) {
208
+ $ groups [] = 'remaining vendor ' ;
209
+ }
210
+ array_push ($ groups , 'legacy ' , 'other ' );
211
+
212
+ foreach ($ groups as $ group ) {
171
213
if ($ deprecations [$ group .'Count ' ]) {
172
- echo "\n" , $ colorize (sprintf ('%s deprecation notices (%d) ' , ucfirst ($ group ), $ deprecations [$ group .'Count ' ]), 'legacy ' !== $ group ), "\n" ;
214
+ echo "\n" , $ colorize (
215
+ sprintf ('%s deprecation notices (%d) ' , ucfirst ($ group ), $ deprecations [$ group .'Count ' ]),
216
+ 'legacy ' !== $ group && 'remaining vendor ' !== $ group
217
+ ), "\n" ;
173
218
174
219
uasort ($ deprecations [$ group ], $ cmp );
175
220
0 commit comments