Completed
Push — master ( f4f264...399648 )
by Antarès
03:11
created

updateInitializedPropertyValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 2
crap 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 28
    public function __construct()
13
    {
14 28
        $this->initializeProperties(func_get_args());
15 27
    }
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 29
    protected function initializeProperties($properties = null)
23
    {
24 29
        $this->getPropertiesInfo();
25
26
        // Initialize the properties that were defined using the Initialize / InitializeObject annotations
27 29
        $initializeValueValidationEnabled = Configuration::isInitializeValuesValidationEnabled();
28
29 29
        foreach ($this->_initialPropertiesValues as $propertyName => $value) {
30 29
            if ($initializeValueValidationEnabled) {
31 29
                $this->assertPropertyValue($propertyName, $value);
32 29
            }
33
34 29
            $this->$propertyName = $value;
35 29
            $this->updateInitializedPropertyValue($propertyName, $value);
36 29
        }
37
38
        // Initialize the propeties using given arguments
39 29
        if ($this->_initializationNeededArguments !== null && $properties !== null) {
40 3
            $numberOfNeededArguments = count($this->_initializationNeededArguments);
41
42 3
            MethodCallManager::assertArgsNumber($numberOfNeededArguments, $properties);
43
44 3
            for ($i = 0; $i < $numberOfNeededArguments; $i++) {
45 3
                $propertyName = $this->_initializationNeededArguments[$i];
46 3
                $argument = $properties[$i];
47
48 3
                $this->assertPropertyValue($propertyName, $argument);
49 2
                $this->$propertyName = $argument;
50 2
                $this->updateInitializedPropertyValue($propertyName, $argument);
51 2
            }
52 2
        }
53 28
    }
54
55
    /**
56
     * Update an initialized value.
57
     *
58
     * @param  string $propertyName
59
     * @param  mixed $value
60
     */
61 29
    private function updateInitializedPropertyValue($propertyName, $value)
62
    {
63 29
        if (empty($this->_collectionsItemNames['byProperty'][$propertyName])) {
64 29
            $this->updatePropertyAssociation($propertyName, array("oldValue" => null, "newValue" => $value));
65 29
        } else {
66 29
            foreach ($value as $newValue) {
67 8
                $this->updatePropertyAssociation($propertyName, array("oldValue" => null, "newValue" => $newValue));
68 29
            }
69
        }
70 29
    }
71
}
72