18
18
*/
19
19
class Deprecation
20
20
{
21
- const PATH_TYPE_VENDOR = 'path_type_vendor ' ;
22
- const PATH_TYPE_SELF = 'path_type_internal ' ;
23
- const PATH_TYPE_UNDETERMINED = 'path_type_undetermined ' ;
24
-
25
- const TYPE_SELF = 'type_self ' ;
26
- const TYPE_DIRECT = 'type_direct ' ;
27
- const TYPE_INDIRECT = 'type_indirect ' ;
28
- const TYPE_UNDETERMINED = 'type_undetermined ' ;
29
-
30
21
/**
31
22
* @var array
32
23
*/
@@ -48,21 +39,13 @@ class Deprecation
48
39
private $ originMethod ;
49
40
50
41
/**
51
- * @var string one of the PATH_TYPE_* constants
42
+ * @var bool
52
43
*/
53
- private $ triggeringFilePathType ;
44
+ private $ self ;
54
45
55
46
/** @var string[] absolute paths to vendor directories */
56
47
private static $ vendors ;
57
48
58
- /**
59
- * @var string[] absolute paths to source or tests of the project. This
60
- * excludes cache directories, because it is based on
61
- * autoloading rules and cache systems typically do not use
62
- * those.
63
- */
64
- private static $ internalPaths ;
65
-
66
49
/**
67
50
* @param string $message
68
51
* @param string $file
@@ -76,7 +59,7 @@ public function __construct($message, array $trace, $file)
76
59
// No-op
77
60
}
78
61
$ line = $ trace [$ i ];
79
- $ this ->trigerringFilePathType = $ this ->getPathType ($ file );
62
+ $ this ->self = ! $ this ->pathOriginatesFromVendor ($ file );
80
63
if (isset ($ line ['object ' ]) || isset ($ line ['class ' ])) {
81
64
if (isset ($ line ['class ' ]) && 0 === strpos ($ line ['class ' ], SymfonyTestsListenerFor::class)) {
82
65
$ parsedMsg = unserialize ($ this ->message );
@@ -87,9 +70,8 @@ public function __construct($message, array $trace, $file)
87
70
// \Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait::endTest()
88
71
// then we need to use the serialized information to determine
89
72
// if the error has been triggered from vendor code.
90
- if (isset ($ parsedMsg ['triggering_file ' ])) {
91
- $ this ->trigerringFilePathType = $ this ->getPathType ($ parsedMsg ['triggering_file ' ]);
92
- }
73
+ $ this ->self = isset ($ parsedMsg ['triggering_file ' ])
74
+ && $ this ->pathOriginatesFromVendor ($ parsedMsg ['triggering_file ' ]);
93
75
94
76
return ;
95
77
}
@@ -119,6 +101,14 @@ public function originatesFromAnObject()
119
101
return isset ($ this ->originClass );
120
102
}
121
103
104
+ /**
105
+ * @return bool
106
+ */
107
+ public function isSelf ()
108
+ {
109
+ return $ this ->self ;
110
+ }
111
+
122
112
/**
123
113
* @return string
124
114
*/
@@ -173,16 +163,10 @@ public function isLegacy($utilPrefix)
173
163
* Tells whether both the calling package and the called package are vendor
174
164
* packages.
175
165
*
176
- * @return string
166
+ * @return bool
177
167
*/
178
- public function getType ()
168
+ public function isIndirect ()
179
169
{
180
- if (self ::PATH_TYPE_SELF === $ this ->trigerringFilePathType ) {
181
- return self ::TYPE_SELF ;
182
- }
183
- if (self ::PATH_TYPE_UNDETERMINED === $ this ->trigerringFilePathType ) {
184
- return self ::TYPE_UNDETERMINED ;
185
- }
186
170
$ erroringFile = $ erroringPackage = null ;
187
171
foreach ($ this ->trace as $ line ) {
188
172
if (\in_array ($ line ['function ' ], ['require ' , 'require_once ' , 'include ' , 'include_once ' ], true )) {
@@ -195,28 +179,25 @@ public function getType()
195
179
if ('- ' === $ file || 'Standard input code ' === $ file || !realpath ($ file )) {
196
180
continue ;
197
181
}
198
- if (self ::PATH_TYPE_SELF === $ this ->getPathType ($ file )) {
199
- return self ::TYPE_DIRECT ;
200
- }
201
- if (self ::PATH_TYPE_UNDETERMINED === $ this ->getPathType ($ file )) {
202
- return self ::TYPE_UNDETERMINED ;
182
+ if (!$ this ->pathOriginatesFromVendor ($ file )) {
183
+ return false ;
203
184
}
204
185
if (null !== $ erroringFile && null !== $ erroringPackage ) {
205
186
$ package = $ this ->getPackage ($ file );
206
187
if ('composer ' !== $ package && $ package !== $ erroringPackage ) {
207
- return self :: TYPE_INDIRECT ;
188
+ return true ;
208
189
}
209
190
continue ;
210
191
}
211
192
$ erroringFile = $ file ;
212
193
$ erroringPackage = $ this ->getPackage ($ file );
213
194
}
214
195
215
- return self :: TYPE_DIRECT ;
196
+ return false ;
216
197
}
217
198
218
199
/**
219
- * getPathType () should always be called prior to calling this method.
200
+ * pathOriginatesFromVendor () should always be called prior to calling this method.
220
201
*
221
202
* @param string $path
222
203
*
@@ -256,15 +237,6 @@ private static function getVendors()
256
237
$ v = \dirname (\dirname ($ r ->getFileName ()));
257
238
if (file_exists ($ v .'/composer/installed.json ' )) {
258
239
self ::$ vendors [] = $ v ;
259
- $ loader = require $ v .'/autoload.php ' ;
260
- $ paths = self ::getSourcePathsFromPrefixes (array_merge ($ loader ->getPrefixes (), $ loader ->getPrefixesPsr4 ()));
261
- }
262
- }
263
- }
264
- foreach ($ paths as $ path ) {
265
- foreach (self ::$ vendors as $ vendor ) {
266
- if (0 !== strpos ($ path , $ vendor )) {
267
- self ::$ internalPaths [] = $ path ;
268
240
}
269
241
}
270
242
}
@@ -273,41 +245,24 @@ private static function getVendors()
273
245
return self ::$ vendors ;
274
246
}
275
247
276
- private static function getSourcePathsFromPrefixes (array $ prefixesByNamespace )
277
- {
278
- foreach ($ prefixesByNamespace as $ prefixes ) {
279
- foreach ($ prefixes as $ prefix ) {
280
- if (false !== realpath ($ prefix )) {
281
- yield realpath ($ prefix );
282
- }
283
- }
284
- }
285
- }
286
-
287
248
/**
288
249
* @param string $path
289
250
*
290
- * @return string
251
+ * @return bool
291
252
*/
292
- private function getPathType ($ path )
253
+ private function pathOriginatesFromVendor ($ path )
293
254
{
294
255
$ realPath = realpath ($ path );
295
256
if (false === $ realPath && '- ' !== $ path && 'Standard input code ' !== $ path ) {
296
- return self :: PATH_TYPE_UNDETERMINED ;
257
+ return true ;
297
258
}
298
259
foreach (self ::getVendors () as $ vendor ) {
299
260
if (0 === strpos ($ realPath , $ vendor ) && false !== strpbrk (substr ($ realPath , \strlen ($ vendor ), 1 ), '/ ' .\DIRECTORY_SEPARATOR )) {
300
- return self :: PATH_TYPE_VENDOR ;
261
+ return true ;
301
262
}
302
263
}
303
264
304
- foreach (self ::$ internalPaths as $ internalPath ) {
305
- if (0 === strpos ($ realPath , $ internalPath )) {
306
- return self ::PATH_TYPE_SELF ;
307
- }
308
- }
309
-
310
- return self ::PATH_TYPE_UNDETERMINED ;
265
+ return false ;
311
266
}
312
267
313
268
/**
@@ -326,4 +281,19 @@ public function toString()
326
281
"\n" .str_replace (' ' .getcwd ().\DIRECTORY_SEPARATOR , ' ' , $ exception ->getTraceAsString ()).
327
282
"\n" ;
328
283
}
284
+
285
+ private function getPackageFromLine (array $ line )
286
+ {
287
+ if (!isset ($ line ['file ' ])) {
288
+ return 'internal function ' ;
289
+ }
290
+ if (!$ this ->pathOriginatesFromVendor ($ line ['file ' ])) {
291
+ return 'source code ' ;
292
+ }
293
+ try {
294
+ return $ this ->getPackage ($ line ['file ' ]);
295
+ } catch (\RuntimeException $ e ) {
296
+ return 'unknown ' ;
297
+ }
298
+ }
329
299
}
0 commit comments