@@ -42,519 +42,519 @@ discard block |
||
42 | 42 | */ |
43 | 43 | class ClassLoader |
44 | 44 | { |
45 | - /** @var ?string */ |
|
46 | - private $vendorDir; |
|
47 | - |
|
48 | - // PSR-4 |
|
49 | - /** |
|
50 | - * @var array[] |
|
51 | - * @psalm-var array<string, array<string, int>> |
|
52 | - */ |
|
53 | - private $prefixLengthsPsr4 = array(); |
|
54 | - /** |
|
55 | - * @var array[] |
|
56 | - * @psalm-var array<string, array<int, string>> |
|
57 | - */ |
|
58 | - private $prefixDirsPsr4 = array(); |
|
59 | - /** |
|
60 | - * @var array[] |
|
61 | - * @psalm-var array<string, string> |
|
62 | - */ |
|
63 | - private $fallbackDirsPsr4 = array(); |
|
64 | - |
|
65 | - // PSR-0 |
|
66 | - /** |
|
67 | - * @var array[] |
|
68 | - * @psalm-var array<string, array<string, string[]>> |
|
69 | - */ |
|
70 | - private $prefixesPsr0 = array(); |
|
71 | - /** |
|
72 | - * @var array[] |
|
73 | - * @psalm-var array<string, string> |
|
74 | - */ |
|
75 | - private $fallbackDirsPsr0 = array(); |
|
76 | - |
|
77 | - /** @var bool */ |
|
78 | - private $useIncludePath = false; |
|
79 | - |
|
80 | - /** |
|
81 | - * @var string[] |
|
82 | - * @psalm-var array<string, string> |
|
83 | - */ |
|
84 | - private $classMap = array(); |
|
85 | - |
|
86 | - /** @var bool */ |
|
87 | - private $classMapAuthoritative = false; |
|
88 | - |
|
89 | - /** |
|
90 | - * @var bool[] |
|
91 | - * @psalm-var array<string, bool> |
|
92 | - */ |
|
93 | - private $missingClasses = array(); |
|
94 | - |
|
95 | - /** @var ?string */ |
|
96 | - private $apcuPrefix; |
|
97 | - |
|
98 | - /** |
|
99 | - * @var self[] |
|
100 | - */ |
|
101 | - private static $registeredLoaders = array(); |
|
102 | - |
|
103 | - /** |
|
104 | - * @param ?string $vendorDir |
|
105 | - */ |
|
106 | - public function __construct($vendorDir = null) |
|
107 | - { |
|
108 | - $this->vendorDir = $vendorDir; |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * @return string[] |
|
113 | - */ |
|
114 | - public function getPrefixes() |
|
115 | - { |
|
116 | - if (!empty($this->prefixesPsr0)) { |
|
117 | - return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); |
|
118 | - } |
|
119 | - |
|
120 | - return array(); |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * @return array[] |
|
125 | - * @psalm-return array<string, array<int, string>> |
|
126 | - */ |
|
127 | - public function getPrefixesPsr4() |
|
128 | - { |
|
129 | - return $this->prefixDirsPsr4; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * @return array[] |
|
134 | - * @psalm-return array<string, string> |
|
135 | - */ |
|
136 | - public function getFallbackDirs() |
|
137 | - { |
|
138 | - return $this->fallbackDirsPsr0; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * @return array[] |
|
143 | - * @psalm-return array<string, string> |
|
144 | - */ |
|
145 | - public function getFallbackDirsPsr4() |
|
146 | - { |
|
147 | - return $this->fallbackDirsPsr4; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * @return string[] Array of classname => path |
|
152 | - * @psalm-var array<string, string> |
|
153 | - */ |
|
154 | - public function getClassMap() |
|
155 | - { |
|
156 | - return $this->classMap; |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * @param string[] $classMap Class to filename map |
|
161 | - * @psalm-param array<string, string> $classMap |
|
162 | - * |
|
163 | - * @return void |
|
164 | - */ |
|
165 | - public function addClassMap(array $classMap) |
|
166 | - { |
|
167 | - if ($this->classMap) { |
|
168 | - $this->classMap = array_merge($this->classMap, $classMap); |
|
169 | - } else { |
|
170 | - $this->classMap = $classMap; |
|
171 | - } |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Registers a set of PSR-0 directories for a given prefix, either |
|
176 | - * appending or prepending to the ones previously set for this prefix. |
|
177 | - * |
|
178 | - * @param string $prefix The prefix |
|
179 | - * @param string[]|string $paths The PSR-0 root directories |
|
180 | - * @param bool $prepend Whether to prepend the directories |
|
181 | - * |
|
182 | - * @return void |
|
183 | - */ |
|
184 | - public function add($prefix, $paths, $prepend = false) |
|
185 | - { |
|
186 | - if (!$prefix) { |
|
187 | - if ($prepend) { |
|
188 | - $this->fallbackDirsPsr0 = array_merge( |
|
189 | - (array) $paths, |
|
190 | - $this->fallbackDirsPsr0 |
|
191 | - ); |
|
192 | - } else { |
|
193 | - $this->fallbackDirsPsr0 = array_merge( |
|
194 | - $this->fallbackDirsPsr0, |
|
195 | - (array) $paths |
|
196 | - ); |
|
197 | - } |
|
198 | - |
|
199 | - return; |
|
200 | - } |
|
201 | - |
|
202 | - $first = $prefix[0]; |
|
203 | - if (!isset($this->prefixesPsr0[$first][$prefix])) { |
|
204 | - $this->prefixesPsr0[$first][$prefix] = (array) $paths; |
|
205 | - |
|
206 | - return; |
|
207 | - } |
|
208 | - if ($prepend) { |
|
209 | - $this->prefixesPsr0[$first][$prefix] = array_merge( |
|
210 | - (array) $paths, |
|
211 | - $this->prefixesPsr0[$first][$prefix] |
|
212 | - ); |
|
213 | - } else { |
|
214 | - $this->prefixesPsr0[$first][$prefix] = array_merge( |
|
215 | - $this->prefixesPsr0[$first][$prefix], |
|
216 | - (array) $paths |
|
217 | - ); |
|
218 | - } |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Registers a set of PSR-4 directories for a given namespace, either |
|
223 | - * appending or prepending to the ones previously set for this namespace. |
|
224 | - * |
|
225 | - * @param string $prefix The prefix/namespace, with trailing '\\' |
|
226 | - * @param string[]|string $paths The PSR-4 base directories |
|
227 | - * @param bool $prepend Whether to prepend the directories |
|
228 | - * |
|
229 | - * @throws \InvalidArgumentException |
|
230 | - * |
|
231 | - * @return void |
|
232 | - */ |
|
233 | - public function addPsr4($prefix, $paths, $prepend = false) |
|
234 | - { |
|
235 | - if (!$prefix) { |
|
236 | - // Register directories for the root namespace. |
|
237 | - if ($prepend) { |
|
238 | - $this->fallbackDirsPsr4 = array_merge( |
|
239 | - (array) $paths, |
|
240 | - $this->fallbackDirsPsr4 |
|
241 | - ); |
|
242 | - } else { |
|
243 | - $this->fallbackDirsPsr4 = array_merge( |
|
244 | - $this->fallbackDirsPsr4, |
|
245 | - (array) $paths |
|
246 | - ); |
|
247 | - } |
|
248 | - } elseif (!isset($this->prefixDirsPsr4[$prefix])) { |
|
249 | - // Register directories for a new namespace. |
|
250 | - $length = strlen($prefix); |
|
251 | - if ('\\' !== $prefix[$length - 1]) { |
|
252 | - throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
|
253 | - } |
|
254 | - $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
|
255 | - $this->prefixDirsPsr4[$prefix] = (array) $paths; |
|
256 | - } elseif ($prepend) { |
|
257 | - // Prepend directories for an already registered namespace. |
|
258 | - $this->prefixDirsPsr4[$prefix] = array_merge( |
|
259 | - (array) $paths, |
|
260 | - $this->prefixDirsPsr4[$prefix] |
|
261 | - ); |
|
262 | - } else { |
|
263 | - // Append directories for an already registered namespace. |
|
264 | - $this->prefixDirsPsr4[$prefix] = array_merge( |
|
265 | - $this->prefixDirsPsr4[$prefix], |
|
266 | - (array) $paths |
|
267 | - ); |
|
268 | - } |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * Registers a set of PSR-0 directories for a given prefix, |
|
273 | - * replacing any others previously set for this prefix. |
|
274 | - * |
|
275 | - * @param string $prefix The prefix |
|
276 | - * @param string[]|string $paths The PSR-0 base directories |
|
277 | - * |
|
278 | - * @return void |
|
279 | - */ |
|
280 | - public function set($prefix, $paths) |
|
281 | - { |
|
282 | - if (!$prefix) { |
|
283 | - $this->fallbackDirsPsr0 = (array) $paths; |
|
284 | - } else { |
|
285 | - $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; |
|
286 | - } |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * Registers a set of PSR-4 directories for a given namespace, |
|
291 | - * replacing any others previously set for this namespace. |
|
292 | - * |
|
293 | - * @param string $prefix The prefix/namespace, with trailing '\\' |
|
294 | - * @param string[]|string $paths The PSR-4 base directories |
|
295 | - * |
|
296 | - * @throws \InvalidArgumentException |
|
297 | - * |
|
298 | - * @return void |
|
299 | - */ |
|
300 | - public function setPsr4($prefix, $paths) |
|
301 | - { |
|
302 | - if (!$prefix) { |
|
303 | - $this->fallbackDirsPsr4 = (array) $paths; |
|
304 | - } else { |
|
305 | - $length = strlen($prefix); |
|
306 | - if ('\\' !== $prefix[$length - 1]) { |
|
307 | - throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
|
308 | - } |
|
309 | - $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
|
310 | - $this->prefixDirsPsr4[$prefix] = (array) $paths; |
|
311 | - } |
|
312 | - } |
|
313 | - |
|
314 | - /** |
|
315 | - * Turns on searching the include path for class files. |
|
316 | - * |
|
317 | - * @param bool $useIncludePath |
|
318 | - * |
|
319 | - * @return void |
|
320 | - */ |
|
321 | - public function setUseIncludePath($useIncludePath) |
|
322 | - { |
|
323 | - $this->useIncludePath = $useIncludePath; |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * Can be used to check if the autoloader uses the include path to check |
|
328 | - * for classes. |
|
329 | - * |
|
330 | - * @return bool |
|
331 | - */ |
|
332 | - public function getUseIncludePath() |
|
333 | - { |
|
334 | - return $this->useIncludePath; |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * Turns off searching the prefix and fallback directories for classes |
|
339 | - * that have not been registered with the class map. |
|
340 | - * |
|
341 | - * @param bool $classMapAuthoritative |
|
342 | - * |
|
343 | - * @return void |
|
344 | - */ |
|
345 | - public function setClassMapAuthoritative($classMapAuthoritative) |
|
346 | - { |
|
347 | - $this->classMapAuthoritative = $classMapAuthoritative; |
|
348 | - } |
|
349 | - |
|
350 | - /** |
|
351 | - * Should class lookup fail if not found in the current class map? |
|
352 | - * |
|
353 | - * @return bool |
|
354 | - */ |
|
355 | - public function isClassMapAuthoritative() |
|
356 | - { |
|
357 | - return $this->classMapAuthoritative; |
|
358 | - } |
|
359 | - |
|
360 | - /** |
|
361 | - * APCu prefix to use to cache found/not-found classes, if the extension is enabled. |
|
362 | - * |
|
363 | - * @param string|null $apcuPrefix |
|
364 | - * |
|
365 | - * @return void |
|
366 | - */ |
|
367 | - public function setApcuPrefix($apcuPrefix) |
|
368 | - { |
|
369 | - $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; |
|
370 | - } |
|
371 | - |
|
372 | - /** |
|
373 | - * The APCu prefix in use, or null if APCu caching is not enabled. |
|
374 | - * |
|
375 | - * @return string|null |
|
376 | - */ |
|
377 | - public function getApcuPrefix() |
|
378 | - { |
|
379 | - return $this->apcuPrefix; |
|
380 | - } |
|
381 | - |
|
382 | - /** |
|
383 | - * Registers this instance as an autoloader. |
|
384 | - * |
|
385 | - * @param bool $prepend Whether to prepend the autoloader or not |
|
386 | - * |
|
387 | - * @return void |
|
388 | - */ |
|
389 | - public function register($prepend = false) |
|
390 | - { |
|
391 | - spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
|
392 | - |
|
393 | - if (null === $this->vendorDir) { |
|
394 | - return; |
|
395 | - } |
|
396 | - |
|
397 | - if ($prepend) { |
|
398 | - self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders; |
|
399 | - } else { |
|
400 | - unset(self::$registeredLoaders[$this->vendorDir]); |
|
401 | - self::$registeredLoaders[$this->vendorDir] = $this; |
|
402 | - } |
|
403 | - } |
|
404 | - |
|
405 | - /** |
|
406 | - * Unregisters this instance as an autoloader. |
|
407 | - * |
|
408 | - * @return void |
|
409 | - */ |
|
410 | - public function unregister() |
|
411 | - { |
|
412 | - spl_autoload_unregister(array($this, 'loadClass')); |
|
413 | - |
|
414 | - if (null !== $this->vendorDir) { |
|
415 | - unset(self::$registeredLoaders[$this->vendorDir]); |
|
416 | - } |
|
417 | - } |
|
418 | - |
|
419 | - /** |
|
420 | - * Loads the given class or interface. |
|
421 | - * |
|
422 | - * @param string $class The name of the class |
|
423 | - * @return true|null True if loaded, null otherwise |
|
424 | - */ |
|
425 | - public function loadClass($class) |
|
426 | - { |
|
427 | - if ($file = $this->findFile($class)) { |
|
428 | - includeFile($file); |
|
429 | - |
|
430 | - return true; |
|
431 | - } |
|
432 | - |
|
433 | - return null; |
|
434 | - } |
|
435 | - |
|
436 | - /** |
|
437 | - * Finds the path to the file where the class is defined. |
|
438 | - * |
|
439 | - * @param string $class The name of the class |
|
440 | - * |
|
441 | - * @return string|false The path if found, false otherwise |
|
442 | - */ |
|
443 | - public function findFile($class) |
|
444 | - { |
|
445 | - // class map lookup |
|
446 | - if (isset($this->classMap[$class])) { |
|
447 | - return $this->classMap[$class]; |
|
448 | - } |
|
449 | - if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { |
|
450 | - return false; |
|
451 | - } |
|
452 | - if (null !== $this->apcuPrefix) { |
|
453 | - $file = apcu_fetch($this->apcuPrefix.$class, $hit); |
|
454 | - if ($hit) { |
|
455 | - return $file; |
|
456 | - } |
|
457 | - } |
|
458 | - |
|
459 | - $file = $this->findFileWithExtension($class, '.php'); |
|
460 | - |
|
461 | - // Search for Hack files if we are running on HHVM |
|
462 | - if (false === $file && defined('HHVM_VERSION')) { |
|
463 | - $file = $this->findFileWithExtension($class, '.hh'); |
|
464 | - } |
|
465 | - |
|
466 | - if (null !== $this->apcuPrefix) { |
|
467 | - apcu_add($this->apcuPrefix.$class, $file); |
|
468 | - } |
|
469 | - |
|
470 | - if (false === $file) { |
|
471 | - // Remember that this class does not exist. |
|
472 | - $this->missingClasses[$class] = true; |
|
473 | - } |
|
474 | - |
|
475 | - return $file; |
|
476 | - } |
|
477 | - |
|
478 | - /** |
|
479 | - * Returns the currently registered loaders indexed by their corresponding vendor directories. |
|
480 | - * |
|
481 | - * @return self[] |
|
482 | - */ |
|
483 | - public static function getRegisteredLoaders() |
|
484 | - { |
|
485 | - return self::$registeredLoaders; |
|
486 | - } |
|
487 | - |
|
488 | - /** |
|
489 | - * @param string $class |
|
490 | - * @param string $ext |
|
491 | - * @return string|false |
|
492 | - */ |
|
493 | - private function findFileWithExtension($class, $ext) |
|
494 | - { |
|
495 | - // PSR-4 lookup |
|
496 | - $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
|
497 | - |
|
498 | - $first = $class[0]; |
|
499 | - if (isset($this->prefixLengthsPsr4[$first])) { |
|
500 | - $subPath = $class; |
|
501 | - while (false !== $lastPos = strrpos($subPath, '\\')) { |
|
502 | - $subPath = substr($subPath, 0, $lastPos); |
|
503 | - $search = $subPath . '\\'; |
|
504 | - if (isset($this->prefixDirsPsr4[$search])) { |
|
505 | - $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); |
|
506 | - foreach ($this->prefixDirsPsr4[$search] as $dir) { |
|
507 | - if (file_exists($file = $dir . $pathEnd)) { |
|
508 | - return $file; |
|
509 | - } |
|
510 | - } |
|
511 | - } |
|
512 | - } |
|
513 | - } |
|
514 | - |
|
515 | - // PSR-4 fallback dirs |
|
516 | - foreach ($this->fallbackDirsPsr4 as $dir) { |
|
517 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { |
|
518 | - return $file; |
|
519 | - } |
|
520 | - } |
|
521 | - |
|
522 | - // PSR-0 lookup |
|
523 | - if (false !== $pos = strrpos($class, '\\')) { |
|
524 | - // namespaced class name |
|
525 | - $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) |
|
526 | - . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); |
|
527 | - } else { |
|
528 | - // PEAR-like class name |
|
529 | - $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; |
|
530 | - } |
|
531 | - |
|
532 | - if (isset($this->prefixesPsr0[$first])) { |
|
533 | - foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { |
|
534 | - if (0 === strpos($class, $prefix)) { |
|
535 | - foreach ($dirs as $dir) { |
|
536 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
537 | - return $file; |
|
538 | - } |
|
539 | - } |
|
540 | - } |
|
541 | - } |
|
542 | - } |
|
543 | - |
|
544 | - // PSR-0 fallback dirs |
|
545 | - foreach ($this->fallbackDirsPsr0 as $dir) { |
|
546 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
547 | - return $file; |
|
548 | - } |
|
549 | - } |
|
550 | - |
|
551 | - // PSR-0 include paths. |
|
552 | - if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { |
|
553 | - return $file; |
|
554 | - } |
|
555 | - |
|
556 | - return false; |
|
557 | - } |
|
45 | + /** @var ?string */ |
|
46 | + private $vendorDir; |
|
47 | + |
|
48 | + // PSR-4 |
|
49 | + /** |
|
50 | + * @var array[] |
|
51 | + * @psalm-var array<string, array<string, int>> |
|
52 | + */ |
|
53 | + private $prefixLengthsPsr4 = array(); |
|
54 | + /** |
|
55 | + * @var array[] |
|
56 | + * @psalm-var array<string, array<int, string>> |
|
57 | + */ |
|
58 | + private $prefixDirsPsr4 = array(); |
|
59 | + /** |
|
60 | + * @var array[] |
|
61 | + * @psalm-var array<string, string> |
|
62 | + */ |
|
63 | + private $fallbackDirsPsr4 = array(); |
|
64 | + |
|
65 | + // PSR-0 |
|
66 | + /** |
|
67 | + * @var array[] |
|
68 | + * @psalm-var array<string, array<string, string[]>> |
|
69 | + */ |
|
70 | + private $prefixesPsr0 = array(); |
|
71 | + /** |
|
72 | + * @var array[] |
|
73 | + * @psalm-var array<string, string> |
|
74 | + */ |
|
75 | + private $fallbackDirsPsr0 = array(); |
|
76 | + |
|
77 | + /** @var bool */ |
|
78 | + private $useIncludePath = false; |
|
79 | + |
|
80 | + /** |
|
81 | + * @var string[] |
|
82 | + * @psalm-var array<string, string> |
|
83 | + */ |
|
84 | + private $classMap = array(); |
|
85 | + |
|
86 | + /** @var bool */ |
|
87 | + private $classMapAuthoritative = false; |
|
88 | + |
|
89 | + /** |
|
90 | + * @var bool[] |
|
91 | + * @psalm-var array<string, bool> |
|
92 | + */ |
|
93 | + private $missingClasses = array(); |
|
94 | + |
|
95 | + /** @var ?string */ |
|
96 | + private $apcuPrefix; |
|
97 | + |
|
98 | + /** |
|
99 | + * @var self[] |
|
100 | + */ |
|
101 | + private static $registeredLoaders = array(); |
|
102 | + |
|
103 | + /** |
|
104 | + * @param ?string $vendorDir |
|
105 | + */ |
|
106 | + public function __construct($vendorDir = null) |
|
107 | + { |
|
108 | + $this->vendorDir = $vendorDir; |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * @return string[] |
|
113 | + */ |
|
114 | + public function getPrefixes() |
|
115 | + { |
|
116 | + if (!empty($this->prefixesPsr0)) { |
|
117 | + return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); |
|
118 | + } |
|
119 | + |
|
120 | + return array(); |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * @return array[] |
|
125 | + * @psalm-return array<string, array<int, string>> |
|
126 | + */ |
|
127 | + public function getPrefixesPsr4() |
|
128 | + { |
|
129 | + return $this->prefixDirsPsr4; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * @return array[] |
|
134 | + * @psalm-return array<string, string> |
|
135 | + */ |
|
136 | + public function getFallbackDirs() |
|
137 | + { |
|
138 | + return $this->fallbackDirsPsr0; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * @return array[] |
|
143 | + * @psalm-return array<string, string> |
|
144 | + */ |
|
145 | + public function getFallbackDirsPsr4() |
|
146 | + { |
|
147 | + return $this->fallbackDirsPsr4; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * @return string[] Array of classname => path |
|
152 | + * @psalm-var array<string, string> |
|
153 | + */ |
|
154 | + public function getClassMap() |
|
155 | + { |
|
156 | + return $this->classMap; |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * @param string[] $classMap Class to filename map |
|
161 | + * @psalm-param array<string, string> $classMap |
|
162 | + * |
|
163 | + * @return void |
|
164 | + */ |
|
165 | + public function addClassMap(array $classMap) |
|
166 | + { |
|
167 | + if ($this->classMap) { |
|
168 | + $this->classMap = array_merge($this->classMap, $classMap); |
|
169 | + } else { |
|
170 | + $this->classMap = $classMap; |
|
171 | + } |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * Registers a set of PSR-0 directories for a given prefix, either |
|
176 | + * appending or prepending to the ones previously set for this prefix. |
|
177 | + * |
|
178 | + * @param string $prefix The prefix |
|
179 | + * @param string[]|string $paths The PSR-0 root directories |
|
180 | + * @param bool $prepend Whether to prepend the directories |
|
181 | + * |
|
182 | + * @return void |
|
183 | + */ |
|
184 | + public function add($prefix, $paths, $prepend = false) |
|
185 | + { |
|
186 | + if (!$prefix) { |
|
187 | + if ($prepend) { |
|
188 | + $this->fallbackDirsPsr0 = array_merge( |
|
189 | + (array) $paths, |
|
190 | + $this->fallbackDirsPsr0 |
|
191 | + ); |
|
192 | + } else { |
|
193 | + $this->fallbackDirsPsr0 = array_merge( |
|
194 | + $this->fallbackDirsPsr0, |
|
195 | + (array) $paths |
|
196 | + ); |
|
197 | + } |
|
198 | + |
|
199 | + return; |
|
200 | + } |
|
201 | + |
|
202 | + $first = $prefix[0]; |
|
203 | + if (!isset($this->prefixesPsr0[$first][$prefix])) { |
|
204 | + $this->prefixesPsr0[$first][$prefix] = (array) $paths; |
|
205 | + |
|
206 | + return; |
|
207 | + } |
|
208 | + if ($prepend) { |
|
209 | + $this->prefixesPsr0[$first][$prefix] = array_merge( |
|
210 | + (array) $paths, |
|
211 | + $this->prefixesPsr0[$first][$prefix] |
|
212 | + ); |
|
213 | + } else { |
|
214 | + $this->prefixesPsr0[$first][$prefix] = array_merge( |
|
215 | + $this->prefixesPsr0[$first][$prefix], |
|
216 | + (array) $paths |
|
217 | + ); |
|
218 | + } |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Registers a set of PSR-4 directories for a given namespace, either |
|
223 | + * appending or prepending to the ones previously set for this namespace. |
|
224 | + * |
|
225 | + * @param string $prefix The prefix/namespace, with trailing '\\' |
|
226 | + * @param string[]|string $paths The PSR-4 base directories |
|
227 | + * @param bool $prepend Whether to prepend the directories |
|
228 | + * |
|
229 | + * @throws \InvalidArgumentException |
|
230 | + * |
|
231 | + * @return void |
|
232 | + */ |
|
233 | + public function addPsr4($prefix, $paths, $prepend = false) |
|
234 | + { |
|
235 | + if (!$prefix) { |
|
236 | + // Register directories for the root namespace. |
|
237 | + if ($prepend) { |
|
238 | + $this->fallbackDirsPsr4 = array_merge( |
|
239 | + (array) $paths, |
|
240 | + $this->fallbackDirsPsr4 |
|
241 | + ); |
|
242 | + } else { |
|
243 | + $this->fallbackDirsPsr4 = array_merge( |
|
244 | + $this->fallbackDirsPsr4, |
|
245 | + (array) $paths |
|
246 | + ); |
|
247 | + } |
|
248 | + } elseif (!isset($this->prefixDirsPsr4[$prefix])) { |
|
249 | + // Register directories for a new namespace. |
|
250 | + $length = strlen($prefix); |
|
251 | + if ('\\' !== $prefix[$length - 1]) { |
|
252 | + throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
|
253 | + } |
|
254 | + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
|
255 | + $this->prefixDirsPsr4[$prefix] = (array) $paths; |
|
256 | + } elseif ($prepend) { |
|
257 | + // Prepend directories for an already registered namespace. |
|
258 | + $this->prefixDirsPsr4[$prefix] = array_merge( |
|
259 | + (array) $paths, |
|
260 | + $this->prefixDirsPsr4[$prefix] |
|
261 | + ); |
|
262 | + } else { |
|
263 | + // Append directories for an already registered namespace. |
|
264 | + $this->prefixDirsPsr4[$prefix] = array_merge( |
|
265 | + $this->prefixDirsPsr4[$prefix], |
|
266 | + (array) $paths |
|
267 | + ); |
|
268 | + } |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * Registers a set of PSR-0 directories for a given prefix, |
|
273 | + * replacing any others previously set for this prefix. |
|
274 | + * |
|
275 | + * @param string $prefix The prefix |
|
276 | + * @param string[]|string $paths The PSR-0 base directories |
|
277 | + * |
|
278 | + * @return void |
|
279 | + */ |
|
280 | + public function set($prefix, $paths) |
|
281 | + { |
|
282 | + if (!$prefix) { |
|
283 | + $this->fallbackDirsPsr0 = (array) $paths; |
|
284 | + } else { |
|
285 | + $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; |
|
286 | + } |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * Registers a set of PSR-4 directories for a given namespace, |
|
291 | + * replacing any others previously set for this namespace. |
|
292 | + * |
|
293 | + * @param string $prefix The prefix/namespace, with trailing '\\' |
|
294 | + * @param string[]|string $paths The PSR-4 base directories |
|
295 | + * |
|
296 | + * @throws \InvalidArgumentException |
|
297 | + * |
|
298 | + * @return void |
|
299 | + */ |
|
300 | + public function setPsr4($prefix, $paths) |
|
301 | + { |
|
302 | + if (!$prefix) { |
|
303 | + $this->fallbackDirsPsr4 = (array) $paths; |
|
304 | + } else { |
|
305 | + $length = strlen($prefix); |
|
306 | + if ('\\' !== $prefix[$length - 1]) { |
|
307 | + throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
|
308 | + } |
|
309 | + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
|
310 | + $this->prefixDirsPsr4[$prefix] = (array) $paths; |
|
311 | + } |
|
312 | + } |
|
313 | + |
|
314 | + /** |
|
315 | + * Turns on searching the include path for class files. |
|
316 | + * |
|
317 | + * @param bool $useIncludePath |
|
318 | + * |
|
319 | + * @return void |
|
320 | + */ |
|
321 | + public function setUseIncludePath($useIncludePath) |
|
322 | + { |
|
323 | + $this->useIncludePath = $useIncludePath; |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * Can be used to check if the autoloader uses the include path to check |
|
328 | + * for classes. |
|
329 | + * |
|
330 | + * @return bool |
|
331 | + */ |
|
332 | + public function getUseIncludePath() |
|
333 | + { |
|
334 | + return $this->useIncludePath; |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * Turns off searching the prefix and fallback directories for classes |
|
339 | + * that have not been registered with the class map. |
|
340 | + * |
|
341 | + * @param bool $classMapAuthoritative |
|
342 | + * |
|
343 | + * @return void |
|
344 | + */ |
|
345 | + public function setClassMapAuthoritative($classMapAuthoritative) |
|
346 | + { |
|
347 | + $this->classMapAuthoritative = $classMapAuthoritative; |
|
348 | + } |
|
349 | + |
|
350 | + /** |
|
351 | + * Should class lookup fail if not found in the current class map? |
|
352 | + * |
|
353 | + * @return bool |
|
354 | + */ |
|
355 | + public function isClassMapAuthoritative() |
|
356 | + { |
|
357 | + return $this->classMapAuthoritative; |
|
358 | + } |
|
359 | + |
|
360 | + /** |
|
361 | + * APCu prefix to use to cache found/not-found classes, if the extension is enabled. |
|
362 | + * |
|
363 | + * @param string|null $apcuPrefix |
|
364 | + * |
|
365 | + * @return void |
|
366 | + */ |
|
367 | + public function setApcuPrefix($apcuPrefix) |
|
368 | + { |
|
369 | + $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; |
|
370 | + } |
|
371 | + |
|
372 | + /** |
|
373 | + * The APCu prefix in use, or null if APCu caching is not enabled. |
|
374 | + * |
|
375 | + * @return string|null |
|
376 | + */ |
|
377 | + public function getApcuPrefix() |
|
378 | + { |
|
379 | + return $this->apcuPrefix; |
|
380 | + } |
|
381 | + |
|
382 | + /** |
|
383 | + * Registers this instance as an autoloader. |
|
384 | + * |
|
385 | + * @param bool $prepend Whether to prepend the autoloader or not |
|
386 | + * |
|
387 | + * @return void |
|
388 | + */ |
|
389 | + public function register($prepend = false) |
|
390 | + { |
|
391 | + spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
|
392 | + |
|
393 | + if (null === $this->vendorDir) { |
|
394 | + return; |
|
395 | + } |
|
396 | + |
|
397 | + if ($prepend) { |
|
398 | + self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders; |
|
399 | + } else { |
|
400 | + unset(self::$registeredLoaders[$this->vendorDir]); |
|
401 | + self::$registeredLoaders[$this->vendorDir] = $this; |
|
402 | + } |
|
403 | + } |
|
404 | + |
|
405 | + /** |
|
406 | + * Unregisters this instance as an autoloader. |
|
407 | + * |
|
408 | + * @return void |
|
409 | + */ |
|
410 | + public function unregister() |
|
411 | + { |
|
412 | + spl_autoload_unregister(array($this, 'loadClass')); |
|
413 | + |
|
414 | + if (null !== $this->vendorDir) { |
|
415 | + unset(self::$registeredLoaders[$this->vendorDir]); |
|
416 | + } |
|
417 | + } |
|
418 | + |
|
419 | + /** |
|
420 | + * Loads the given class or interface. |
|
421 | + * |
|
422 | + * @param string $class The name of the class |
|
423 | + * @return true|null True if loaded, null otherwise |
|
424 | + */ |
|
425 | + public function loadClass($class) |
|
426 | + { |
|
427 | + if ($file = $this->findFile($class)) { |
|
428 | + includeFile($file); |
|
429 | + |
|
430 | + return true; |
|
431 | + } |
|
432 | + |
|
433 | + return null; |
|
434 | + } |
|
435 | + |
|
436 | + /** |
|
437 | + * Finds the path to the file where the class is defined. |
|
438 | + * |
|
439 | + * @param string $class The name of the class |
|
440 | + * |
|
441 | + * @return string|false The path if found, false otherwise |
|
442 | + */ |
|
443 | + public function findFile($class) |
|
444 | + { |
|
445 | + // class map lookup |
|
446 | + if (isset($this->classMap[$class])) { |
|
447 | + return $this->classMap[$class]; |
|
448 | + } |
|
449 | + if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { |
|
450 | + return false; |
|
451 | + } |
|
452 | + if (null !== $this->apcuPrefix) { |
|
453 | + $file = apcu_fetch($this->apcuPrefix.$class, $hit); |
|
454 | + if ($hit) { |
|
455 | + return $file; |
|
456 | + } |
|
457 | + } |
|
458 | + |
|
459 | + $file = $this->findFileWithExtension($class, '.php'); |
|
460 | + |
|
461 | + // Search for Hack files if we are running on HHVM |
|
462 | + if (false === $file && defined('HHVM_VERSION')) { |
|
463 | + $file = $this->findFileWithExtension($class, '.hh'); |
|
464 | + } |
|
465 | + |
|
466 | + if (null !== $this->apcuPrefix) { |
|
467 | + apcu_add($this->apcuPrefix.$class, $file); |
|
468 | + } |
|
469 | + |
|
470 | + if (false === $file) { |
|
471 | + // Remember that this class does not exist. |
|
472 | + $this->missingClasses[$class] = true; |
|
473 | + } |
|
474 | + |
|
475 | + return $file; |
|
476 | + } |
|
477 | + |
|
478 | + /** |
|
479 | + * Returns the currently registered loaders indexed by their corresponding vendor directories. |
|
480 | + * |
|
481 | + * @return self[] |
|
482 | + */ |
|
483 | + public static function getRegisteredLoaders() |
|
484 | + { |
|
485 | + return self::$registeredLoaders; |
|
486 | + } |
|
487 | + |
|
488 | + /** |
|
489 | + * @param string $class |
|
490 | + * @param string $ext |
|
491 | + * @return string|false |
|
492 | + */ |
|
493 | + private function findFileWithExtension($class, $ext) |
|
494 | + { |
|
495 | + // PSR-4 lookup |
|
496 | + $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
|
497 | + |
|
498 | + $first = $class[0]; |
|
499 | + if (isset($this->prefixLengthsPsr4[$first])) { |
|
500 | + $subPath = $class; |
|
501 | + while (false !== $lastPos = strrpos($subPath, '\\')) { |
|
502 | + $subPath = substr($subPath, 0, $lastPos); |
|
503 | + $search = $subPath . '\\'; |
|
504 | + if (isset($this->prefixDirsPsr4[$search])) { |
|
505 | + $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); |
|
506 | + foreach ($this->prefixDirsPsr4[$search] as $dir) { |
|
507 | + if (file_exists($file = $dir . $pathEnd)) { |
|
508 | + return $file; |
|
509 | + } |
|
510 | + } |
|
511 | + } |
|
512 | + } |
|
513 | + } |
|
514 | + |
|
515 | + // PSR-4 fallback dirs |
|
516 | + foreach ($this->fallbackDirsPsr4 as $dir) { |
|
517 | + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { |
|
518 | + return $file; |
|
519 | + } |
|
520 | + } |
|
521 | + |
|
522 | + // PSR-0 lookup |
|
523 | + if (false !== $pos = strrpos($class, '\\')) { |
|
524 | + // namespaced class name |
|
525 | + $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) |
|
526 | + . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); |
|
527 | + } else { |
|
528 | + // PEAR-like class name |
|
529 | + $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; |
|
530 | + } |
|
531 | + |
|
532 | + if (isset($this->prefixesPsr0[$first])) { |
|
533 | + foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { |
|
534 | + if (0 === strpos($class, $prefix)) { |
|
535 | + foreach ($dirs as $dir) { |
|
536 | + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
537 | + return $file; |
|
538 | + } |
|
539 | + } |
|
540 | + } |
|
541 | + } |
|
542 | + } |
|
543 | + |
|
544 | + // PSR-0 fallback dirs |
|
545 | + foreach ($this->fallbackDirsPsr0 as $dir) { |
|
546 | + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
547 | + return $file; |
|
548 | + } |
|
549 | + } |
|
550 | + |
|
551 | + // PSR-0 include paths. |
|
552 | + if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { |
|
553 | + return $file; |
|
554 | + } |
|
555 | + |
|
556 | + return false; |
|
557 | + } |
|
558 | 558 | } |
559 | 559 | |
560 | 560 | /** |
@@ -568,5 +568,5 @@ discard block |
||
568 | 568 | */ |
569 | 569 | function includeFile($file) |
570 | 570 | { |
571 | - include $file; |
|
571 | + include $file; |
|
572 | 572 | } |
@@ -24,327 +24,327 @@ |
||
24 | 24 | */ |
25 | 25 | class InstalledVersions |
26 | 26 | { |
27 | - /** |
|
28 | - * @var mixed[]|null |
|
29 | - * @psalm-var array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}, versions: array<string, array{dev_requirement: bool, pretty_version?: string, version?: string, aliases?: string[], reference?: string, replaced?: string[], provided?: string[], install_path?: string, type?: string}>}|array{}|null |
|
30 | - */ |
|
31 | - private static $installed; |
|
32 | - |
|
33 | - /** |
|
34 | - * @var bool|null |
|
35 | - */ |
|
36 | - private static $canGetVendors; |
|
37 | - |
|
38 | - /** |
|
39 | - * @var array[] |
|
40 | - * @psalm-var array<string, array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}, versions: array<string, array{dev_requirement: bool, pretty_version?: string, version?: string, aliases?: string[], reference?: string, replaced?: string[], provided?: string[], install_path?: string, type?: string}>}> |
|
41 | - */ |
|
42 | - private static $installedByVendor = array(); |
|
43 | - |
|
44 | - /** |
|
45 | - * Returns a list of all package names which are present, either by being installed, replaced or provided |
|
46 | - * |
|
47 | - * @return string[] |
|
48 | - * @psalm-return list<string> |
|
49 | - */ |
|
50 | - public static function getInstalledPackages() |
|
51 | - { |
|
52 | - $packages = array(); |
|
53 | - foreach (self::getInstalled() as $installed) { |
|
54 | - $packages[] = array_keys($installed['versions']); |
|
55 | - } |
|
56 | - |
|
57 | - if (1 === \count($packages)) { |
|
58 | - return $packages[0]; |
|
59 | - } |
|
60 | - |
|
61 | - return array_keys(array_flip(\call_user_func_array('array_merge', $packages))); |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Returns a list of all package names with a specific type e.g. 'library' |
|
66 | - * |
|
67 | - * @param string $type |
|
68 | - * @return string[] |
|
69 | - * @psalm-return list<string> |
|
70 | - */ |
|
71 | - public static function getInstalledPackagesByType($type) |
|
72 | - { |
|
73 | - $packagesByType = array(); |
|
74 | - |
|
75 | - foreach (self::getInstalled() as $installed) { |
|
76 | - foreach ($installed['versions'] as $name => $package) { |
|
77 | - if (isset($package['type']) && $package['type'] === $type) { |
|
78 | - $packagesByType[] = $name; |
|
79 | - } |
|
80 | - } |
|
81 | - } |
|
82 | - |
|
83 | - return $packagesByType; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Checks whether the given package is installed |
|
88 | - * |
|
89 | - * This also returns true if the package name is provided or replaced by another package |
|
90 | - * |
|
91 | - * @param string $packageName |
|
92 | - * @param bool $includeDevRequirements |
|
93 | - * @return bool |
|
94 | - */ |
|
95 | - public static function isInstalled($packageName, $includeDevRequirements = true) |
|
96 | - { |
|
97 | - foreach (self::getInstalled() as $installed) { |
|
98 | - if (isset($installed['versions'][$packageName])) { |
|
99 | - return $includeDevRequirements || empty($installed['versions'][$packageName]['dev_requirement']); |
|
100 | - } |
|
101 | - } |
|
102 | - |
|
103 | - return false; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Checks whether the given package satisfies a version constraint |
|
108 | - * |
|
109 | - * e.g. If you want to know whether version 2.3+ of package foo/bar is installed, you would call: |
|
110 | - * |
|
111 | - * Composer\InstalledVersions::satisfies(new VersionParser, 'foo/bar', '^2.3') |
|
112 | - * |
|
113 | - * @param VersionParser $parser Install composer/semver to have access to this class and functionality |
|
114 | - * @param string $packageName |
|
115 | - * @param string|null $constraint A version constraint to check for, if you pass one you have to make sure composer/semver is required by your package |
|
116 | - * @return bool |
|
117 | - */ |
|
118 | - public static function satisfies(VersionParser $parser, $packageName, $constraint) |
|
119 | - { |
|
120 | - $constraint = $parser->parseConstraints($constraint); |
|
121 | - $provided = $parser->parseConstraints(self::getVersionRanges($packageName)); |
|
122 | - |
|
123 | - return $provided->matches($constraint); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Returns a version constraint representing all the range(s) which are installed for a given package |
|
128 | - * |
|
129 | - * It is easier to use this via isInstalled() with the $constraint argument if you need to check |
|
130 | - * whether a given version of a package is installed, and not just whether it exists |
|
131 | - * |
|
132 | - * @param string $packageName |
|
133 | - * @return string Version constraint usable with composer/semver |
|
134 | - */ |
|
135 | - public static function getVersionRanges($packageName) |
|
136 | - { |
|
137 | - foreach (self::getInstalled() as $installed) { |
|
138 | - if (!isset($installed['versions'][$packageName])) { |
|
139 | - continue; |
|
140 | - } |
|
141 | - |
|
142 | - $ranges = array(); |
|
143 | - if (isset($installed['versions'][$packageName]['pretty_version'])) { |
|
144 | - $ranges[] = $installed['versions'][$packageName]['pretty_version']; |
|
145 | - } |
|
146 | - if (array_key_exists('aliases', $installed['versions'][$packageName])) { |
|
147 | - $ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']); |
|
148 | - } |
|
149 | - if (array_key_exists('replaced', $installed['versions'][$packageName])) { |
|
150 | - $ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']); |
|
151 | - } |
|
152 | - if (array_key_exists('provided', $installed['versions'][$packageName])) { |
|
153 | - $ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']); |
|
154 | - } |
|
155 | - |
|
156 | - return implode(' || ', $ranges); |
|
157 | - } |
|
158 | - |
|
159 | - throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * @param string $packageName |
|
164 | - * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present |
|
165 | - */ |
|
166 | - public static function getVersion($packageName) |
|
167 | - { |
|
168 | - foreach (self::getInstalled() as $installed) { |
|
169 | - if (!isset($installed['versions'][$packageName])) { |
|
170 | - continue; |
|
171 | - } |
|
172 | - |
|
173 | - if (!isset($installed['versions'][$packageName]['version'])) { |
|
174 | - return null; |
|
175 | - } |
|
176 | - |
|
177 | - return $installed['versions'][$packageName]['version']; |
|
178 | - } |
|
179 | - |
|
180 | - throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * @param string $packageName |
|
185 | - * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present |
|
186 | - */ |
|
187 | - public static function getPrettyVersion($packageName) |
|
188 | - { |
|
189 | - foreach (self::getInstalled() as $installed) { |
|
190 | - if (!isset($installed['versions'][$packageName])) { |
|
191 | - continue; |
|
192 | - } |
|
193 | - |
|
194 | - if (!isset($installed['versions'][$packageName]['pretty_version'])) { |
|
195 | - return null; |
|
196 | - } |
|
197 | - |
|
198 | - return $installed['versions'][$packageName]['pretty_version']; |
|
199 | - } |
|
200 | - |
|
201 | - throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * @param string $packageName |
|
206 | - * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as reference |
|
207 | - */ |
|
208 | - public static function getReference($packageName) |
|
209 | - { |
|
210 | - foreach (self::getInstalled() as $installed) { |
|
211 | - if (!isset($installed['versions'][$packageName])) { |
|
212 | - continue; |
|
213 | - } |
|
214 | - |
|
215 | - if (!isset($installed['versions'][$packageName]['reference'])) { |
|
216 | - return null; |
|
217 | - } |
|
218 | - |
|
219 | - return $installed['versions'][$packageName]['reference']; |
|
220 | - } |
|
221 | - |
|
222 | - throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * @param string $packageName |
|
227 | - * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as install path. Packages of type metapackages also have a null install path. |
|
228 | - */ |
|
229 | - public static function getInstallPath($packageName) |
|
230 | - { |
|
231 | - foreach (self::getInstalled() as $installed) { |
|
232 | - if (!isset($installed['versions'][$packageName])) { |
|
233 | - continue; |
|
234 | - } |
|
235 | - |
|
236 | - return isset($installed['versions'][$packageName]['install_path']) ? $installed['versions'][$packageName]['install_path'] : null; |
|
237 | - } |
|
238 | - |
|
239 | - throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * @return array |
|
244 | - * @psalm-return array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string} |
|
245 | - */ |
|
246 | - public static function getRootPackage() |
|
247 | - { |
|
248 | - $installed = self::getInstalled(); |
|
249 | - |
|
250 | - return $installed[0]['root']; |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * Returns the raw installed.php data for custom implementations |
|
255 | - * |
|
256 | - * @deprecated Use getAllRawData() instead which returns all datasets for all autoloaders present in the process. getRawData only returns the first dataset loaded, which may not be what you expect. |
|
257 | - * @return array[] |
|
258 | - * @psalm-return array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}, versions: array<string, array{dev_requirement: bool, pretty_version?: string, version?: string, aliases?: string[], reference?: string, replaced?: string[], provided?: string[], install_path?: string, type?: string}>} |
|
259 | - */ |
|
260 | - public static function getRawData() |
|
261 | - { |
|
262 | - @trigger_error('getRawData only returns the first dataset loaded, which may not be what you expect. Use getAllRawData() instead which returns all datasets for all autoloaders present in the process.', E_USER_DEPRECATED); |
|
263 | - |
|
264 | - if (null === self::$installed) { |
|
265 | - // only require the installed.php file if this file is loaded from its dumped location, |
|
266 | - // and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937 |
|
267 | - if (substr(__DIR__, -8, 1) !== 'C') { |
|
268 | - self::$installed = include __DIR__ . '/installed.php'; |
|
269 | - } else { |
|
270 | - self::$installed = array(); |
|
271 | - } |
|
272 | - } |
|
273 | - |
|
274 | - return self::$installed; |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * Returns the raw data of all installed.php which are currently loaded for custom implementations |
|
279 | - * |
|
280 | - * @return array[] |
|
281 | - * @psalm-return list<array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}, versions: array<string, array{dev_requirement: bool, pretty_version?: string, version?: string, aliases?: string[], reference?: string, replaced?: string[], provided?: string[], install_path?: string, type?: string}>}> |
|
282 | - */ |
|
283 | - public static function getAllRawData() |
|
284 | - { |
|
285 | - return self::getInstalled(); |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * Lets you reload the static array from another file |
|
290 | - * |
|
291 | - * This is only useful for complex integrations in which a project needs to use |
|
292 | - * this class but then also needs to execute another project's autoloader in process, |
|
293 | - * and wants to ensure both projects have access to their version of installed.php. |
|
294 | - * |
|
295 | - * A typical case would be PHPUnit, where it would need to make sure it reads all |
|
296 | - * the data it needs from this class, then call reload() with |
|
297 | - * `require $CWD/vendor/composer/installed.php` (or similar) as input to make sure |
|
298 | - * the project in which it runs can then also use this class safely, without |
|
299 | - * interference between PHPUnit's dependencies and the project's dependencies. |
|
300 | - * |
|
301 | - * @param array[] $data A vendor/composer/installed.php data set |
|
302 | - * @return void |
|
303 | - * |
|
304 | - * @psalm-param array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}, versions: array<string, array{dev_requirement: bool, pretty_version?: string, version?: string, aliases?: string[], reference?: string, replaced?: string[], provided?: string[], install_path?: string, type?: string}>} $data |
|
305 | - */ |
|
306 | - public static function reload($data) |
|
307 | - { |
|
308 | - self::$installed = $data; |
|
309 | - self::$installedByVendor = array(); |
|
310 | - } |
|
311 | - |
|
312 | - /** |
|
313 | - * @return array[] |
|
314 | - * @psalm-return list<array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}, versions: array<string, array{dev_requirement: bool, pretty_version?: string, version?: string, aliases?: string[], reference?: string, replaced?: string[], provided?: string[], install_path?: string, type?: string}>}> |
|
315 | - */ |
|
316 | - private static function getInstalled() |
|
317 | - { |
|
318 | - if (null === self::$canGetVendors) { |
|
319 | - self::$canGetVendors = method_exists('Composer\Autoload\ClassLoader', 'getRegisteredLoaders'); |
|
320 | - } |
|
321 | - |
|
322 | - $installed = array(); |
|
323 | - |
|
324 | - if (self::$canGetVendors) { |
|
325 | - foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) { |
|
326 | - if (isset(self::$installedByVendor[$vendorDir])) { |
|
327 | - $installed[] = self::$installedByVendor[$vendorDir]; |
|
328 | - } elseif (is_file($vendorDir.'/composer/installed.php')) { |
|
329 | - $installed[] = self::$installedByVendor[$vendorDir] = require $vendorDir.'/composer/installed.php'; |
|
330 | - if (null === self::$installed && strtr($vendorDir.'/composer', '\\', '/') === strtr(__DIR__, '\\', '/')) { |
|
331 | - self::$installed = $installed[count($installed) - 1]; |
|
332 | - } |
|
333 | - } |
|
334 | - } |
|
335 | - } |
|
336 | - |
|
337 | - if (null === self::$installed) { |
|
338 | - // only require the installed.php file if this file is loaded from its dumped location, |
|
339 | - // and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937 |
|
340 | - if (substr(__DIR__, -8, 1) !== 'C') { |
|
341 | - self::$installed = require __DIR__ . '/installed.php'; |
|
342 | - } else { |
|
343 | - self::$installed = array(); |
|
344 | - } |
|
345 | - } |
|
346 | - $installed[] = self::$installed; |
|
347 | - |
|
348 | - return $installed; |
|
349 | - } |
|
27 | + /** |
|
28 | + * @var mixed[]|null |
|
29 | + * @psalm-var array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}, versions: array<string, array{dev_requirement: bool, pretty_version?: string, version?: string, aliases?: string[], reference?: string, replaced?: string[], provided?: string[], install_path?: string, type?: string}>}|array{}|null |
|
30 | + */ |
|
31 | + private static $installed; |
|
32 | + |
|
33 | + /** |
|
34 | + * @var bool|null |
|
35 | + */ |
|
36 | + private static $canGetVendors; |
|
37 | + |
|
38 | + /** |
|
39 | + * @var array[] |
|
40 | + * @psalm-var array<string, array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}, versions: array<string, array{dev_requirement: bool, pretty_version?: string, version?: string, aliases?: string[], reference?: string, replaced?: string[], provided?: string[], install_path?: string, type?: string}>}> |
|
41 | + */ |
|
42 | + private static $installedByVendor = array(); |
|
43 | + |
|
44 | + /** |
|
45 | + * Returns a list of all package names which are present, either by being installed, replaced or provided |
|
46 | + * |
|
47 | + * @return string[] |
|
48 | + * @psalm-return list<string> |
|
49 | + */ |
|
50 | + public static function getInstalledPackages() |
|
51 | + { |
|
52 | + $packages = array(); |
|
53 | + foreach (self::getInstalled() as $installed) { |
|
54 | + $packages[] = array_keys($installed['versions']); |
|
55 | + } |
|
56 | + |
|
57 | + if (1 === \count($packages)) { |
|
58 | + return $packages[0]; |
|
59 | + } |
|
60 | + |
|
61 | + return array_keys(array_flip(\call_user_func_array('array_merge', $packages))); |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Returns a list of all package names with a specific type e.g. 'library' |
|
66 | + * |
|
67 | + * @param string $type |
|
68 | + * @return string[] |
|
69 | + * @psalm-return list<string> |
|
70 | + */ |
|
71 | + public static function getInstalledPackagesByType($type) |
|
72 | + { |
|
73 | + $packagesByType = array(); |
|
74 | + |
|
75 | + foreach (self::getInstalled() as $installed) { |
|
76 | + foreach ($installed['versions'] as $name => $package) { |
|
77 | + if (isset($package['type']) && $package['type'] === $type) { |
|
78 | + $packagesByType[] = $name; |
|
79 | + } |
|
80 | + } |
|
81 | + } |
|
82 | + |
|
83 | + return $packagesByType; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Checks whether the given package is installed |
|
88 | + * |
|
89 | + * This also returns true if the package name is provided or replaced by another package |
|
90 | + * |
|
91 | + * @param string $packageName |
|
92 | + * @param bool $includeDevRequirements |
|
93 | + * @return bool |
|
94 | + */ |
|
95 | + public static function isInstalled($packageName, $includeDevRequirements = true) |
|
96 | + { |
|
97 | + foreach (self::getInstalled() as $installed) { |
|
98 | + if (isset($installed['versions'][$packageName])) { |
|
99 | + return $includeDevRequirements || empty($installed['versions'][$packageName]['dev_requirement']); |
|
100 | + } |
|
101 | + } |
|
102 | + |
|
103 | + return false; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Checks whether the given package satisfies a version constraint |
|
108 | + * |
|
109 | + * e.g. If you want to know whether version 2.3+ of package foo/bar is installed, you would call: |
|
110 | + * |
|
111 | + * Composer\InstalledVersions::satisfies(new VersionParser, 'foo/bar', '^2.3') |
|
112 | + * |
|
113 | + * @param VersionParser $parser Install composer/semver to have access to this class and functionality |
|
114 | + * @param string $packageName |
|
115 | + * @param string|null $constraint A version constraint to check for, if you pass one you have to make sure composer/semver is required by your package |
|
116 | + * @return bool |
|
117 | + */ |
|
118 | + public static function satisfies(VersionParser $parser, $packageName, $constraint) |
|
119 | + { |
|
120 | + $constraint = $parser->parseConstraints($constraint); |
|
121 | + $provided = $parser->parseConstraints(self::getVersionRanges($packageName)); |
|
122 | + |
|
123 | + return $provided->matches($constraint); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Returns a version constraint representing all the range(s) which are installed for a given package |
|
128 | + * |
|
129 | + * It is easier to use this via isInstalled() with the $constraint argument if you need to check |
|
130 | + * whether a given version of a package is installed, and not just whether it exists |
|
131 | + * |
|
132 | + * @param string $packageName |
|
133 | + * @return string Version constraint usable with composer/semver |
|
134 | + */ |
|
135 | + public static function getVersionRanges($packageName) |
|
136 | + { |
|
137 | + foreach (self::getInstalled() as $installed) { |
|
138 | + if (!isset($installed['versions'][$packageName])) { |
|
139 | + continue; |
|
140 | + } |
|
141 | + |
|
142 | + $ranges = array(); |
|
143 | + if (isset($installed['versions'][$packageName]['pretty_version'])) { |
|
144 | + $ranges[] = $installed['versions'][$packageName]['pretty_version']; |
|
145 | + } |
|
146 | + if (array_key_exists('aliases', $installed['versions'][$packageName])) { |
|
147 | + $ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']); |
|
148 | + } |
|
149 | + if (array_key_exists('replaced', $installed['versions'][$packageName])) { |
|
150 | + $ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']); |
|
151 | + } |
|
152 | + if (array_key_exists('provided', $installed['versions'][$packageName])) { |
|
153 | + $ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']); |
|
154 | + } |
|
155 | + |
|
156 | + return implode(' || ', $ranges); |
|
157 | + } |
|
158 | + |
|
159 | + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * @param string $packageName |
|
164 | + * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present |
|
165 | + */ |
|
166 | + public static function getVersion($packageName) |
|
167 | + { |
|
168 | + foreach (self::getInstalled() as $installed) { |
|
169 | + if (!isset($installed['versions'][$packageName])) { |
|
170 | + continue; |
|
171 | + } |
|
172 | + |
|
173 | + if (!isset($installed['versions'][$packageName]['version'])) { |
|
174 | + return null; |
|
175 | + } |
|
176 | + |
|
177 | + return $installed['versions'][$packageName]['version']; |
|
178 | + } |
|
179 | + |
|
180 | + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * @param string $packageName |
|
185 | + * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present |
|
186 | + */ |
|
187 | + public static function getPrettyVersion($packageName) |
|
188 | + { |
|
189 | + foreach (self::getInstalled() as $installed) { |
|
190 | + if (!isset($installed['versions'][$packageName])) { |
|
191 | + continue; |
|
192 | + } |
|
193 | + |
|
194 | + if (!isset($installed['versions'][$packageName]['pretty_version'])) { |
|
195 | + return null; |
|
196 | + } |
|
197 | + |
|
198 | + return $installed['versions'][$packageName]['pretty_version']; |
|
199 | + } |
|
200 | + |
|
201 | + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * @param string $packageName |
|
206 | + * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as reference |
|
207 | + */ |
|
208 | + public static function getReference($packageName) |
|
209 | + { |
|
210 | + foreach (self::getInstalled() as $installed) { |
|
211 | + if (!isset($installed['versions'][$packageName])) { |
|
212 | + continue; |
|
213 | + } |
|
214 | + |
|
215 | + if (!isset($installed['versions'][$packageName]['reference'])) { |
|
216 | + return null; |
|
217 | + } |
|
218 | + |
|
219 | + return $installed['versions'][$packageName]['reference']; |
|
220 | + } |
|
221 | + |
|
222 | + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * @param string $packageName |
|
227 | + * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as install path. Packages of type metapackages also have a null install path. |
|
228 | + */ |
|
229 | + public static function getInstallPath($packageName) |
|
230 | + { |
|
231 | + foreach (self::getInstalled() as $installed) { |
|
232 | + if (!isset($installed['versions'][$packageName])) { |
|
233 | + continue; |
|
234 | + } |
|
235 | + |
|
236 | + return isset($installed['versions'][$packageName]['install_path']) ? $installed['versions'][$packageName]['install_path'] : null; |
|
237 | + } |
|
238 | + |
|
239 | + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * @return array |
|
244 | + * @psalm-return array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string} |
|
245 | + */ |
|
246 | + public static function getRootPackage() |
|
247 | + { |
|
248 | + $installed = self::getInstalled(); |
|
249 | + |
|
250 | + return $installed[0]['root']; |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * Returns the raw installed.php data for custom implementations |
|
255 | + * |
|
256 | + * @deprecated Use getAllRawData() instead which returns all datasets for all autoloaders present in the process. getRawData only returns the first dataset loaded, which may not be what you expect. |
|
257 | + * @return array[] |
|
258 | + * @psalm-return array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}, versions: array<string, array{dev_requirement: bool, pretty_version?: string, version?: string, aliases?: string[], reference?: string, replaced?: string[], provided?: string[], install_path?: string, type?: string}>} |
|
259 | + */ |
|
260 | + public static function getRawData() |
|
261 | + { |
|
262 | + @trigger_error('getRawData only returns the first dataset loaded, which may not be what you expect. Use getAllRawData() instead which returns all datasets for all autoloaders present in the process.', E_USER_DEPRECATED); |
|
263 | + |
|
264 | + if (null === self::$installed) { |
|
265 | + // only require the installed.php file if this file is loaded from its dumped location, |
|
266 | + // and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937 |
|
267 | + if (substr(__DIR__, -8, 1) !== 'C') { |
|
268 | + self::$installed = include __DIR__ . '/installed.php'; |
|
269 | + } else { |
|
270 | + self::$installed = array(); |
|
271 | + } |
|
272 | + } |
|
273 | + |
|
274 | + return self::$installed; |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * Returns the raw data of all installed.php which are currently loaded for custom implementations |
|
279 | + * |
|
280 | + * @return array[] |
|
281 | + * @psalm-return list<array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}, versions: array<string, array{dev_requirement: bool, pretty_version?: string, version?: string, aliases?: string[], reference?: string, replaced?: string[], provided?: string[], install_path?: string, type?: string}>}> |
|
282 | + */ |
|
283 | + public static function getAllRawData() |
|
284 | + { |
|
285 | + return self::getInstalled(); |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * Lets you reload the static array from another file |
|
290 | + * |
|
291 | + * This is only useful for complex integrations in which a project needs to use |
|
292 | + * this class but then also needs to execute another project's autoloader in process, |
|
293 | + * and wants to ensure both projects have access to their version of installed.php. |
|
294 | + * |
|
295 | + * A typical case would be PHPUnit, where it would need to make sure it reads all |
|
296 | + * the data it needs from this class, then call reload() with |
|
297 | + * `require $CWD/vendor/composer/installed.php` (or similar) as input to make sure |
|
298 | + * the project in which it runs can then also use this class safely, without |
|
299 | + * interference between PHPUnit's dependencies and the project's dependencies. |
|
300 | + * |
|
301 | + * @param array[] $data A vendor/composer/installed.php data set |
|
302 | + * @return void |
|
303 | + * |
|
304 | + * @psalm-param array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}, versions: array<string, array{dev_requirement: bool, pretty_version?: string, version?: string, aliases?: string[], reference?: string, replaced?: string[], provided?: string[], install_path?: string, type?: string}>} $data |
|
305 | + */ |
|
306 | + public static function reload($data) |
|
307 | + { |
|
308 | + self::$installed = $data; |
|
309 | + self::$installedByVendor = array(); |
|
310 | + } |
|
311 | + |
|
312 | + /** |
|
313 | + * @return array[] |
|
314 | + * @psalm-return list<array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}, versions: array<string, array{dev_requirement: bool, pretty_version?: string, version?: string, aliases?: string[], reference?: string, replaced?: string[], provided?: string[], install_path?: string, type?: string}>}> |
|
315 | + */ |
|
316 | + private static function getInstalled() |
|
317 | + { |
|
318 | + if (null === self::$canGetVendors) { |
|
319 | + self::$canGetVendors = method_exists('Composer\Autoload\ClassLoader', 'getRegisteredLoaders'); |
|
320 | + } |
|
321 | + |
|
322 | + $installed = array(); |
|
323 | + |
|
324 | + if (self::$canGetVendors) { |
|
325 | + foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) { |
|
326 | + if (isset(self::$installedByVendor[$vendorDir])) { |
|
327 | + $installed[] = self::$installedByVendor[$vendorDir]; |
|
328 | + } elseif (is_file($vendorDir.'/composer/installed.php')) { |
|
329 | + $installed[] = self::$installedByVendor[$vendorDir] = require $vendorDir.'/composer/installed.php'; |
|
330 | + if (null === self::$installed && strtr($vendorDir.'/composer', '\\', '/') === strtr(__DIR__, '\\', '/')) { |
|
331 | + self::$installed = $installed[count($installed) - 1]; |
|
332 | + } |
|
333 | + } |
|
334 | + } |
|
335 | + } |
|
336 | + |
|
337 | + if (null === self::$installed) { |
|
338 | + // only require the installed.php file if this file is loaded from its dumped location, |
|
339 | + // and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937 |
|
340 | + if (substr(__DIR__, -8, 1) !== 'C') { |
|
341 | + self::$installed = require __DIR__ . '/installed.php'; |
|
342 | + } else { |
|
343 | + self::$installed = array(); |
|
344 | + } |
|
345 | + } |
|
346 | + $installed[] = self::$installed; |
|
347 | + |
|
348 | + return $installed; |
|
349 | + } |
|
350 | 350 | } |
@@ -6,7 +6,7 @@ |
||
6 | 6 | $baseDir = dirname($vendorDir); |
7 | 7 | |
8 | 8 | return array( |
9 | - 'ec07570ca5a812141189b1fa81503674' => $vendorDir . '/phpunit/phpunit/src/Framework/Assert/Functions.php', |
|
10 | - '6124b4c8570aa390c21fafd04a26c69f' => $vendorDir . '/myclabs/deep-copy/src/DeepCopy/deep_copy.php', |
|
11 | - '7d3b315c4f303f2fc14aca642a738e50' => $vendorDir . '/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php', |
|
9 | + 'ec07570ca5a812141189b1fa81503674' => $vendorDir . '/phpunit/phpunit/src/Framework/Assert/Functions.php', |
|
10 | + '6124b4c8570aa390c21fafd04a26c69f' => $vendorDir . '/myclabs/deep-copy/src/DeepCopy/deep_copy.php', |
|
11 | + '7d3b315c4f303f2fc14aca642a738e50' => $vendorDir . '/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php', |
|
12 | 12 | ); |
@@ -6,7 +6,7 @@ |
||
6 | 6 | $baseDir = dirname($vendorDir); |
7 | 7 | |
8 | 8 | return array( |
9 | - 'ec07570ca5a812141189b1fa81503674' => $vendorDir . '/phpunit/phpunit/src/Framework/Assert/Functions.php', |
|
10 | - '6124b4c8570aa390c21fafd04a26c69f' => $vendorDir . '/myclabs/deep-copy/src/DeepCopy/deep_copy.php', |
|
11 | - '7d3b315c4f303f2fc14aca642a738e50' => $vendorDir . '/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php', |
|
9 | + 'ec07570ca5a812141189b1fa81503674' => $vendorDir.'/phpunit/phpunit/src/Framework/Assert/Functions.php', |
|
10 | + '6124b4c8570aa390c21fafd04a26c69f' => $vendorDir.'/myclabs/deep-copy/src/DeepCopy/deep_copy.php', |
|
11 | + '7d3b315c4f303f2fc14aca642a738e50' => $vendorDir.'/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php', |
|
12 | 12 | ); |
@@ -23,73 +23,73 @@ |
||
23 | 23 | */ |
24 | 24 | final class Registry |
25 | 25 | { |
26 | - /** @var null|self */ |
|
27 | - private static $instance; |
|
26 | + /** @var null|self */ |
|
27 | + private static $instance; |
|
28 | 28 | |
29 | - /** @var array<string, DocBlock> indexed by class name */ |
|
30 | - private $classDocBlocks = []; |
|
29 | + /** @var array<string, DocBlock> indexed by class name */ |
|
30 | + private $classDocBlocks = []; |
|
31 | 31 | |
32 | - /** @var array<string, array<string, DocBlock>> indexed by class name and method name */ |
|
33 | - private $methodDocBlocks = []; |
|
32 | + /** @var array<string, array<string, DocBlock>> indexed by class name and method name */ |
|
33 | + private $methodDocBlocks = []; |
|
34 | 34 | |
35 | - public static function getInstance(): self |
|
36 | - { |
|
37 | - return self::$instance ?? self::$instance = new self; |
|
38 | - } |
|
35 | + public static function getInstance(): self |
|
36 | + { |
|
37 | + return self::$instance ?? self::$instance = new self; |
|
38 | + } |
|
39 | 39 | |
40 | - private function __construct() |
|
41 | - { |
|
42 | - } |
|
40 | + private function __construct() |
|
41 | + { |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * @throws Exception |
|
46 | - * |
|
47 | - * @psalm-param class-string $class |
|
48 | - */ |
|
49 | - public function forClassName(string $class): DocBlock |
|
50 | - { |
|
51 | - if (array_key_exists($class, $this->classDocBlocks)) { |
|
52 | - return $this->classDocBlocks[$class]; |
|
53 | - } |
|
44 | + /** |
|
45 | + * @throws Exception |
|
46 | + * |
|
47 | + * @psalm-param class-string $class |
|
48 | + */ |
|
49 | + public function forClassName(string $class): DocBlock |
|
50 | + { |
|
51 | + if (array_key_exists($class, $this->classDocBlocks)) { |
|
52 | + return $this->classDocBlocks[$class]; |
|
53 | + } |
|
54 | 54 | |
55 | - try { |
|
56 | - $reflection = new ReflectionClass($class); |
|
57 | - // @codeCoverageIgnoreStart |
|
58 | - } catch (ReflectionException $e) { |
|
59 | - throw new Exception( |
|
60 | - $e->getMessage(), |
|
61 | - (int) $e->getCode(), |
|
62 | - $e |
|
63 | - ); |
|
64 | - } |
|
65 | - // @codeCoverageIgnoreEnd |
|
55 | + try { |
|
56 | + $reflection = new ReflectionClass($class); |
|
57 | + // @codeCoverageIgnoreStart |
|
58 | + } catch (ReflectionException $e) { |
|
59 | + throw new Exception( |
|
60 | + $e->getMessage(), |
|
61 | + (int) $e->getCode(), |
|
62 | + $e |
|
63 | + ); |
|
64 | + } |
|
65 | + // @codeCoverageIgnoreEnd |
|
66 | 66 | |
67 | - return $this->classDocBlocks[$class] = DocBlock::ofClass($reflection); |
|
68 | - } |
|
67 | + return $this->classDocBlocks[$class] = DocBlock::ofClass($reflection); |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * @throws Exception |
|
72 | - * |
|
73 | - * @psalm-param class-string $classInHierarchy |
|
74 | - */ |
|
75 | - public function forMethod(string $classInHierarchy, string $method): DocBlock |
|
76 | - { |
|
77 | - if (isset($this->methodDocBlocks[$classInHierarchy][$method])) { |
|
78 | - return $this->methodDocBlocks[$classInHierarchy][$method]; |
|
79 | - } |
|
70 | + /** |
|
71 | + * @throws Exception |
|
72 | + * |
|
73 | + * @psalm-param class-string $classInHierarchy |
|
74 | + */ |
|
75 | + public function forMethod(string $classInHierarchy, string $method): DocBlock |
|
76 | + { |
|
77 | + if (isset($this->methodDocBlocks[$classInHierarchy][$method])) { |
|
78 | + return $this->methodDocBlocks[$classInHierarchy][$method]; |
|
79 | + } |
|
80 | 80 | |
81 | - try { |
|
82 | - $reflection = new ReflectionMethod($classInHierarchy, $method); |
|
83 | - // @codeCoverageIgnoreStart |
|
84 | - } catch (ReflectionException $e) { |
|
85 | - throw new Exception( |
|
86 | - $e->getMessage(), |
|
87 | - (int) $e->getCode(), |
|
88 | - $e |
|
89 | - ); |
|
90 | - } |
|
91 | - // @codeCoverageIgnoreEnd |
|
81 | + try { |
|
82 | + $reflection = new ReflectionMethod($classInHierarchy, $method); |
|
83 | + // @codeCoverageIgnoreStart |
|
84 | + } catch (ReflectionException $e) { |
|
85 | + throw new Exception( |
|
86 | + $e->getMessage(), |
|
87 | + (int) $e->getCode(), |
|
88 | + $e |
|
89 | + ); |
|
90 | + } |
|
91 | + // @codeCoverageIgnoreEnd |
|
92 | 92 | |
93 | - return $this->methodDocBlocks[$classInHierarchy][$method] = DocBlock::ofMethod($reflection, $classInHierarchy); |
|
94 | - } |
|
93 | + return $this->methodDocBlocks[$classInHierarchy][$method] = DocBlock::ofMethod($reflection, $classInHierarchy); |
|
94 | + } |
|
95 | 95 | } |
@@ -14,26 +14,26 @@ |
||
14 | 14 | */ |
15 | 15 | final class Type |
16 | 16 | { |
17 | - public static function isType(string $type): bool |
|
18 | - { |
|
19 | - switch ($type) { |
|
20 | - case 'numeric': |
|
21 | - case 'integer': |
|
22 | - case 'int': |
|
23 | - case 'iterable': |
|
24 | - case 'float': |
|
25 | - case 'string': |
|
26 | - case 'boolean': |
|
27 | - case 'bool': |
|
28 | - case 'null': |
|
29 | - case 'array': |
|
30 | - case 'object': |
|
31 | - case 'resource': |
|
32 | - case 'scalar': |
|
33 | - return true; |
|
17 | + public static function isType(string $type): bool |
|
18 | + { |
|
19 | + switch ($type) { |
|
20 | + case 'numeric': |
|
21 | + case 'integer': |
|
22 | + case 'int': |
|
23 | + case 'iterable': |
|
24 | + case 'float': |
|
25 | + case 'string': |
|
26 | + case 'boolean': |
|
27 | + case 'bool': |
|
28 | + case 'null': |
|
29 | + case 'array': |
|
30 | + case 'object': |
|
31 | + case 'resource': |
|
32 | + case 'scalar': |
|
33 | + return true; |
|
34 | 34 | |
35 | - default: |
|
36 | - return false; |
|
37 | - } |
|
38 | - } |
|
35 | + default: |
|
36 | + return false; |
|
37 | + } |
|
38 | + } |
|
39 | 39 | } |
@@ -23,69 +23,69 @@ |
||
23 | 23 | */ |
24 | 24 | final class XmlTestListRenderer |
25 | 25 | { |
26 | - /** |
|
27 | - * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
|
28 | - */ |
|
29 | - public function render(TestSuite $suite): string |
|
30 | - { |
|
31 | - $writer = new XMLWriter; |
|
26 | + /** |
|
27 | + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
|
28 | + */ |
|
29 | + public function render(TestSuite $suite): string |
|
30 | + { |
|
31 | + $writer = new XMLWriter; |
|
32 | 32 | |
33 | - $writer->openMemory(); |
|
34 | - $writer->setIndent(true); |
|
35 | - $writer->startDocument('1.0', 'UTF-8'); |
|
36 | - $writer->startElement('tests'); |
|
33 | + $writer->openMemory(); |
|
34 | + $writer->setIndent(true); |
|
35 | + $writer->startDocument('1.0', 'UTF-8'); |
|
36 | + $writer->startElement('tests'); |
|
37 | 37 | |
38 | - $currentTestCase = null; |
|
38 | + $currentTestCase = null; |
|
39 | 39 | |
40 | - foreach (new RecursiveIteratorIterator($suite->getIterator()) as $test) { |
|
41 | - if ($test instanceof TestCase) { |
|
42 | - if (get_class($test) !== $currentTestCase) { |
|
43 | - if ($currentTestCase !== null) { |
|
44 | - $writer->endElement(); |
|
45 | - } |
|
40 | + foreach (new RecursiveIteratorIterator($suite->getIterator()) as $test) { |
|
41 | + if ($test instanceof TestCase) { |
|
42 | + if (get_class($test) !== $currentTestCase) { |
|
43 | + if ($currentTestCase !== null) { |
|
44 | + $writer->endElement(); |
|
45 | + } |
|
46 | 46 | |
47 | - $writer->startElement('testCaseClass'); |
|
48 | - $writer->writeAttribute('name', get_class($test)); |
|
47 | + $writer->startElement('testCaseClass'); |
|
48 | + $writer->writeAttribute('name', get_class($test)); |
|
49 | 49 | |
50 | - $currentTestCase = get_class($test); |
|
51 | - } |
|
50 | + $currentTestCase = get_class($test); |
|
51 | + } |
|
52 | 52 | |
53 | - $writer->startElement('testCaseMethod'); |
|
54 | - $writer->writeAttribute('name', $test->getName(false)); |
|
55 | - $writer->writeAttribute('groups', implode(',', $test->getGroups())); |
|
53 | + $writer->startElement('testCaseMethod'); |
|
54 | + $writer->writeAttribute('name', $test->getName(false)); |
|
55 | + $writer->writeAttribute('groups', implode(',', $test->getGroups())); |
|
56 | 56 | |
57 | - if (!empty($test->getDataSetAsString(false))) { |
|
58 | - $writer->writeAttribute( |
|
59 | - 'dataSet', |
|
60 | - str_replace( |
|
61 | - ' with data set ', |
|
62 | - '', |
|
63 | - $test->getDataSetAsString(false) |
|
64 | - ) |
|
65 | - ); |
|
66 | - } |
|
57 | + if (!empty($test->getDataSetAsString(false))) { |
|
58 | + $writer->writeAttribute( |
|
59 | + 'dataSet', |
|
60 | + str_replace( |
|
61 | + ' with data set ', |
|
62 | + '', |
|
63 | + $test->getDataSetAsString(false) |
|
64 | + ) |
|
65 | + ); |
|
66 | + } |
|
67 | 67 | |
68 | - $writer->endElement(); |
|
69 | - } elseif ($test instanceof PhptTestCase) { |
|
70 | - if ($currentTestCase !== null) { |
|
71 | - $writer->endElement(); |
|
68 | + $writer->endElement(); |
|
69 | + } elseif ($test instanceof PhptTestCase) { |
|
70 | + if ($currentTestCase !== null) { |
|
71 | + $writer->endElement(); |
|
72 | 72 | |
73 | - $currentTestCase = null; |
|
74 | - } |
|
73 | + $currentTestCase = null; |
|
74 | + } |
|
75 | 75 | |
76 | - $writer->startElement('phptFile'); |
|
77 | - $writer->writeAttribute('path', $test->getName()); |
|
78 | - $writer->endElement(); |
|
79 | - } |
|
80 | - } |
|
76 | + $writer->startElement('phptFile'); |
|
77 | + $writer->writeAttribute('path', $test->getName()); |
|
78 | + $writer->endElement(); |
|
79 | + } |
|
80 | + } |
|
81 | 81 | |
82 | - if ($currentTestCase !== null) { |
|
83 | - $writer->endElement(); |
|
84 | - } |
|
82 | + if ($currentTestCase !== null) { |
|
83 | + $writer->endElement(); |
|
84 | + } |
|
85 | 85 | |
86 | - $writer->endElement(); |
|
87 | - $writer->endDocument(); |
|
86 | + $writer->endElement(); |
|
87 | + $writer->endDocument(); |
|
88 | 88 | |
89 | - return $writer->outputMemory(); |
|
90 | - } |
|
89 | + return $writer->outputMemory(); |
|
90 | + } |
|
91 | 91 | } |
@@ -16,19 +16,19 @@ |
||
16 | 16 | */ |
17 | 17 | final class Cloner |
18 | 18 | { |
19 | - /** |
|
20 | - * @psalm-template OriginalType |
|
21 | - * |
|
22 | - * @psalm-param OriginalType $original |
|
23 | - * |
|
24 | - * @psalm-return OriginalType |
|
25 | - */ |
|
26 | - public static function clone(object $original): object |
|
27 | - { |
|
28 | - try { |
|
29 | - return clone $original; |
|
30 | - } catch (Throwable $t) { |
|
31 | - return $original; |
|
32 | - } |
|
33 | - } |
|
19 | + /** |
|
20 | + * @psalm-template OriginalType |
|
21 | + * |
|
22 | + * @psalm-param OriginalType $original |
|
23 | + * |
|
24 | + * @psalm-return OriginalType |
|
25 | + */ |
|
26 | + public static function clone(object $original): object |
|
27 | + { |
|
28 | + try { |
|
29 | + return clone $original; |
|
30 | + } catch (Throwable $t) { |
|
31 | + return $original; |
|
32 | + } |
|
33 | + } |
|
34 | 34 | } |
@@ -22,63 +22,63 @@ |
||
22 | 22 | */ |
23 | 23 | final class FileLoader |
24 | 24 | { |
25 | - /** |
|
26 | - * Checks if a PHP sourcecode file is readable. The sourcecode file is loaded through the load() method. |
|
27 | - * |
|
28 | - * As a fallback, PHP looks in the directory of the file executing the stream_resolve_include_path function. |
|
29 | - * We do not want to load the Test.php file here, so skip it if it found that. |
|
30 | - * PHP prioritizes the include_path setting, so if the current directory is in there, it will first look in the |
|
31 | - * current working directory. |
|
32 | - * |
|
33 | - * @throws Exception |
|
34 | - */ |
|
35 | - public static function checkAndLoad(string $filename): string |
|
36 | - { |
|
37 | - $includePathFilename = stream_resolve_include_path($filename); |
|
25 | + /** |
|
26 | + * Checks if a PHP sourcecode file is readable. The sourcecode file is loaded through the load() method. |
|
27 | + * |
|
28 | + * As a fallback, PHP looks in the directory of the file executing the stream_resolve_include_path function. |
|
29 | + * We do not want to load the Test.php file here, so skip it if it found that. |
|
30 | + * PHP prioritizes the include_path setting, so if the current directory is in there, it will first look in the |
|
31 | + * current working directory. |
|
32 | + * |
|
33 | + * @throws Exception |
|
34 | + */ |
|
35 | + public static function checkAndLoad(string $filename): string |
|
36 | + { |
|
37 | + $includePathFilename = stream_resolve_include_path($filename); |
|
38 | 38 | |
39 | - $localFile = __DIR__ . DIRECTORY_SEPARATOR . $filename; |
|
39 | + $localFile = __DIR__ . DIRECTORY_SEPARATOR . $filename; |
|
40 | 40 | |
41 | - if (!$includePathFilename || |
|
42 | - $includePathFilename === $localFile || |
|
43 | - !self::isReadable($includePathFilename)) { |
|
44 | - throw new Exception( |
|
45 | - sprintf('Cannot open file "%s".' . "\n", $filename) |
|
46 | - ); |
|
47 | - } |
|
41 | + if (!$includePathFilename || |
|
42 | + $includePathFilename === $localFile || |
|
43 | + !self::isReadable($includePathFilename)) { |
|
44 | + throw new Exception( |
|
45 | + sprintf('Cannot open file "%s".' . "\n", $filename) |
|
46 | + ); |
|
47 | + } |
|
48 | 48 | |
49 | - self::load($includePathFilename); |
|
49 | + self::load($includePathFilename); |
|
50 | 50 | |
51 | - return $includePathFilename; |
|
52 | - } |
|
51 | + return $includePathFilename; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Loads a PHP sourcefile. |
|
56 | - */ |
|
57 | - public static function load(string $filename): void |
|
58 | - { |
|
59 | - $oldVariableNames = array_keys(get_defined_vars()); |
|
54 | + /** |
|
55 | + * Loads a PHP sourcefile. |
|
56 | + */ |
|
57 | + public static function load(string $filename): void |
|
58 | + { |
|
59 | + $oldVariableNames = array_keys(get_defined_vars()); |
|
60 | 60 | |
61 | - /** |
|
62 | - * @noinspection PhpIncludeInspection |
|
63 | - * |
|
64 | - * @psalm-suppress UnresolvableInclude |
|
65 | - */ |
|
66 | - include_once $filename; |
|
61 | + /** |
|
62 | + * @noinspection PhpIncludeInspection |
|
63 | + * |
|
64 | + * @psalm-suppress UnresolvableInclude |
|
65 | + */ |
|
66 | + include_once $filename; |
|
67 | 67 | |
68 | - $newVariables = get_defined_vars(); |
|
68 | + $newVariables = get_defined_vars(); |
|
69 | 69 | |
70 | - foreach (array_diff(array_keys($newVariables), $oldVariableNames) as $variableName) { |
|
71 | - if ($variableName !== 'oldVariableNames') { |
|
72 | - $GLOBALS[$variableName] = $newVariables[$variableName]; |
|
73 | - } |
|
74 | - } |
|
75 | - } |
|
70 | + foreach (array_diff(array_keys($newVariables), $oldVariableNames) as $variableName) { |
|
71 | + if ($variableName !== 'oldVariableNames') { |
|
72 | + $GLOBALS[$variableName] = $newVariables[$variableName]; |
|
73 | + } |
|
74 | + } |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * @see https://github.com/sebastianbergmann/phpunit/pull/2751 |
|
79 | - */ |
|
80 | - private static function isReadable(string $filename): bool |
|
81 | - { |
|
82 | - return @fopen($filename, 'r') !== false; |
|
83 | - } |
|
77 | + /** |
|
78 | + * @see https://github.com/sebastianbergmann/phpunit/pull/2751 |
|
79 | + */ |
|
80 | + private static function isReadable(string $filename): bool |
|
81 | + { |
|
82 | + return @fopen($filename, 'r') !== false; |
|
83 | + } |
|
84 | 84 | } |
@@ -58,726 +58,726 @@ |
||
58 | 58 | */ |
59 | 59 | final class Test |
60 | 60 | { |
61 | - /** |
|
62 | - * @var int |
|
63 | - */ |
|
64 | - public const UNKNOWN = -1; |
|
65 | - |
|
66 | - /** |
|
67 | - * @var int |
|
68 | - */ |
|
69 | - public const SMALL = 0; |
|
70 | - |
|
71 | - /** |
|
72 | - * @var int |
|
73 | - */ |
|
74 | - public const MEDIUM = 1; |
|
75 | - |
|
76 | - /** |
|
77 | - * @var int |
|
78 | - */ |
|
79 | - public const LARGE = 2; |
|
80 | - |
|
81 | - /** |
|
82 | - * @var array |
|
83 | - */ |
|
84 | - private static $hookMethods = []; |
|
85 | - |
|
86 | - /** |
|
87 | - * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
|
88 | - */ |
|
89 | - public static function describe(\PHPUnit\Framework\Test $test): array |
|
90 | - { |
|
91 | - if ($test instanceof TestCase) { |
|
92 | - return [get_class($test), $test->getName()]; |
|
93 | - } |
|
94 | - |
|
95 | - if ($test instanceof SelfDescribing) { |
|
96 | - return ['', $test->toString()]; |
|
97 | - } |
|
98 | - |
|
99 | - return ['', get_class($test)]; |
|
100 | - } |
|
101 | - |
|
102 | - public static function describeAsString(\PHPUnit\Framework\Test $test): string |
|
103 | - { |
|
104 | - if ($test instanceof SelfDescribing) { |
|
105 | - return $test->toString(); |
|
106 | - } |
|
107 | - |
|
108 | - return get_class($test); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * @throws CodeCoverageException |
|
113 | - * |
|
114 | - * @return array|bool |
|
115 | - * |
|
116 | - * @psalm-param class-string $className |
|
117 | - */ |
|
118 | - public static function getLinesToBeCovered(string $className, string $methodName) |
|
119 | - { |
|
120 | - $annotations = self::parseTestMethodAnnotations( |
|
121 | - $className, |
|
122 | - $methodName |
|
123 | - ); |
|
124 | - |
|
125 | - if (!self::shouldCoversAnnotationBeUsed($annotations)) { |
|
126 | - return false; |
|
127 | - } |
|
128 | - |
|
129 | - return self::getLinesToBeCoveredOrUsed($className, $methodName, 'covers'); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Returns lines of code specified with the @uses annotation. |
|
134 | - * |
|
135 | - * @throws CodeCoverageException |
|
136 | - * |
|
137 | - * @psalm-param class-string $className |
|
138 | - */ |
|
139 | - public static function getLinesToBeUsed(string $className, string $methodName): array |
|
140 | - { |
|
141 | - return self::getLinesToBeCoveredOrUsed($className, $methodName, 'uses'); |
|
142 | - } |
|
143 | - |
|
144 | - public static function requiresCodeCoverageDataCollection(TestCase $test): bool |
|
145 | - { |
|
146 | - $annotations = self::parseTestMethodAnnotations( |
|
147 | - get_class($test), |
|
148 | - $test->getName(false) |
|
149 | - ); |
|
150 | - |
|
151 | - // If there is no @covers annotation but a @coversNothing annotation on |
|
152 | - // the test method then code coverage data does not need to be collected |
|
153 | - if (isset($annotations['method']['coversNothing'])) { |
|
154 | - // @see https://github.com/sebastianbergmann/phpunit/issues/4947#issuecomment-1084480950 |
|
155 | - // return false; |
|
156 | - } |
|
157 | - |
|
158 | - // If there is at least one @covers annotation then |
|
159 | - // code coverage data needs to be collected |
|
160 | - if (isset($annotations['method']['covers'])) { |
|
161 | - return true; |
|
162 | - } |
|
163 | - |
|
164 | - // If there is no @covers annotation but a @coversNothing annotation |
|
165 | - // then code coverage data does not need to be collected |
|
166 | - if (isset($annotations['class']['coversNothing'])) { |
|
167 | - // @see https://github.com/sebastianbergmann/phpunit/issues/4947#issuecomment-1084480950 |
|
168 | - // return false; |
|
169 | - } |
|
170 | - |
|
171 | - // If there is no @coversNothing annotation then |
|
172 | - // code coverage data may be collected |
|
173 | - return true; |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * @throws Exception |
|
178 | - * |
|
179 | - * @psalm-param class-string $className |
|
180 | - */ |
|
181 | - public static function getRequirements(string $className, string $methodName): array |
|
182 | - { |
|
183 | - return self::mergeArraysRecursively( |
|
184 | - Registry::getInstance()->forClassName($className)->requirements(), |
|
185 | - Registry::getInstance()->forMethod($className, $methodName)->requirements() |
|
186 | - ); |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Returns the missing requirements for a test. |
|
191 | - * |
|
192 | - * @throws Exception |
|
193 | - * @throws Warning |
|
194 | - * |
|
195 | - * @psalm-param class-string $className |
|
196 | - */ |
|
197 | - public static function getMissingRequirements(string $className, string $methodName): array |
|
198 | - { |
|
199 | - $required = self::getRequirements($className, $methodName); |
|
200 | - $missing = []; |
|
201 | - $hint = null; |
|
202 | - |
|
203 | - if (!empty($required['PHP'])) { |
|
204 | - $operator = new VersionComparisonOperator(empty($required['PHP']['operator']) ? '>=' : $required['PHP']['operator']); |
|
205 | - |
|
206 | - if (!version_compare(PHP_VERSION, $required['PHP']['version'], $operator->asString())) { |
|
207 | - $missing[] = sprintf('PHP %s %s is required.', $operator->asString(), $required['PHP']['version']); |
|
208 | - $hint = 'PHP'; |
|
209 | - } |
|
210 | - } elseif (!empty($required['PHP_constraint'])) { |
|
211 | - $version = new \PharIo\Version\Version(self::sanitizeVersionNumber(PHP_VERSION)); |
|
212 | - |
|
213 | - if (!$required['PHP_constraint']['constraint']->complies($version)) { |
|
214 | - $missing[] = sprintf( |
|
215 | - 'PHP version does not match the required constraint %s.', |
|
216 | - $required['PHP_constraint']['constraint']->asString() |
|
217 | - ); |
|
218 | - |
|
219 | - $hint = 'PHP_constraint'; |
|
220 | - } |
|
221 | - } |
|
222 | - |
|
223 | - if (!empty($required['PHPUnit'])) { |
|
224 | - $phpunitVersion = Version::id(); |
|
225 | - |
|
226 | - $operator = new VersionComparisonOperator(empty($required['PHPUnit']['operator']) ? '>=' : $required['PHPUnit']['operator']); |
|
227 | - |
|
228 | - if (!version_compare($phpunitVersion, $required['PHPUnit']['version'], $operator->asString())) { |
|
229 | - $missing[] = sprintf('PHPUnit %s %s is required.', $operator->asString(), $required['PHPUnit']['version']); |
|
230 | - $hint = $hint ?? 'PHPUnit'; |
|
231 | - } |
|
232 | - } elseif (!empty($required['PHPUnit_constraint'])) { |
|
233 | - $phpunitVersion = new \PharIo\Version\Version(self::sanitizeVersionNumber(Version::id())); |
|
234 | - |
|
235 | - if (!$required['PHPUnit_constraint']['constraint']->complies($phpunitVersion)) { |
|
236 | - $missing[] = sprintf( |
|
237 | - 'PHPUnit version does not match the required constraint %s.', |
|
238 | - $required['PHPUnit_constraint']['constraint']->asString() |
|
239 | - ); |
|
240 | - |
|
241 | - $hint = $hint ?? 'PHPUnit_constraint'; |
|
242 | - } |
|
243 | - } |
|
244 | - |
|
245 | - if (!empty($required['OSFAMILY']) && $required['OSFAMILY'] !== (new OperatingSystem)->getFamily()) { |
|
246 | - $missing[] = sprintf('Operating system %s is required.', $required['OSFAMILY']); |
|
247 | - $hint = $hint ?? 'OSFAMILY'; |
|
248 | - } |
|
249 | - |
|
250 | - if (!empty($required['OS'])) { |
|
251 | - $requiredOsPattern = sprintf('/%s/i', addcslashes($required['OS'], '/')); |
|
252 | - |
|
253 | - if (!preg_match($requiredOsPattern, PHP_OS)) { |
|
254 | - $missing[] = sprintf('Operating system matching %s is required.', $requiredOsPattern); |
|
255 | - $hint = $hint ?? 'OS'; |
|
256 | - } |
|
257 | - } |
|
258 | - |
|
259 | - if (!empty($required['functions'])) { |
|
260 | - foreach ($required['functions'] as $function) { |
|
261 | - $pieces = explode('::', $function); |
|
262 | - |
|
263 | - if (count($pieces) === 2 && class_exists($pieces[0]) && method_exists($pieces[0], $pieces[1])) { |
|
264 | - continue; |
|
265 | - } |
|
266 | - |
|
267 | - if (function_exists($function)) { |
|
268 | - continue; |
|
269 | - } |
|
270 | - |
|
271 | - $missing[] = sprintf('Function %s is required.', $function); |
|
272 | - $hint = $hint ?? 'function_' . $function; |
|
273 | - } |
|
274 | - } |
|
275 | - |
|
276 | - if (!empty($required['setting'])) { |
|
277 | - foreach ($required['setting'] as $setting => $value) { |
|
278 | - if (ini_get($setting) !== $value) { |
|
279 | - $missing[] = sprintf('Setting "%s" must be "%s".', $setting, $value); |
|
280 | - $hint = $hint ?? '__SETTING_' . $setting; |
|
281 | - } |
|
282 | - } |
|
283 | - } |
|
284 | - |
|
285 | - if (!empty($required['extensions'])) { |
|
286 | - foreach ($required['extensions'] as $extension) { |
|
287 | - if (isset($required['extension_versions'][$extension])) { |
|
288 | - continue; |
|
289 | - } |
|
290 | - |
|
291 | - if (!extension_loaded($extension)) { |
|
292 | - $missing[] = sprintf('Extension %s is required.', $extension); |
|
293 | - $hint = $hint ?? 'extension_' . $extension; |
|
294 | - } |
|
295 | - } |
|
296 | - } |
|
297 | - |
|
298 | - if (!empty($required['extension_versions'])) { |
|
299 | - foreach ($required['extension_versions'] as $extension => $req) { |
|
300 | - $actualVersion = phpversion($extension); |
|
301 | - |
|
302 | - $operator = new VersionComparisonOperator(empty($req['operator']) ? '>=' : $req['operator']); |
|
303 | - |
|
304 | - if ($actualVersion === false || !version_compare($actualVersion, $req['version'], $operator->asString())) { |
|
305 | - $missing[] = sprintf('Extension %s %s %s is required.', $extension, $operator->asString(), $req['version']); |
|
306 | - $hint = $hint ?? 'extension_' . $extension; |
|
307 | - } |
|
308 | - } |
|
309 | - } |
|
310 | - |
|
311 | - if ($hint && isset($required['__OFFSET'])) { |
|
312 | - array_unshift($missing, '__OFFSET_FILE=' . $required['__OFFSET']['__FILE']); |
|
313 | - array_unshift($missing, '__OFFSET_LINE=' . ($required['__OFFSET'][$hint] ?? 1)); |
|
314 | - } |
|
315 | - |
|
316 | - return $missing; |
|
317 | - } |
|
318 | - |
|
319 | - /** |
|
320 | - * Returns the provided data for a method. |
|
321 | - * |
|
322 | - * @throws Exception |
|
323 | - * |
|
324 | - * @psalm-param class-string $className |
|
325 | - */ |
|
326 | - public static function getProvidedData(string $className, string $methodName): ?array |
|
327 | - { |
|
328 | - return Registry::getInstance()->forMethod($className, $methodName)->getProvidedData(); |
|
329 | - } |
|
330 | - |
|
331 | - /** |
|
332 | - * @psalm-param class-string $className |
|
333 | - */ |
|
334 | - public static function parseTestMethodAnnotations(string $className, ?string $methodName = ''): array |
|
335 | - { |
|
336 | - $registry = Registry::getInstance(); |
|
337 | - |
|
338 | - if ($methodName !== null) { |
|
339 | - try { |
|
340 | - return [ |
|
341 | - 'method' => $registry->forMethod($className, $methodName)->symbolAnnotations(), |
|
342 | - 'class' => $registry->forClassName($className)->symbolAnnotations(), |
|
343 | - ]; |
|
344 | - } catch (Exception $methodNotFound) { |
|
345 | - // ignored |
|
346 | - } |
|
347 | - } |
|
348 | - |
|
349 | - return [ |
|
350 | - 'method' => null, |
|
351 | - 'class' => $registry->forClassName($className)->symbolAnnotations(), |
|
352 | - ]; |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * @psalm-param class-string $className |
|
357 | - */ |
|
358 | - public static function getInlineAnnotations(string $className, string $methodName): array |
|
359 | - { |
|
360 | - return Registry::getInstance()->forMethod($className, $methodName)->getInlineAnnotations(); |
|
361 | - } |
|
362 | - |
|
363 | - /** @psalm-param class-string $className */ |
|
364 | - public static function getBackupSettings(string $className, string $methodName): array |
|
365 | - { |
|
366 | - return [ |
|
367 | - 'backupGlobals' => self::getBooleanAnnotationSetting( |
|
368 | - $className, |
|
369 | - $methodName, |
|
370 | - 'backupGlobals' |
|
371 | - ), |
|
372 | - 'backupStaticAttributes' => self::getBooleanAnnotationSetting( |
|
373 | - $className, |
|
374 | - $methodName, |
|
375 | - 'backupStaticAttributes' |
|
376 | - ), |
|
377 | - ]; |
|
378 | - } |
|
379 | - |
|
380 | - /** |
|
381 | - * @psalm-param class-string $className |
|
382 | - * |
|
383 | - * @return ExecutionOrderDependency[] |
|
384 | - */ |
|
385 | - public static function getDependencies(string $className, string $methodName): array |
|
386 | - { |
|
387 | - $annotations = self::parseTestMethodAnnotations( |
|
388 | - $className, |
|
389 | - $methodName |
|
390 | - ); |
|
391 | - |
|
392 | - $dependsAnnotations = $annotations['class']['depends'] ?? []; |
|
393 | - |
|
394 | - if (isset($annotations['method']['depends'])) { |
|
395 | - $dependsAnnotations = array_merge( |
|
396 | - $dependsAnnotations, |
|
397 | - $annotations['method']['depends'] |
|
398 | - ); |
|
399 | - } |
|
400 | - |
|
401 | - // Normalize dependency name to className::methodName |
|
402 | - $dependencies = []; |
|
403 | - |
|
404 | - foreach ($dependsAnnotations as $value) { |
|
405 | - $dependencies[] = ExecutionOrderDependency::createFromDependsAnnotation($className, $value); |
|
406 | - } |
|
407 | - |
|
408 | - return array_unique($dependencies); |
|
409 | - } |
|
410 | - |
|
411 | - /** @psalm-param class-string $className */ |
|
412 | - public static function getGroups(string $className, ?string $methodName = ''): array |
|
413 | - { |
|
414 | - $annotations = self::parseTestMethodAnnotations( |
|
415 | - $className, |
|
416 | - $methodName |
|
417 | - ); |
|
418 | - |
|
419 | - $groups = []; |
|
420 | - |
|
421 | - if (isset($annotations['method']['author'])) { |
|
422 | - $groups[] = $annotations['method']['author']; |
|
423 | - } elseif (isset($annotations['class']['author'])) { |
|
424 | - $groups[] = $annotations['class']['author']; |
|
425 | - } |
|
426 | - |
|
427 | - if (isset($annotations['class']['group'])) { |
|
428 | - $groups[] = $annotations['class']['group']; |
|
429 | - } |
|
430 | - |
|
431 | - if (isset($annotations['method']['group'])) { |
|
432 | - $groups[] = $annotations['method']['group']; |
|
433 | - } |
|
434 | - |
|
435 | - if (isset($annotations['class']['ticket'])) { |
|
436 | - $groups[] = $annotations['class']['ticket']; |
|
437 | - } |
|
438 | - |
|
439 | - if (isset($annotations['method']['ticket'])) { |
|
440 | - $groups[] = $annotations['method']['ticket']; |
|
441 | - } |
|
442 | - |
|
443 | - foreach (['method', 'class'] as $element) { |
|
444 | - foreach (['small', 'medium', 'large'] as $size) { |
|
445 | - if (isset($annotations[$element][$size])) { |
|
446 | - $groups[] = [$size]; |
|
447 | - |
|
448 | - break 2; |
|
449 | - } |
|
450 | - } |
|
451 | - } |
|
452 | - |
|
453 | - foreach (['method', 'class'] as $element) { |
|
454 | - if (isset($annotations[$element]['covers'])) { |
|
455 | - foreach ($annotations[$element]['covers'] as $coversTarget) { |
|
456 | - $groups[] = ['__phpunit_covers_' . self::canonicalizeName($coversTarget)]; |
|
457 | - } |
|
458 | - } |
|
459 | - |
|
460 | - if (isset($annotations[$element]['uses'])) { |
|
461 | - foreach ($annotations[$element]['uses'] as $usesTarget) { |
|
462 | - $groups[] = ['__phpunit_uses_' . self::canonicalizeName($usesTarget)]; |
|
463 | - } |
|
464 | - } |
|
465 | - } |
|
466 | - |
|
467 | - return array_unique(array_merge([], ...$groups)); |
|
468 | - } |
|
469 | - |
|
470 | - /** @psalm-param class-string $className */ |
|
471 | - public static function getSize(string $className, ?string $methodName): int |
|
472 | - { |
|
473 | - $groups = array_flip(self::getGroups($className, $methodName)); |
|
474 | - |
|
475 | - if (isset($groups['large'])) { |
|
476 | - return self::LARGE; |
|
477 | - } |
|
478 | - |
|
479 | - if (isset($groups['medium'])) { |
|
480 | - return self::MEDIUM; |
|
481 | - } |
|
482 | - |
|
483 | - if (isset($groups['small'])) { |
|
484 | - return self::SMALL; |
|
485 | - } |
|
486 | - |
|
487 | - return self::UNKNOWN; |
|
488 | - } |
|
489 | - |
|
490 | - /** @psalm-param class-string $className */ |
|
491 | - public static function getProcessIsolationSettings(string $className, string $methodName): bool |
|
492 | - { |
|
493 | - $annotations = self::parseTestMethodAnnotations( |
|
494 | - $className, |
|
495 | - $methodName |
|
496 | - ); |
|
497 | - |
|
498 | - return isset($annotations['class']['runTestsInSeparateProcesses']) || isset($annotations['method']['runInSeparateProcess']); |
|
499 | - } |
|
500 | - |
|
501 | - /** @psalm-param class-string $className */ |
|
502 | - public static function getClassProcessIsolationSettings(string $className, string $methodName): bool |
|
503 | - { |
|
504 | - $annotations = self::parseTestMethodAnnotations( |
|
505 | - $className, |
|
506 | - $methodName |
|
507 | - ); |
|
508 | - |
|
509 | - return isset($annotations['class']['runClassInSeparateProcess']); |
|
510 | - } |
|
511 | - |
|
512 | - /** @psalm-param class-string $className */ |
|
513 | - public static function getPreserveGlobalStateSettings(string $className, string $methodName): ?bool |
|
514 | - { |
|
515 | - return self::getBooleanAnnotationSetting( |
|
516 | - $className, |
|
517 | - $methodName, |
|
518 | - 'preserveGlobalState' |
|
519 | - ); |
|
520 | - } |
|
521 | - |
|
522 | - /** @psalm-param class-string $className */ |
|
523 | - public static function getHookMethods(string $className): array |
|
524 | - { |
|
525 | - if (!class_exists($className, false)) { |
|
526 | - return self::emptyHookMethodsArray(); |
|
527 | - } |
|
528 | - |
|
529 | - if (!isset(self::$hookMethods[$className])) { |
|
530 | - self::$hookMethods[$className] = self::emptyHookMethodsArray(); |
|
531 | - |
|
532 | - try { |
|
533 | - foreach ((new Reflection)->methodsInTestClass(new ReflectionClass($className)) as $method) { |
|
534 | - $docBlock = Registry::getInstance()->forMethod($className, $method->getName()); |
|
535 | - |
|
536 | - if ($method->isStatic()) { |
|
537 | - if ($docBlock->isHookToBeExecutedBeforeClass()) { |
|
538 | - array_unshift( |
|
539 | - self::$hookMethods[$className]['beforeClass'], |
|
540 | - $method->getName() |
|
541 | - ); |
|
542 | - } |
|
543 | - |
|
544 | - if ($docBlock->isHookToBeExecutedAfterClass()) { |
|
545 | - self::$hookMethods[$className]['afterClass'][] = $method->getName(); |
|
546 | - } |
|
547 | - } |
|
548 | - |
|
549 | - if ($docBlock->isToBeExecutedBeforeTest()) { |
|
550 | - array_unshift( |
|
551 | - self::$hookMethods[$className]['before'], |
|
552 | - $method->getName() |
|
553 | - ); |
|
554 | - } |
|
555 | - |
|
556 | - if ($docBlock->isToBeExecutedAsPreCondition()) { |
|
557 | - array_unshift( |
|
558 | - self::$hookMethods[$className]['preCondition'], |
|
559 | - $method->getName() |
|
560 | - ); |
|
561 | - } |
|
562 | - |
|
563 | - if ($docBlock->isToBeExecutedAsPostCondition()) { |
|
564 | - self::$hookMethods[$className]['postCondition'][] = $method->getName(); |
|
565 | - } |
|
566 | - |
|
567 | - if ($docBlock->isToBeExecutedAfterTest()) { |
|
568 | - self::$hookMethods[$className]['after'][] = $method->getName(); |
|
569 | - } |
|
570 | - } |
|
571 | - } catch (ReflectionException $e) { |
|
572 | - } |
|
573 | - } |
|
574 | - |
|
575 | - return self::$hookMethods[$className]; |
|
576 | - } |
|
577 | - |
|
578 | - public static function isTestMethod(ReflectionMethod $method): bool |
|
579 | - { |
|
580 | - if (!$method->isPublic()) { |
|
581 | - return false; |
|
582 | - } |
|
583 | - |
|
584 | - if (strpos($method->getName(), 'test') === 0) { |
|
585 | - return true; |
|
586 | - } |
|
587 | - |
|
588 | - return array_key_exists( |
|
589 | - 'test', |
|
590 | - Registry::getInstance()->forMethod( |
|
591 | - $method->getDeclaringClass()->getName(), |
|
592 | - $method->getName() |
|
593 | - ) |
|
594 | - ->symbolAnnotations() |
|
595 | - ); |
|
596 | - } |
|
597 | - |
|
598 | - /** |
|
599 | - * @throws CodeCoverageException |
|
600 | - * |
|
601 | - * @psalm-param class-string $className |
|
602 | - */ |
|
603 | - private static function getLinesToBeCoveredOrUsed(string $className, string $methodName, string $mode): array |
|
604 | - { |
|
605 | - $annotations = self::parseTestMethodAnnotations( |
|
606 | - $className, |
|
607 | - $methodName |
|
608 | - ); |
|
609 | - |
|
610 | - $classShortcut = null; |
|
611 | - |
|
612 | - if (!empty($annotations['class'][$mode . 'DefaultClass'])) { |
|
613 | - if (count($annotations['class'][$mode . 'DefaultClass']) > 1) { |
|
614 | - throw new CodeCoverageException( |
|
615 | - sprintf( |
|
616 | - 'More than one @%sClass annotation in class or interface "%s".', |
|
617 | - $mode, |
|
618 | - $className |
|
619 | - ) |
|
620 | - ); |
|
621 | - } |
|
622 | - |
|
623 | - $classShortcut = $annotations['class'][$mode . 'DefaultClass'][0]; |
|
624 | - } |
|
625 | - |
|
626 | - $list = $annotations['class'][$mode] ?? []; |
|
627 | - |
|
628 | - if (isset($annotations['method'][$mode])) { |
|
629 | - $list = array_merge($list, $annotations['method'][$mode]); |
|
630 | - } |
|
631 | - |
|
632 | - $codeUnits = CodeUnitCollection::fromArray([]); |
|
633 | - $mapper = new Mapper; |
|
634 | - |
|
635 | - foreach (array_unique($list) as $element) { |
|
636 | - if ($classShortcut && strncmp($element, '::', 2) === 0) { |
|
637 | - $element = $classShortcut . $element; |
|
638 | - } |
|
639 | - |
|
640 | - $element = preg_replace('/[\s()]+$/', '', $element); |
|
641 | - $element = explode(' ', $element); |
|
642 | - $element = $element[0]; |
|
643 | - |
|
644 | - if ($mode === 'covers' && interface_exists($element)) { |
|
645 | - throw new InvalidCoversTargetException( |
|
646 | - sprintf( |
|
647 | - 'Trying to @cover interface "%s".', |
|
648 | - $element |
|
649 | - ) |
|
650 | - ); |
|
651 | - } |
|
652 | - |
|
653 | - try { |
|
654 | - $codeUnits = $codeUnits->mergeWith($mapper->stringToCodeUnits($element)); |
|
655 | - } catch (InvalidCodeUnitException $e) { |
|
656 | - throw new InvalidCoversTargetException( |
|
657 | - sprintf( |
|
658 | - '"@%s %s" is invalid', |
|
659 | - $mode, |
|
660 | - $element |
|
661 | - ), |
|
662 | - (int) $e->getCode(), |
|
663 | - $e |
|
664 | - ); |
|
665 | - } |
|
666 | - } |
|
667 | - |
|
668 | - return $mapper->codeUnitsToSourceLines($codeUnits); |
|
669 | - } |
|
670 | - |
|
671 | - private static function emptyHookMethodsArray(): array |
|
672 | - { |
|
673 | - return [ |
|
674 | - 'beforeClass' => ['setUpBeforeClass'], |
|
675 | - 'before' => ['setUp'], |
|
676 | - 'preCondition' => ['assertPreConditions'], |
|
677 | - 'postCondition' => ['assertPostConditions'], |
|
678 | - 'after' => ['tearDown'], |
|
679 | - 'afterClass' => ['tearDownAfterClass'], |
|
680 | - ]; |
|
681 | - } |
|
682 | - |
|
683 | - /** @psalm-param class-string $className */ |
|
684 | - private static function getBooleanAnnotationSetting(string $className, ?string $methodName, string $settingName): ?bool |
|
685 | - { |
|
686 | - $annotations = self::parseTestMethodAnnotations( |
|
687 | - $className, |
|
688 | - $methodName |
|
689 | - ); |
|
690 | - |
|
691 | - if (isset($annotations['method'][$settingName])) { |
|
692 | - if ($annotations['method'][$settingName][0] === 'enabled') { |
|
693 | - return true; |
|
694 | - } |
|
695 | - |
|
696 | - if ($annotations['method'][$settingName][0] === 'disabled') { |
|
697 | - return false; |
|
698 | - } |
|
699 | - } |
|
700 | - |
|
701 | - if (isset($annotations['class'][$settingName])) { |
|
702 | - if ($annotations['class'][$settingName][0] === 'enabled') { |
|
703 | - return true; |
|
704 | - } |
|
705 | - |
|
706 | - if ($annotations['class'][$settingName][0] === 'disabled') { |
|
707 | - return false; |
|
708 | - } |
|
709 | - } |
|
710 | - |
|
711 | - return null; |
|
712 | - } |
|
713 | - |
|
714 | - /** |
|
715 | - * Trims any extensions from version string that follows after |
|
716 | - * the <major>.<minor>[.<patch>] format. |
|
717 | - */ |
|
718 | - private static function sanitizeVersionNumber(string $version) |
|
719 | - { |
|
720 | - return preg_replace( |
|
721 | - '/^(\d+\.\d+(?:.\d+)?).*$/', |
|
722 | - '$1', |
|
723 | - $version |
|
724 | - ); |
|
725 | - } |
|
726 | - |
|
727 | - private static function shouldCoversAnnotationBeUsed(array $annotations): bool |
|
728 | - { |
|
729 | - if (isset($annotations['method']['coversNothing'])) { |
|
730 | - return false; |
|
731 | - } |
|
732 | - |
|
733 | - if (isset($annotations['method']['covers'])) { |
|
734 | - return true; |
|
735 | - } |
|
736 | - |
|
737 | - if (isset($annotations['class']['coversNothing'])) { |
|
738 | - return false; |
|
739 | - } |
|
740 | - |
|
741 | - return true; |
|
742 | - } |
|
743 | - |
|
744 | - /** |
|
745 | - * Merge two arrays together. |
|
746 | - * |
|
747 | - * If an integer key exists in both arrays and preserveNumericKeys is false, the value |
|
748 | - * from the second array will be appended to the first array. If both values are arrays, they |
|
749 | - * are merged together, else the value of the second array overwrites the one of the first array. |
|
750 | - * |
|
751 | - * This implementation is copied from https://github.com/zendframework/zend-stdlib/blob/76b653c5e99b40eccf5966e3122c90615134ae46/src/ArrayUtils.php |
|
752 | - * |
|
753 | - * Zend Framework (http://framework.zend.com/) |
|
754 | - * |
|
755 | - * @see http://github.com/zendframework/zf2 for the canonical source repository |
|
756 | - * |
|
757 | - * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) |
|
758 | - * @license http://framework.zend.com/license/new-bsd New BSD License |
|
759 | - */ |
|
760 | - private static function mergeArraysRecursively(array $a, array $b): array |
|
761 | - { |
|
762 | - foreach ($b as $key => $value) { |
|
763 | - if (array_key_exists($key, $a)) { |
|
764 | - if (is_int($key)) { |
|
765 | - $a[] = $value; |
|
766 | - } elseif (is_array($value) && is_array($a[$key])) { |
|
767 | - $a[$key] = self::mergeArraysRecursively($a[$key], $value); |
|
768 | - } else { |
|
769 | - $a[$key] = $value; |
|
770 | - } |
|
771 | - } else { |
|
772 | - $a[$key] = $value; |
|
773 | - } |
|
774 | - } |
|
775 | - |
|
776 | - return $a; |
|
777 | - } |
|
778 | - |
|
779 | - private static function canonicalizeName(string $name): string |
|
780 | - { |
|
781 | - return strtolower(trim($name, '\\')); |
|
782 | - } |
|
61 | + /** |
|
62 | + * @var int |
|
63 | + */ |
|
64 | + public const UNKNOWN = -1; |
|
65 | + |
|
66 | + /** |
|
67 | + * @var int |
|
68 | + */ |
|
69 | + public const SMALL = 0; |
|
70 | + |
|
71 | + /** |
|
72 | + * @var int |
|
73 | + */ |
|
74 | + public const MEDIUM = 1; |
|
75 | + |
|
76 | + /** |
|
77 | + * @var int |
|
78 | + */ |
|
79 | + public const LARGE = 2; |
|
80 | + |
|
81 | + /** |
|
82 | + * @var array |
|
83 | + */ |
|
84 | + private static $hookMethods = []; |
|
85 | + |
|
86 | + /** |
|
87 | + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
|
88 | + */ |
|
89 | + public static function describe(\PHPUnit\Framework\Test $test): array |
|
90 | + { |
|
91 | + if ($test instanceof TestCase) { |
|
92 | + return [get_class($test), $test->getName()]; |
|
93 | + } |
|
94 | + |
|
95 | + if ($test instanceof SelfDescribing) { |
|
96 | + return ['', $test->toString()]; |
|
97 | + } |
|
98 | + |
|
99 | + return ['', get_class($test)]; |
|
100 | + } |
|
101 | + |
|
102 | + public static function describeAsString(\PHPUnit\Framework\Test $test): string |
|
103 | + { |
|
104 | + if ($test instanceof SelfDescribing) { |
|
105 | + return $test->toString(); |
|
106 | + } |
|
107 | + |
|
108 | + return get_class($test); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * @throws CodeCoverageException |
|
113 | + * |
|
114 | + * @return array|bool |
|
115 | + * |
|
116 | + * @psalm-param class-string $className |
|
117 | + */ |
|
118 | + public static function getLinesToBeCovered(string $className, string $methodName) |
|
119 | + { |
|
120 | + $annotations = self::parseTestMethodAnnotations( |
|
121 | + $className, |
|
122 | + $methodName |
|
123 | + ); |
|
124 | + |
|
125 | + if (!self::shouldCoversAnnotationBeUsed($annotations)) { |
|
126 | + return false; |
|
127 | + } |
|
128 | + |
|
129 | + return self::getLinesToBeCoveredOrUsed($className, $methodName, 'covers'); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Returns lines of code specified with the @uses annotation. |
|
134 | + * |
|
135 | + * @throws CodeCoverageException |
|
136 | + * |
|
137 | + * @psalm-param class-string $className |
|
138 | + */ |
|
139 | + public static function getLinesToBeUsed(string $className, string $methodName): array |
|
140 | + { |
|
141 | + return self::getLinesToBeCoveredOrUsed($className, $methodName, 'uses'); |
|
142 | + } |
|
143 | + |
|
144 | + public static function requiresCodeCoverageDataCollection(TestCase $test): bool |
|
145 | + { |
|
146 | + $annotations = self::parseTestMethodAnnotations( |
|
147 | + get_class($test), |
|
148 | + $test->getName(false) |
|
149 | + ); |
|
150 | + |
|
151 | + // If there is no @covers annotation but a @coversNothing annotation on |
|
152 | + // the test method then code coverage data does not need to be collected |
|
153 | + if (isset($annotations['method']['coversNothing'])) { |
|
154 | + // @see https://github.com/sebastianbergmann/phpunit/issues/4947#issuecomment-1084480950 |
|
155 | + // return false; |
|
156 | + } |
|
157 | + |
|
158 | + // If there is at least one @covers annotation then |
|
159 | + // code coverage data needs to be collected |
|
160 | + if (isset($annotations['method']['covers'])) { |
|
161 | + return true; |
|
162 | + } |
|
163 | + |
|
164 | + // If there is no @covers annotation but a @coversNothing annotation |
|
165 | + // then code coverage data does not need to be collected |
|
166 | + if (isset($annotations['class']['coversNothing'])) { |
|
167 | + // @see https://github.com/sebastianbergmann/phpunit/issues/4947#issuecomment-1084480950 |
|
168 | + // return false; |
|
169 | + } |
|
170 | + |
|
171 | + // If there is no @coversNothing annotation then |
|
172 | + // code coverage data may be collected |
|
173 | + return true; |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * @throws Exception |
|
178 | + * |
|
179 | + * @psalm-param class-string $className |
|
180 | + */ |
|
181 | + public static function getRequirements(string $className, string $methodName): array |
|
182 | + { |
|
183 | + return self::mergeArraysRecursively( |
|
184 | + Registry::getInstance()->forClassName($className)->requirements(), |
|
185 | + Registry::getInstance()->forMethod($className, $methodName)->requirements() |
|
186 | + ); |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Returns the missing requirements for a test. |
|
191 | + * |
|
192 | + * @throws Exception |
|
193 | + * @throws Warning |
|
194 | + * |
|
195 | + * @psalm-param class-string $className |
|
196 | + */ |
|
197 | + public static function getMissingRequirements(string $className, string $methodName): array |
|
198 | + { |
|
199 | + $required = self::getRequirements($className, $methodName); |
|
200 | + $missing = []; |
|
201 | + $hint = null; |
|
202 | + |
|
203 | + if (!empty($required['PHP'])) { |
|
204 | + $operator = new VersionComparisonOperator(empty($required['PHP']['operator']) ? '>=' : $required['PHP']['operator']); |
|
205 | + |
|
206 | + if (!version_compare(PHP_VERSION, $required['PHP']['version'], $operator->asString())) { |
|
207 | + $missing[] = sprintf('PHP %s %s is required.', $operator->asString(), $required['PHP']['version']); |
|
208 | + $hint = 'PHP'; |
|
209 | + } |
|
210 | + } elseif (!empty($required['PHP_constraint'])) { |
|
211 | + $version = new \PharIo\Version\Version(self::sanitizeVersionNumber(PHP_VERSION)); |
|
212 | + |
|
213 | + if (!$required['PHP_constraint']['constraint']->complies($version)) { |
|
214 | + $missing[] = sprintf( |
|
215 | + 'PHP version does not match the required constraint %s.', |
|
216 | + $required['PHP_constraint']['constraint']->asString() |
|
217 | + ); |
|
218 | + |
|
219 | + $hint = 'PHP_constraint'; |
|
220 | + } |
|
221 | + } |
|
222 | + |
|
223 | + if (!empty($required['PHPUnit'])) { |
|
224 | + $phpunitVersion = Version::id(); |
|
225 | + |
|
226 | + $operator = new VersionComparisonOperator(empty($required['PHPUnit']['operator']) ? '>=' : $required['PHPUnit']['operator']); |
|
227 | + |
|
228 | + if (!version_compare($phpunitVersion, $required['PHPUnit']['version'], $operator->asString())) { |
|
229 | + $missing[] = sprintf('PHPUnit %s %s is required.', $operator->asString(), $required['PHPUnit']['version']); |
|
230 | + $hint = $hint ?? 'PHPUnit'; |
|
231 | + } |
|
232 | + } elseif (!empty($required['PHPUnit_constraint'])) { |
|
233 | + $phpunitVersion = new \PharIo\Version\Version(self::sanitizeVersionNumber(Version::id())); |
|
234 | + |
|
235 | + if (!$required['PHPUnit_constraint']['constraint']->complies($phpunitVersion)) { |
|
236 | + $missing[] = sprintf( |
|
237 | + 'PHPUnit version does not match the required constraint %s.', |
|
238 | + $required['PHPUnit_constraint']['constraint']->asString() |
|
239 | + ); |
|
240 | + |
|
241 | + $hint = $hint ?? 'PHPUnit_constraint'; |
|
242 | + } |
|
243 | + } |
|
244 | + |
|
245 | + if (!empty($required['OSFAMILY']) && $required['OSFAMILY'] !== (new OperatingSystem)->getFamily()) { |
|
246 | + $missing[] = sprintf('Operating system %s is required.', $required['OSFAMILY']); |
|
247 | + $hint = $hint ?? 'OSFAMILY'; |
|
248 | + } |
|
249 | + |
|
250 | + if (!empty($required['OS'])) { |
|
251 | + $requiredOsPattern = sprintf('/%s/i', addcslashes($required['OS'], '/')); |
|
252 | + |
|
253 | + if (!preg_match($requiredOsPattern, PHP_OS)) { |
|
254 | + $missing[] = sprintf('Operating system matching %s is required.', $requiredOsPattern); |
|
255 | + $hint = $hint ?? 'OS'; |
|
256 | + } |
|
257 | + } |
|
258 | + |
|
259 | + if (!empty($required['functions'])) { |
|
260 | + foreach ($required['functions'] as $function) { |
|
261 | + $pieces = explode('::', $function); |
|
262 | + |
|
263 | + if (count($pieces) === 2 && class_exists($pieces[0]) && method_exists($pieces[0], $pieces[1])) { |
|
264 | + continue; |
|
265 | + } |
|
266 | + |
|
267 | + if (function_exists($function)) { |
|
268 | + continue; |
|
269 | + } |
|
270 | + |
|
271 | + $missing[] = sprintf('Function %s is required.', $function); |
|
272 | + $hint = $hint ?? 'function_' . $function; |
|
273 | + } |
|
274 | + } |
|
275 | + |
|
276 | + if (!empty($required['setting'])) { |
|
277 | + foreach ($required['setting'] as $setting => $value) { |
|
278 | + if (ini_get($setting) !== $value) { |
|
279 | + $missing[] = sprintf('Setting "%s" must be "%s".', $setting, $value); |
|
280 | + $hint = $hint ?? '__SETTING_' . $setting; |
|
281 | + } |
|
282 | + } |
|
283 | + } |
|
284 | + |
|
285 | + if (!empty($required['extensions'])) { |
|
286 | + foreach ($required['extensions'] as $extension) { |
|
287 | + if (isset($required['extension_versions'][$extension])) { |
|
288 | + continue; |
|
289 | + } |
|
290 | + |
|
291 | + if (!extension_loaded($extension)) { |
|
292 | + $missing[] = sprintf('Extension %s is required.', $extension); |
|
293 | + $hint = $hint ?? 'extension_' . $extension; |
|
294 | + } |
|
295 | + } |
|
296 | + } |
|
297 | + |
|
298 | + if (!empty($required['extension_versions'])) { |
|
299 | + foreach ($required['extension_versions'] as $extension => $req) { |
|
300 | + $actualVersion = phpversion($extension); |
|
301 | + |
|
302 | + $operator = new VersionComparisonOperator(empty($req['operator']) ? '>=' : $req['operator']); |
|
303 | + |
|
304 | + if ($actualVersion === false || !version_compare($actualVersion, $req['version'], $operator->asString())) { |
|
305 | + $missing[] = sprintf('Extension %s %s %s is required.', $extension, $operator->asString(), $req['version']); |
|
306 | + $hint = $hint ?? 'extension_' . $extension; |
|
307 | + } |
|
308 | + } |
|
309 | + } |
|
310 | + |
|
311 | + if ($hint && isset($required['__OFFSET'])) { |
|
312 | + array_unshift($missing, '__OFFSET_FILE=' . $required['__OFFSET']['__FILE']); |
|
313 | + array_unshift($missing, '__OFFSET_LINE=' . ($required['__OFFSET'][$hint] ?? 1)); |
|
314 | + } |
|
315 | + |
|
316 | + return $missing; |
|
317 | + } |
|
318 | + |
|
319 | + /** |
|
320 | + * Returns the provided data for a method. |
|
321 | + * |
|
322 | + * @throws Exception |
|
323 | + * |
|
324 | + * @psalm-param class-string $className |
|
325 | + */ |
|
326 | + public static function getProvidedData(string $className, string $methodName): ?array |
|
327 | + { |
|
328 | + return Registry::getInstance()->forMethod($className, $methodName)->getProvidedData(); |
|
329 | + } |
|
330 | + |
|
331 | + /** |
|
332 | + * @psalm-param class-string $className |
|
333 | + */ |
|
334 | + public static function parseTestMethodAnnotations(string $className, ?string $methodName = ''): array |
|
335 | + { |
|
336 | + $registry = Registry::getInstance(); |
|
337 | + |
|
338 | + if ($methodName !== null) { |
|
339 | + try { |
|
340 | + return [ |
|
341 | + 'method' => $registry->forMethod($className, $methodName)->symbolAnnotations(), |
|
342 | + 'class' => $registry->forClassName($className)->symbolAnnotations(), |
|
343 | + ]; |
|
344 | + } catch (Exception $methodNotFound) { |
|
345 | + // ignored |
|
346 | + } |
|
347 | + } |
|
348 | + |
|
349 | + return [ |
|
350 | + 'method' => null, |
|
351 | + 'class' => $registry->forClassName($className)->symbolAnnotations(), |
|
352 | + ]; |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * @psalm-param class-string $className |
|
357 | + */ |
|
358 | + public static function getInlineAnnotations(string $className, string $methodName): array |
|
359 | + { |
|
360 | + return Registry::getInstance()->forMethod($className, $methodName)->getInlineAnnotations(); |
|
361 | + } |
|
362 | + |
|
363 | + /** @psalm-param class-string $className */ |
|
364 | + public static function getBackupSettings(string $className, string $methodName): array |
|
365 | + { |
|
366 | + return [ |
|
367 | + 'backupGlobals' => self::getBooleanAnnotationSetting( |
|
368 | + $className, |
|
369 | + $methodName, |
|
370 | + 'backupGlobals' |
|
371 | + ), |
|
372 | + 'backupStaticAttributes' => self::getBooleanAnnotationSetting( |
|
373 | + $className, |
|
374 | + $methodName, |
|
375 | + 'backupStaticAttributes' |
|
376 | + ), |
|
377 | + ]; |
|
378 | + } |
|
379 | + |
|
380 | + /** |
|
381 | + * @psalm-param class-string $className |
|
382 | + * |
|
383 | + * @return ExecutionOrderDependency[] |
|
384 | + */ |
|
385 | + public static function getDependencies(string $className, string $methodName): array |
|
386 | + { |
|
387 | + $annotations = self::parseTestMethodAnnotations( |
|
388 | + $className, |
|
389 | + $methodName |
|
390 | + ); |
|
391 | + |
|
392 | + $dependsAnnotations = $annotations['class']['depends'] ?? []; |
|
393 | + |
|
394 | + if (isset($annotations['method']['depends'])) { |
|
395 | + $dependsAnnotations = array_merge( |
|
396 | + $dependsAnnotations, |
|
397 | + $annotations['method']['depends'] |
|
398 | + ); |
|
399 | + } |
|
400 | + |
|
401 | + // Normalize dependency name to className::methodName |
|
402 | + $dependencies = []; |
|
403 | + |
|
404 | + foreach ($dependsAnnotations as $value) { |
|
405 | + $dependencies[] = ExecutionOrderDependency::createFromDependsAnnotation($className, $value); |
|
406 | + } |
|
407 | + |
|
408 | + return array_unique($dependencies); |
|
409 | + } |
|
410 | + |
|
411 | + /** @psalm-param class-string $className */ |
|
412 | + public static function getGroups(string $className, ?string $methodName = ''): array |
|
413 | + { |
|
414 | + $annotations = self::parseTestMethodAnnotations( |
|
415 | + $className, |
|
416 | + $methodName |
|
417 | + ); |
|
418 | + |
|
419 | + $groups = []; |
|
420 | + |
|
421 | + if (isset($annotations['method']['author'])) { |
|
422 | + $groups[] = $annotations['method']['author']; |
|
423 | + } elseif (isset($annotations['class']['author'])) { |
|
424 | + $groups[] = $annotations['class']['author']; |
|
425 | + } |
|
426 | + |
|
427 | + if (isset($annotations['class']['group'])) { |
|
428 | + $groups[] = $annotations['class']['group']; |
|
429 | + } |
|
430 | + |
|
431 | + if (isset($annotations['method']['group'])) { |
|
432 | + $groups[] = $annotations['method']['group']; |
|
433 | + } |
|
434 | + |
|
435 | + if (isset($annotations['class']['ticket'])) { |
|
436 | + $groups[] = $annotations['class']['ticket']; |
|
437 | + } |
|
438 | + |
|
439 | + if (isset($annotations['method']['ticket'])) { |
|
440 | + $groups[] = $annotations['method']['ticket']; |
|
441 | + } |
|
442 | + |
|
443 | + foreach (['method', 'class'] as $element) { |
|
444 | + foreach (['small', 'medium', 'large'] as $size) { |
|
445 | + if (isset($annotations[$element][$size])) { |
|
446 | + $groups[] = [$size]; |
|
447 | + |
|
448 | + break 2; |
|
449 | + } |
|
450 | + } |
|
451 | + } |
|
452 | + |
|
453 | + foreach (['method', 'class'] as $element) { |
|
454 | + if (isset($annotations[$element]['covers'])) { |
|
455 | + foreach ($annotations[$element]['covers'] as $coversTarget) { |
|
456 | + $groups[] = ['__phpunit_covers_' . self::canonicalizeName($coversTarget)]; |
|
457 | + } |
|
458 | + } |
|
459 | + |
|
460 | + if (isset($annotations[$element]['uses'])) { |
|
461 | + foreach ($annotations[$element]['uses'] as $usesTarget) { |
|
462 | + $groups[] = ['__phpunit_uses_' . self::canonicalizeName($usesTarget)]; |
|
463 | + } |
|
464 | + } |
|
465 | + } |
|
466 | + |
|
467 | + return array_unique(array_merge([], ...$groups)); |
|
468 | + } |
|
469 | + |
|
470 | + /** @psalm-param class-string $className */ |
|
471 | + public static function getSize(string $className, ?string $methodName): int |
|
472 | + { |
|
473 | + $groups = array_flip(self::getGroups($className, $methodName)); |
|
474 | + |
|
475 | + if (isset($groups['large'])) { |
|
476 | + return self::LARGE; |
|
477 | + } |
|
478 | + |
|
479 | + if (isset($groups['medium'])) { |
|
480 | + return self::MEDIUM; |
|
481 | + } |
|
482 | + |
|
483 | + if (isset($groups['small'])) { |
|
484 | + return self::SMALL; |
|
485 | + } |
|
486 | + |
|
487 | + return self::UNKNOWN; |
|
488 | + } |
|
489 | + |
|
490 | + /** @psalm-param class-string $className */ |
|
491 | + public static function getProcessIsolationSettings(string $className, string $methodName): bool |
|
492 | + { |
|
493 | + $annotations = self::parseTestMethodAnnotations( |
|
494 | + $className, |
|
495 | + $methodName |
|
496 | + ); |
|
497 | + |
|
498 | + return isset($annotations['class']['runTestsInSeparateProcesses']) || isset($annotations['method']['runInSeparateProcess']); |
|
499 | + } |
|
500 | + |
|
501 | + /** @psalm-param class-string $className */ |
|
502 | + public static function getClassProcessIsolationSettings(string $className, string $methodName): bool |
|
503 | + { |
|
504 | + $annotations = self::parseTestMethodAnnotations( |
|
505 | + $className, |
|
506 | + $methodName |
|
507 | + ); |
|
508 | + |
|
509 | + return isset($annotations['class']['runClassInSeparateProcess']); |
|
510 | + } |
|
511 | + |
|
512 | + /** @psalm-param class-string $className */ |
|
513 | + public static function getPreserveGlobalStateSettings(string $className, string $methodName): ?bool |
|
514 | + { |
|
515 | + return self::getBooleanAnnotationSetting( |
|
516 | + $className, |
|
517 | + $methodName, |
|
518 | + 'preserveGlobalState' |
|
519 | + ); |
|
520 | + } |
|
521 | + |
|
522 | + /** @psalm-param class-string $className */ |
|
523 | + public static function getHookMethods(string $className): array |
|
524 | + { |
|
525 | + if (!class_exists($className, false)) { |
|
526 | + return self::emptyHookMethodsArray(); |
|
527 | + } |
|
528 | + |
|
529 | + if (!isset(self::$hookMethods[$className])) { |
|
530 | + self::$hookMethods[$className] = self::emptyHookMethodsArray(); |
|
531 | + |
|
532 | + try { |
|
533 | + foreach ((new Reflection)->methodsInTestClass(new ReflectionClass($className)) as $method) { |
|
534 | + $docBlock = Registry::getInstance()->forMethod($className, $method->getName()); |
|
535 | + |
|
536 | + if ($method->isStatic()) { |
|
537 | + if ($docBlock->isHookToBeExecutedBeforeClass()) { |
|
538 | + array_unshift( |
|
539 | + self::$hookMethods[$className]['beforeClass'], |
|
540 | + $method->getName() |
|
541 | + ); |
|
542 | + } |
|
543 | + |
|
544 | + if ($docBlock->isHookToBeExecutedAfterClass()) { |
|
545 | + self::$hookMethods[$className]['afterClass'][] = $method->getName(); |
|
546 | + } |
|
547 | + } |
|
548 | + |
|
549 | + if ($docBlock->isToBeExecutedBeforeTest()) { |
|
550 | + array_unshift( |
|
551 | + self::$hookMethods[$className]['before'], |
|
552 | + $method->getName() |
|
553 | + ); |
|
554 | + } |
|
555 | + |
|
556 | + if ($docBlock->isToBeExecutedAsPreCondition()) { |
|
557 | + array_unshift( |
|
558 | + self::$hookMethods[$className]['preCondition'], |
|
559 | + $method->getName() |
|
560 | + ); |
|
561 | + } |
|
562 | + |
|
563 | + if ($docBlock->isToBeExecutedAsPostCondition()) { |
|
564 | + self::$hookMethods[$className]['postCondition'][] = $method->getName(); |
|
565 | + } |
|
566 | + |
|
567 | + if ($docBlock->isToBeExecutedAfterTest()) { |
|
568 | + self::$hookMethods[$className]['after'][] = $method->getName(); |
|
569 | + } |
|
570 | + } |
|
571 | + } catch (ReflectionException $e) { |
|
572 | + } |
|
573 | + } |
|
574 | + |
|
575 | + return self::$hookMethods[$className]; |
|
576 | + } |
|
577 | + |
|
578 | + public static function isTestMethod(ReflectionMethod $method): bool |
|
579 | + { |
|
580 | + if (!$method->isPublic()) { |
|
581 | + return false; |
|
582 | + } |
|
583 | + |
|
584 | + if (strpos($method->getName(), 'test') === 0) { |
|
585 | + return true; |
|
586 | + } |
|
587 | + |
|
588 | + return array_key_exists( |
|
589 | + 'test', |
|
590 | + Registry::getInstance()->forMethod( |
|
591 | + $method->getDeclaringClass()->getName(), |
|
592 | + $method->getName() |
|
593 | + ) |
|
594 | + ->symbolAnnotations() |
|
595 | + ); |
|
596 | + } |
|
597 | + |
|
598 | + /** |
|
599 | + * @throws CodeCoverageException |
|
600 | + * |
|
601 | + * @psalm-param class-string $className |
|
602 | + */ |
|
603 | + private static function getLinesToBeCoveredOrUsed(string $className, string $methodName, string $mode): array |
|
604 | + { |
|
605 | + $annotations = self::parseTestMethodAnnotations( |
|
606 | + $className, |
|
607 | + $methodName |
|
608 | + ); |
|
609 | + |
|
610 | + $classShortcut = null; |
|
611 | + |
|
612 | + if (!empty($annotations['class'][$mode . 'DefaultClass'])) { |
|
613 | + if (count($annotations['class'][$mode . 'DefaultClass']) > 1) { |
|
614 | + throw new CodeCoverageException( |
|
615 | + sprintf( |
|
616 | + 'More than one @%sClass annotation in class or interface "%s".', |
|
617 | + $mode, |
|
618 | + $className |
|
619 | + ) |
|
620 | + ); |
|
621 | + } |
|
622 | + |
|
623 | + $classShortcut = $annotations['class'][$mode . 'DefaultClass'][0]; |
|
624 | + } |
|
625 | + |
|
626 | + $list = $annotations['class'][$mode] ?? []; |
|
627 | + |
|
628 | + if (isset($annotations['method'][$mode])) { |
|
629 | + $list = array_merge($list, $annotations['method'][$mode]); |
|
630 | + } |
|
631 | + |
|
632 | + $codeUnits = CodeUnitCollection::fromArray([]); |
|
633 | + $mapper = new Mapper; |
|
634 | + |
|
635 | + foreach (array_unique($list) as $element) { |
|
636 | + if ($classShortcut && strncmp($element, '::', 2) === 0) { |
|
637 | + $element = $classShortcut . $element; |
|
638 | + } |
|
639 | + |
|
640 | + $element = preg_replace('/[\s()]+$/', '', $element); |
|
641 | + $element = explode(' ', $element); |
|
642 | + $element = $element[0]; |
|
643 | + |
|
644 | + if ($mode === 'covers' && interface_exists($element)) { |
|
645 | + throw new InvalidCoversTargetException( |
|
646 | + sprintf( |
|
647 | + 'Trying to @cover interface "%s".', |
|
648 | + $element |
|
649 | + ) |
|
650 | + ); |
|
651 | + } |
|
652 | + |
|
653 | + try { |
|
654 | + $codeUnits = $codeUnits->mergeWith($mapper->stringToCodeUnits($element)); |
|
655 | + } catch (InvalidCodeUnitException $e) { |
|
656 | + throw new InvalidCoversTargetException( |
|
657 | + sprintf( |
|
658 | + '"@%s %s" is invalid', |
|
659 | + $mode, |
|
660 | + $element |
|
661 | + ), |
|
662 | + (int) $e->getCode(), |
|
663 | + $e |
|
664 | + ); |
|
665 | + } |
|
666 | + } |
|
667 | + |
|
668 | + return $mapper->codeUnitsToSourceLines($codeUnits); |
|
669 | + } |
|
670 | + |
|
671 | + private static function emptyHookMethodsArray(): array |
|
672 | + { |
|
673 | + return [ |
|
674 | + 'beforeClass' => ['setUpBeforeClass'], |
|
675 | + 'before' => ['setUp'], |
|
676 | + 'preCondition' => ['assertPreConditions'], |
|
677 | + 'postCondition' => ['assertPostConditions'], |
|
678 | + 'after' => ['tearDown'], |
|
679 | + 'afterClass' => ['tearDownAfterClass'], |
|
680 | + ]; |
|
681 | + } |
|
682 | + |
|
683 | + /** @psalm-param class-string $className */ |
|
684 | + private static function getBooleanAnnotationSetting(string $className, ?string $methodName, string $settingName): ?bool |
|
685 | + { |
|
686 | + $annotations = self::parseTestMethodAnnotations( |
|
687 | + $className, |
|
688 | + $methodName |
|
689 | + ); |
|
690 | + |
|
691 | + if (isset($annotations['method'][$settingName])) { |
|
692 | + if ($annotations['method'][$settingName][0] === 'enabled') { |
|
693 | + return true; |
|
694 | + } |
|
695 | + |
|
696 | + if ($annotations['method'][$settingName][0] === 'disabled') { |
|
697 | + return false; |
|
698 | + } |
|
699 | + } |
|
700 | + |
|
701 | + if (isset($annotations['class'][$settingName])) { |
|
702 | + if ($annotations['class'][$settingName][0] === 'enabled') { |
|
703 | + return true; |
|
704 | + } |
|
705 | + |
|
706 | + if ($annotations['class'][$settingName][0] === 'disabled') { |
|
707 | + return false; |
|
708 | + } |
|
709 | + } |
|
710 | + |
|
711 | + return null; |
|
712 | + } |
|
713 | + |
|
714 | + /** |
|
715 | + * Trims any extensions from version string that follows after |
|
716 | + * the <major>.<minor>[.<patch>] format. |
|
717 | + */ |
|
718 | + private static function sanitizeVersionNumber(string $version) |
|
719 | + { |
|
720 | + return preg_replace( |
|
721 | + '/^(\d+\.\d+(?:.\d+)?).*$/', |
|
722 | + '$1', |
|
723 | + $version |
|
724 | + ); |
|
725 | + } |
|
726 | + |
|
727 | + private static function shouldCoversAnnotationBeUsed(array $annotations): bool |
|
728 | + { |
|
729 | + if (isset($annotations['method']['coversNothing'])) { |
|
730 | + return false; |
|
731 | + } |
|
732 | + |
|
733 | + if (isset($annotations['method']['covers'])) { |
|
734 | + return true; |
|
735 | + } |
|
736 | + |
|
737 | + if (isset($annotations['class']['coversNothing'])) { |
|
738 | + return false; |
|
739 | + } |
|
740 | + |
|
741 | + return true; |
|
742 | + } |
|
743 | + |
|
744 | + /** |
|
745 | + * Merge two arrays together. |
|
746 | + * |
|
747 | + * If an integer key exists in both arrays and preserveNumericKeys is false, the value |
|
748 | + * from the second array will be appended to the first array. If both values are arrays, they |
|
749 | + * are merged together, else the value of the second array overwrites the one of the first array. |
|
750 | + * |
|
751 | + * This implementation is copied from https://github.com/zendframework/zend-stdlib/blob/76b653c5e99b40eccf5966e3122c90615134ae46/src/ArrayUtils.php |
|
752 | + * |
|
753 | + * Zend Framework (http://framework.zend.com/) |
|
754 | + * |
|
755 | + * @see http://github.com/zendframework/zf2 for the canonical source repository |
|
756 | + * |
|
757 | + * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) |
|
758 | + * @license http://framework.zend.com/license/new-bsd New BSD License |
|
759 | + */ |
|
760 | + private static function mergeArraysRecursively(array $a, array $b): array |
|
761 | + { |
|
762 | + foreach ($b as $key => $value) { |
|
763 | + if (array_key_exists($key, $a)) { |
|
764 | + if (is_int($key)) { |
|
765 | + $a[] = $value; |
|
766 | + } elseif (is_array($value) && is_array($a[$key])) { |
|
767 | + $a[$key] = self::mergeArraysRecursively($a[$key], $value); |
|
768 | + } else { |
|
769 | + $a[$key] = $value; |
|
770 | + } |
|
771 | + } else { |
|
772 | + $a[$key] = $value; |
|
773 | + } |
|
774 | + } |
|
775 | + |
|
776 | + return $a; |
|
777 | + } |
|
778 | + |
|
779 | + private static function canonicalizeName(string $name): string |
|
780 | + { |
|
781 | + return strtolower(trim($name, '\\')); |
|
782 | + } |
|
783 | 783 | } |