| 148 |
lars |
1 |
<?php declare(strict_types=1);
|
|
|
2 |
/*
|
|
|
3 |
* This file is part of PHPUnit.
|
|
|
4 |
*
|
|
|
5 |
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
|
|
6 |
*
|
|
|
7 |
* For the full copyright and license information, please view the LICENSE
|
|
|
8 |
* file that was distributed with this source code.
|
|
|
9 |
*/
|
|
|
10 |
namespace PHPUnit\Util\Annotation;
|
|
|
11 |
|
|
|
12 |
use function array_key_exists;
|
|
|
13 |
use PHPUnit\Util\Exception;
|
|
|
14 |
use ReflectionClass;
|
|
|
15 |
use ReflectionException;
|
|
|
16 |
use ReflectionMethod;
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* Reflection information, and therefore DocBlock information, is static within
|
|
|
20 |
* a single PHP process. It is therefore okay to use a Singleton registry here.
|
|
|
21 |
*
|
|
|
22 |
* @internal This class is not covered by the backward compatibility promise for PHPUnit
|
|
|
23 |
*/
|
|
|
24 |
final class Registry
|
|
|
25 |
{
|
|
|
26 |
/** @var null|self */
|
|
|
27 |
private static $instance;
|
|
|
28 |
|
|
|
29 |
/** @var array<string, DocBlock> indexed by class name */
|
|
|
30 |
private $classDocBlocks = [];
|
|
|
31 |
|
|
|
32 |
/** @var array<string, array<string, DocBlock>> indexed by class name and method name */
|
|
|
33 |
private $methodDocBlocks = [];
|
|
|
34 |
|
|
|
35 |
public static function getInstance(): self
|
|
|
36 |
{
|
|
|
37 |
return self::$instance ?? self::$instance = new self;
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
private function __construct()
|
|
|
41 |
{
|
|
|
42 |
}
|
|
|
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 |
}
|
|
|
54 |
|
|
|
55 |
try {
|
|
|
56 |
$reflection = new ReflectionClass($class);
|
|
|
57 |
// @codeCoverageIgnoreStart
|
|
|
58 |
} catch (ReflectionException $e) {
|
|
|
59 |
throw new Exception(
|
|
|
60 |
$e->getMessage(),
|
|
|
61 |
$e->getCode(),
|
|
|
62 |
$e
|
|
|
63 |
);
|
|
|
64 |
}
|
|
|
65 |
// @codeCoverageIgnoreEnd
|
|
|
66 |
|
|
|
67 |
return $this->classDocBlocks[$class] = DocBlock::ofClass($reflection);
|
|
|
68 |
}
|
|
|
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 |
}
|
|
|
80 |
|
|
|
81 |
try {
|
|
|
82 |
$reflection = new ReflectionMethod($classInHierarchy, $method);
|
|
|
83 |
// @codeCoverageIgnoreStart
|
|
|
84 |
} catch (ReflectionException $e) {
|
|
|
85 |
throw new Exception(
|
|
|
86 |
$e->getMessage(),
|
|
|
87 |
$e->getCode(),
|
|
|
88 |
$e
|
|
|
89 |
);
|
|
|
90 |
}
|
|
|
91 |
// @codeCoverageIgnoreEnd
|
|
|
92 |
|
|
|
93 |
return $this->methodDocBlocks[$classInHierarchy][$method] = DocBlock::ofMethod($reflection, $classInHierarchy);
|
|
|
94 |
}
|
|
|
95 |
}
|