Passed
Branch master (4c6b1c)
by Antarès
04:47 queued 01:20
created

AutoConstructReader   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.87%

Importance

Changes 11
Bugs 1 Features 2
Metric Value
wmc 13
c 11
b 1
f 2
lcom 1
cbo 2
dl 0
loc 107
ccs 46
cts 47
cp 0.9787
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getConstructArguments() 0 26 4
D getPropertiesToInitialize() 0 40 9
1
<?php
2
3
namespace Accessible\Reader;
4
5
use \Accessible\Configuration;
6
7
class AutoConstructReader extends Reader
8
{
9
    /**
10
     * The name of the annotation class that define the construct arguments.
11
     *
12
     * @var string
13
     */
14
    private static $constructAnnotationClass = "Accessible\\Annotation\\Construct";
15
16
    /**
17
     * The name of the annotation class that define a property's default value.
18
     *
19
     * @var string
20
     */
21
    private static $initializeAnnotationClass = "Accessible\\Annotation\\Initialize";
22
23
    /**
24
     * The name of the annotation class that define the initial value of an object property.
25
     *
26
     * @var string
27
     */
28
    private static $initializeObjectAnnotationClass = "Accessible\\Annotation\\InitializeObject";
29
30
    /**
31
     * Get the list of needed arguments for given object's constructor.
32
     *
33
     * @param object $object The object to analyze.
34
     *
35
     * @return array The list of arguments.
36
     */
37 29
    public static function getConstructArguments($object)
38
    {
39 29
        $reflectionObject = new \ReflectionObject($object);
40 29
        $cacheId = md5("constructArguments:" . $reflectionObject->getName());
41 29
        $constructArguments = self::getFromCache($cacheId);
42 29
        if ($constructArguments !== null) {
43 1
            return $constructArguments;
44
        }
45
46 28
        $constructArguments = null;
47 28
        $annotationReader = Configuration::getAnnotationReader();
48 28
        $objectClasses = self::getClassesToRead($reflectionObject);
49 28
        array_reverse($objectClasses);
50
51 28
        foreach ($objectClasses as $class) {
52 28
            $annotation = $annotationReader->getClassAnnotation($class, self::$constructAnnotationClass);
53 28
            if ($annotation !== null) {
54 2
                $constructArguments = $annotation->getArguments();
55 2
                break;
56
            }
57 28
        }
58
59 28
        self::saveToCache($cacheId, $constructArguments);
60
61 28
        return $constructArguments;
62
    }
63
64
    /**
65
     * Get the list of properties that have to be initialized automatically
66
     * during the object construction, plus their value.
67
     *
68
     * @param object $object The object to analyze.
69
     *
70
     * @return array The list of properties and values,
71
     *               in the form ["property" => "value"].
72
     */
73 29
    public static function getPropertiesToInitialize($object)
74
    {
75 29
        $reflectionObject = new \ReflectionObject($object);
76 29
        $cacheId = md5("propertiesToInitialize:" . $reflectionObject->getName());
77 29
        $propertiesValues = self::getFromCache($cacheId);
78 29
        if ($propertiesValues !== null) {
79 24
            return $propertiesValues;
80
        }
81
82 6
        $annotationReader = Configuration::getAnnotationReader();
83 6
        $objectClasses = self::getClassesToRead($reflectionObject);
84 6
        array_reverse($objectClasses);
85
86 6
        $propertiesValues = array();
87
88 6
        foreach ($objectClasses as $class) {
89 6
            foreach ($class->getProperties() as $property) {
90 6
                $propertyName = $property->getName();
91 6
                $initializeAnnotation = $annotationReader->getPropertyAnnotation($property, self::$initializeAnnotationClass);
92 6
                $initializeObjectAnnotation = $annotationReader->getPropertyAnnotation($property, self::$initializeObjectAnnotationClass);
93
94 6
                if ($initializeAnnotation !== null && $initializeObjectAnnotation !== null) {
95
                    throw new \LogicException("Two initial values are given for property $propertyName.");
96
                }
97
98 6
                if (empty($propertiesValues[$propertyName])) {
99 6
                    if ($initializeAnnotation !== null) {
100 6
                        $propertiesValues[$propertyName] = $initializeAnnotation->getValue();
101 6
                    } else if ($initializeObjectAnnotation !== null) {
102 5
                        $className = $initializeObjectAnnotation->getClassName();
103 5
                        $propertiesValues[$propertyName] = new $className();
104 5
                    }
105 6
                }
106 6
            }
107 6
        }
108
109 6
        self::saveToCache($cacheId, $propertiesValues);
110
111 6
        return $propertiesValues;
112
    }
113
}
114