Completed
Push — master ( 674961...f4f264 )
by Antarès
03:25
created

AutoConstructReader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 96.3%

Importance

Changes 12
Bugs 1 Features 2
Metric Value
wmc 10
c 12
b 1
f 2
lcom 2
cbo 1
dl 0
loc 82
ccs 26
cts 27
cp 0.963
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getConstructArguments() 0 15 3
C getPropertiesToInitialize() 0 24 7
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 array  $objectClasses The classes of the object to read.
34
     * @param Reader $objectClasses The annotation reader to use.
35
     *
36
     * @return array The list of arguments.
37
     */
38 6
    public static function getConstructArguments($objectClasses, $annotationReader)
39
    {
40 6
        $constructArguments = null;
41 6
        array_reverse($objectClasses);
42
43 6
        foreach ($objectClasses as $class) {
44 6
            $annotation = $annotationReader->getClassAnnotation($class, self::$constructAnnotationClass);
45 6
            if ($annotation !== null) {
46 2
                $constructArguments = $annotation->getArguments();
47 2
                break;
48
            }
49 6
        }
50
51 6
        return $constructArguments;
52
    }
53
54
    /**
55
     * Get the list of properties that have to be initialized automatically
56
     * during the object construction, plus their value.
57
     *
58
     * @param array  $properties The properties of the object to read.
59
     * @param Reader $objectClasses The annotation reader to use.
0 ignored issues
show
Bug introduced by
There is no parameter named $objectClasses. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
60
     *
61
     * @return array The list of properties and values,
62
     *               in the form ["property" => "value"].
63
     */
64 6
    public static function getPropertiesToInitialize($properties, $annotationReader)
65
    {
66 6
        $propertiesValues = array();
67
68 6
        foreach ($properties as $propertyName => $property) {
69 6
            $initializeAnnotation = $annotationReader->getPropertyAnnotation($property, self::$initializeAnnotationClass);
70 6
            $initializeObjectAnnotation = $annotationReader->getPropertyAnnotation($property, self::$initializeObjectAnnotationClass);
71
72 6
            if ($initializeAnnotation !== null && $initializeObjectAnnotation !== null) {
73
                throw new \LogicException("Two initial values are given for property $propertyName.");
74
            }
75
76 6
            if (empty($propertiesValues[$propertyName])) {
77 6
                if ($initializeAnnotation !== null) {
78 6
                    $propertiesValues[$propertyName] = $initializeAnnotation->getValue();
79 6
                } else if ($initializeObjectAnnotation !== null) {
80 5
                    $className = $initializeObjectAnnotation->getClassName();
81 5
                    $propertiesValues[$propertyName] = new $className();
82 5
                }
83 6
            }
84 6
        }
85
86 6
        return $propertiesValues;
87
    }
88
}
89