Reader::getClassesToRead()   C
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 7.0031

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 34
ccs 24
cts 25
cp 0.96
rs 6.7272
cc 7
eloc 20
nc 7
nop 1
crap 7.0031
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 7
    public static function getClassesToRead(\ReflectionObject $reflectionObject)
28
    {
29 7
        $cacheId = md5("classesToRead:" . $reflectionObject->getName());
30 7
        $objectClasses = self::getFromCache($cacheId);
31 7
        if ($objectClasses !== null) {
32
            return $objectClasses;
33
        }
34
35 7
        $objectClasses = array($reflectionObject);
36 7
        $objectTraits = $reflectionObject->getTraits();
37 7
        if (!empty($objectTraits)) {
38 3
            foreach ($objectTraits as $trait) {
39 3
                $objectClasses[] = $trait;
40 3
            }
41 3
        }
42
43 7
        $parentClass = $reflectionObject->getParentClass();
44 7
        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 7
        self::saveToCache($cacheId, $objectClasses);
58
59 7
        return $objectClasses;
60
    }
61
62
    /**
63
     * Get the properties from a list of classes.
64
     *
65
     * @param array $classes
66
     *
67
     * @return array
68
     */
69 7
    public static function getProperties($classes)
70
    {
71 7
        array_reverse($classes);
72 7
        $properties = array();
73 7
        foreach ($classes as $class) {
74 7
            foreach ($class->getProperties() as $property) {
75 7
                $properties[$property->getName()] = $property;
76 7
            }
77 7
        }
78
79 7
        return $properties;
80
    }
81
82
    /**
83
     * Get the information on a class from its instance.
84
     *
85
     * @param  object $object
86
     *
87
     * @return array
88
     */
89 38
    public static function getClassInformation($object)
90
    {
91 38
        $reflectionObject = new \ReflectionObject($object);
92 38
        $cacheId = md5("classInformation:" . $reflectionObject->getName());
93 38
        $classInfo = self::getFromCache($cacheId);
94 38
        if ($classInfo !== null) {
95 32
            return $classInfo;
96
        }
97
98 7
        $objectClasses = self::getClassesToRead($reflectionObject);
99 7
        $objectProperties = self::getProperties($objectClasses);
100 7
        $annotationReader = Configuration::getAnnotationReader();
101
102
        $classInfo = array(
103 7
            'accessProperties' => AccessReader::getAccessProperties($objectProperties, $annotationReader),
0 ignored issues
show
Bug introduced by
It seems like $annotationReader defined by \Accessible\Configuration::getAnnotationReader() on line 100 can also be of type object<Doctrine\Common\Annotations\CachedReader>; however, Accessible\Reader\Access...::getAccessProperties() does only seem to accept object<Accessible\Reader\Reader>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
104 7
            'collectionsItemNames' => CollectionsReader::getCollectionsItemNames($objectProperties, $annotationReader),
105 7
            'associationsList' => AssociationReader::getAssociations($objectProperties, $annotationReader),
106 7
            'constraintsValidationEnabled' => ConstraintsReader::isConstraintsValidationEnabled($objectClasses, $annotationReader),
107 7
            'initialPropertiesValues' => AutoConstructReader::getPropertiesToInitialize($objectProperties, $annotationReader),
108 7
            'initializationNeededArguments' => AutoConstructReader::getConstructArguments($objectClasses, $annotationReader)
109 7
        );
110
111 7
        self::saveToCache($cacheId, $classInfo);
112
113 7
        return $classInfo;
114
    }
115
116
    /**
117
     * Get a value from the cache.
118
     *
119
     * @param  string $id
120
     *
121
     * @return mixed
122
     */
123 38
    public static function getFromCache($id)
124
    {
125 38
        $arrayCache = Configuration::getArrayCache();
126 38
        if ($arrayCache->contains($id)) {
127 32
            return $arrayCache->fetch($id);
128
        }
129
130 7
        $cacheDriver = Configuration::getCacheDriver();
131 7
        if ($cacheDriver !== null) {
132
            $cacheResult = $cacheDriver->fetch($id);
133
            if ($cacheResult !== false) {
134
                $arrayCache->save($id, $cacheResult);
135
                return $cacheResult;
136
            }
137
        }
138
139 7
        return null;
140
    }
141
142
    /**
143
     * Save a value to the cache.
144
     *
145
     * @param  string $id
146
     * @param  mixed $value
147
     */
148 7
    public static function saveToCache($id, $value)
149
    {
150 7
        $arrayCache = Configuration::getArrayCache();
151 7
        $cacheDriver = Configuration::getCacheDriver();
152
153 7
        $arrayCache->save($id, $value);
154 7
        if ($cacheDriver !== null) {
155
            $cacheDriver->save($id, $value);
156
        }
157 7
    }
158
}
159