CachedDoubler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 45
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createDoubleClass() 0 9 2
A generateClassId() 0 16 4
A resetCache() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Prophecy.
5
 * (c) Konstantin Kudryashov <[email protected]>
6
 *     Marcello Duarte <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Prophecy\Doubler;
13
14
use ReflectionClass;
15
16
/**
17
 * Cached class doubler.
18
 * Prevents mirroring/creation of the same structure twice.
19
 *
20
 * @author Konstantin Kudryashov <[email protected]>
21
 */
22
class CachedDoubler extends Doubler
23
{
24
    private static $classes = array();
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    protected function createDoubleClass(ReflectionClass $class = null, array $interfaces)
30
    {
31
        $classId = $this->generateClassId($class, $interfaces);
32
        if (isset(self::$classes[$classId])) {
33
            return self::$classes[$classId];
34
        }
35
36
        return self::$classes[$classId] = parent::createDoubleClass($class, $interfaces);
37
    }
38
39
    /**
40
     * @param ReflectionClass   $class
41
     * @param ReflectionClass[] $interfaces
42
     *
43
     * @return string
44
     */
45
    private function generateClassId(ReflectionClass $class = null, array $interfaces)
46
    {
47
        $parts = array();
48
        if (null !== $class) {
49
            $parts[] = $class->getName();
50
        }
51
        foreach ($interfaces as $interface) {
52
            $parts[] = $interface->getName();
53
        }
54
        foreach ($this->getClassPatches() as $patch) {
55
            $parts[] = get_class($patch);
56
        }
57
        sort($parts);
58
59
        return md5(implode('', $parts));
60
    }
61
62
    public function resetCache()
63
    {
64
        self::$classes = array();
65
    }
66
}
67