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; |
|
|
|
|
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
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.