Completed
Push — master ( 84736a...0f447d )
by Antarès
03:14
created

Reader::getClassesToRead()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 7

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 34
ccs 25
cts 25
cp 1
rs 6.7272
cc 7
eloc 20
nc 7
nop 1
crap 7
1
<?php
2
3
namespace Accessible\Reader;
4
5
use \Accessible\Configuration;
6
7
class Reader
8
{
9
    /**
10
     * The name of the annotation classes that define a collection behavior.
11
     *
12
     * @var array<string>
13
     */
14
    protected static $collectionAnnotationClasses = array(
15
        "list" => "Accessible\\Annotation\\ListBehavior",
16
        "map" => "Accessible\\Annotation\\MapBehavior",
17
        "set" => "Accessible\\Annotation\\SetBehavior",
18
    );
19
20
    /**
21
     * Get a list of classes and traits to analyze.
22
     *
23
     * @param \ReflectionObject $reflectionObject The object to get the parents from.
24
     *
25
     * @return array The list of classes to read.
26
     */
27 28
    public static function getClassesToRead(\ReflectionObject $reflectionObject)
28
    {
29 28
        $cacheId = md5("classesToRead:" . $reflectionObject->getName());
30 28
        $objectClasses = self::getFromCache($cacheId);
31 28
        if ($objectClasses !== null) {
32 28
            return $objectClasses;
33
        }
34
35 6
        $objectClasses = array($reflectionObject);
36 6
        $objectTraits = $reflectionObject->getTraits();
37 6
        if (!empty($objectTraits)) {
38 2
            foreach ($objectTraits as $trait) {
39 2
                $objectClasses[] = $trait;
40 2
            }
41 2
        }
42
43 6
        $parentClass = $reflectionObject->getParentClass();
44 6
        while ($parentClass) {
45 4
            $objectClasses[] = $parentClass;
46
47 4
            $parentTraits = $parentClass->getTraits();
48 4
            if (!empty($parentTraits)) {
49 4
                foreach ($parentTraits as $trait) {
50 4
                    $objectClasses[] = $trait;
51 4
                }
52 4
            }
53
54 4
            $parentClass = $parentClass->getParentClass();
55 4
        }
56
57 6
        self::saveToCache($cacheId, $objectClasses);
58
59 6
        return $objectClasses;
60
    }
61
62 29
    public static function getFromCache($id)
63
    {
64 29
        $arrayCache = Configuration::getArrayCache();
65 29
        if ($arrayCache->contains($id)) {
66 29
            return $arrayCache->fetch($id);
67
        }
68
69 6
        $cacheDriver = Configuration::getCacheDriver();
70 6
        if ($cacheDriver !== null) {
71
            $cacheResult = $cacheDriver->fetch($id);
72
            if ($cacheResult !== false) {
73
                $arrayCache->save($id, $cacheResult);
74
                return $cacheResult;
75
            }
76
        }
77
78 6
        return null;
79
    }
80
81 28
    public static function saveToCache($id, $value)
82
    {
83 28
        $arrayCache = Configuration::getArrayCache();
84 28
        $cacheDriver = Configuration::getCacheDriver();
85
86 28
        $arrayCache->save($id, $value);
87 28
        if ($cacheDriver !== null) {
88
            $cacheDriver->save($id, $value);
89
        }
90 28
    }
91
}
92