Completed
Pull Request — master (#2069)
by Andreas
20:35
created

CachingClassNameResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 35
ccs 7
cts 9
cp 0.7778
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getRealClass() 0 8 2
A getClass() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Proxy\Resolver;
6
7
use function get_class;
8
9
/**
10
 * @internal
11
 */
12
final class CachingClassNameResolver implements ClassNameResolver
13
{
14
    /** @var ClassNameResolver */
15
    private $resolver;
16
17
    /**
18
     * @var array<string, string>
19
     */
20
    private $resolvedNames = [];
21
22 1773
    public function __construct(ClassNameResolver $resolver)
23
    {
24 1773
        $this->resolver = $resolver;
25 1773
    }
26
27
    /**
28
     * Gets the real class name of a class name that could be a proxy.
29
     */
30 1548
    public function getRealClass(string $class) : string
31
    {
32 1548
        if (! isset($this->resolvedNames[$class])) {
33 1548
            $this->resolvedNames[$class] = $this->resolver->getRealClass($class);
34
        }
35
36 1548
        return $this->resolvedNames[$class];
37
    }
38
39
    /**
40
     * Gets the real class name of an object (even if its a proxy).
41
     */
42
    public function getClass(object $object) : string
43
    {
44
        return $this->getRealClass(get_class($object));
45
    }
46
}
47