1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of PHPUnit. |
7
|
|
|
* |
8
|
|
|
* (c) Sebastian Bergmann <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace FriendsOfPhpSpec\PhpSpec\CodeCoverage\Annotation; |
15
|
|
|
|
16
|
|
|
use ReflectionClass; |
17
|
|
|
use ReflectionException; |
18
|
|
|
use ReflectionMethod; |
19
|
|
|
|
20
|
|
|
use function array_key_exists; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Reflection information, and therefore DocBlock information, is static within |
24
|
|
|
* a single PHP process. It is therefore okay to use a Singleton registry here. |
25
|
|
|
* |
26
|
|
|
* @internal This class is not covered by the backward compatibility promise for PHPUnit |
27
|
|
|
*/ |
28
|
|
|
final class Registry |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var array<string, DocBlock> indexed by class name |
32
|
|
|
*/ |
33
|
|
|
private $classDocBlocks = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array<string, array<string, DocBlock>> indexed by class name and method name |
37
|
|
|
*/ |
38
|
|
|
private $methodDocBlocks = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param class-string $class |
|
|
|
|
42
|
|
|
* |
43
|
|
|
* @throws ReflectionException |
44
|
|
|
*/ |
45
|
|
|
public function forClassName(string $class): DocBlock |
46
|
|
|
{ |
47
|
|
|
if (array_key_exists($class, $this->classDocBlocks)) { |
48
|
|
|
return $this->classDocBlocks[$class]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$reflection = new ReflectionClass($class); |
52
|
|
|
|
53
|
|
|
return $this->classDocBlocks[$class] = DocBlock::ofClass($reflection); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param class-string $classInHierarchy |
|
|
|
|
58
|
|
|
* |
59
|
|
|
* @throws ReflectionException |
60
|
|
|
*/ |
61
|
|
|
public function forMethod(string $classInHierarchy, string $method): DocBlock |
62
|
|
|
{ |
63
|
|
|
if (isset($this->methodDocBlocks[$classInHierarchy][$method])) { |
64
|
|
|
return $this->methodDocBlocks[$classInHierarchy][$method]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$reflection = new ReflectionMethod($classInHierarchy, $method); |
68
|
|
|
|
69
|
|
|
return $this->methodDocBlocks[$classInHierarchy][$method] = DocBlock::ofMethod($reflection, $classInHierarchy); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|