AutoConstructTrait   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.67%

Importance

Changes 15
Bugs 1 Features 3
Metric Value
wmc 11
c 15
b 1
f 3
lcom 1
cbo 2
dl 0
loc 75
ccs 42
cts 43
cp 0.9767
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C initializeProperties() 0 42 7
A updateInitializedPropertyValue() 0 10 3
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 37
    public function __construct()
13
    {
14 37
        $this->initializeProperties(func_get_args());
15 36
    }
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 38
    protected function initializeProperties($properties = null)
23
    {
24 38
        $this->getPropertiesInfo();
25
26
        // Initialize the properties that were defined using the Initialize / InitializeObject annotations
27 38
        $initializeValueValidationEnabled = Configuration::isInitializeValuesValidationEnabled();
28 38
        foreach ($this->_initialPropertiesValues as $propertyName => $initialization) {
29 37
            $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 37
            switch ($initialization['type']) {
31 37
                case 'initialize':
32 37
                    $value = $initialization['value'];
33 37
                    break;
34 37
                default:
35 37
                    $className = $initialization['value'];
36 37
                    $value = new $className();
37 37
                    break;
38 37
            }
39
40 37
            if ($initializeValueValidationEnabled) {
41 37
                $this->assertPropertyValue($propertyName, $value);
42 37
            }
43
44 37
            $this->$propertyName = $value;
45 37
            $this->updateInitializedPropertyValue($propertyName, $value);
46 38
        }
47
48
        // Initialize the propeties using given arguments
49 38
        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 37
    }
64
65
    /**
66
     * Update an initialized value.
67
     *
68
     * @param  string $propertyName
69
     * @param  mixed $value
70
     */
71 37
    private function updateInitializedPropertyValue($propertyName, $value)
72
    {
73 37
        if (empty($this->_collectionsItemNames['byProperty'][$propertyName])) {
74 37
            $this->updatePropertyAssociation($propertyName, array("oldValue" => null, "newValue" => $value));
75 37
        } else {
76 37
            foreach ($value as $newValue) {
77
                $this->updatePropertyAssociation($propertyName, array("oldValue" => null, "newValue" => $newValue));
78 37
            }
79
        }
80 37
    }
81
}
82