Completed
Push — master ( 4a73dc...12bb39 )
by Antarès
03:01
created

AutoConstructTrait::initializeProperties()   C

Complexity

Conditions 7
Paths 15

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 7

Importance

Changes 7
Bugs 1 Features 0
Metric Value
c 7
b 1
f 0
dl 0
loc 42
ccs 32
cts 32
cp 1
rs 6.7272
cc 7
eloc 26
nc 15
nop 1
crap 7
1
<?php
2
3
namespace Accessible;
4
5
use \Accessible\MethodManager\MethodCallManager;
6
7
trait AutoConstructTrait
8
{
9
    /**
10
     * Directly calls the initialization method.
11
     */
12 35
    public function __construct()
13
    {
14 35
        $this->initializeProperties(func_get_args());
15 34
    }
16
17
    /**
18
     * Initializes the object according to its class specification and given arguments.
19
     *
20
     * @param array $properties The values to give to the properties.
21
     */
22 36
    protected function initializeProperties($properties = null)
23
    {
24 36
        $this->getPropertiesInfo();
25
26
        // Initialize the properties that were defined using the Initialize / InitializeObject annotations
27 36
        $initializeValueValidationEnabled = Configuration::isInitializeValuesValidationEnabled();
28 36
        foreach ($this->_initialPropertiesValues as $propertyName => $initialization) {
29 35
            $value = null;
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
30 35
            switch ($initialization['type']) {
31 35
                case 'initialize':
32 35
                    $value = $initialization['value'];
33 35
                    break;
34 35
                default:
35 35
                    $className = $initialization['value'];
36 35
                    $value = new $className();
37 35
                    break;
38 35
            }
39
40 35
            if ($initializeValueValidationEnabled) {
41 35
                $this->assertPropertyValue($propertyName, $value);
42 35
            }
43
44 35
            $this->$propertyName = $value;
45 35
            $this->updateInitializedPropertyValue($propertyName, $value);
46 36
        }
47
48
        // Initialize the propeties using given arguments
49 36
        if ($this->_initializationNeededArguments !== null && $properties !== null) {
50 3
            $numberOfNeededArguments = count($this->_initializationNeededArguments);
51
52 3
            MethodCallManager::assertArgsNumber($numberOfNeededArguments, $properties);
53
54 3
            for ($i = 0; $i < $numberOfNeededArguments; $i++) {
55 3
                $propertyName = $this->_initializationNeededArguments[$i];
56 3
                $argument = $properties[$i];
57
58 3
                $this->assertPropertyValue($propertyName, $argument);
59 2
                $this->$propertyName = $argument;
60 2
                $this->updateInitializedPropertyValue($propertyName, $argument);
61 2
            }
62 2
        }
63 35
    }
64
65
    /**
66
     * Update an initialized value.
67
     *
68
     * @param  string $propertyName
69
     * @param  mixed $value
70
     */
71 35
    private function updateInitializedPropertyValue($propertyName, $value)
72
    {
73 35
        if (empty($this->_collectionsItemNames['byProperty'][$propertyName])) {
74 35
            $this->updatePropertyAssociation($propertyName, array("oldValue" => null, "newValue" => $value));
75 35
        } else {
76 35
            foreach ($value as $newValue) {
77
                $this->updatePropertyAssociation($propertyName, array("oldValue" => null, "newValue" => $newValue));
78 35
            }
79
        }
80 35
    }
81
}
82