Passed
Pull Request — master (#47)
by Pascal
14:53
created

Registry   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 10
c 1
b 0
f 0
dl 0
loc 43
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A forMethod() 0 9 2
A forClassName() 0 9 2
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
namespace FriendsOfPhpSpec\PhpSpec\CodeCoverage\Annotation;
14
15
use function array_key_exists;
16
use ReflectionClass;
17
use ReflectionException;
18
use ReflectionMethod;
19
20
/**
21
 * Reflection information, and therefore DocBlock information, is static within
22
 * a single PHP process. It is therefore okay to use a Singleton registry here.
23
 *
24
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
25
 */
26
final class Registry
27
{
28
    /**
29
     * @var array<string, DocBlock> indexed by class name
30
     */
31
    private $classDocBlocks = [];
32
33
    /**
34
     * @var array<string, array<string, DocBlock>> indexed by class name and method name
35
     */
36
    private $methodDocBlocks = [];
37
38
    /**
39
     * @param string $class
40
     * @return DocBlock
41
     * @throws ReflectionException
42
     */
43
    public function forClassName(string $class): DocBlock
44
    {
45
        if (array_key_exists($class, $this->classDocBlocks)) {
46
            return $this->classDocBlocks[$class];
47
        }
48
49
        $reflection = new ReflectionClass($class);
50
51
        return $this->classDocBlocks[$class] = DocBlock::ofClass($reflection);
52
    }
53
54
    /**
55
     * @param string $classInHierarchy
56
     * @param string $method
57
     * @return DocBlock
58
     * @throws ReflectionException
59
     */
60
    public function forMethod(string $classInHierarchy, string $method): DocBlock
61
    {
62
        if (isset($this->methodDocBlocks[$classInHierarchy][$method])) {
63
            return $this->methodDocBlocks[$classInHierarchy][$method];
64
        }
65
66
        $reflection = new ReflectionMethod($classInHierarchy, $method);
67
68
        return $this->methodDocBlocks[$classInHierarchy][$method] = DocBlock::ofMethod($reflection, $classInHierarchy);
69
    }
70
}
71