Passed
Pull Request — master (#47)
by Pascal
15:06 queued 27s
created

Registry   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
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 42
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
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
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
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