1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SmsaSDK\Concerns; |
4
|
|
|
|
5
|
|
|
use SmsaSDK\Config; |
6
|
|
|
|
7
|
|
|
trait MapMethods |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* mergeCommonData |
11
|
|
|
* Insert description here. |
12
|
|
|
* |
13
|
|
|
* @param $methodHandler |
14
|
|
|
* @param $arguments |
15
|
|
|
* |
16
|
|
|
* @return |
17
|
|
|
*/ |
18
|
3 |
|
private function mergeCommonData($methodHandler, $arguments = []) |
19
|
|
|
{ |
20
|
3 |
|
foreach (Config::getCommonData() as $key => $value) { |
21
|
3 |
|
$setterMethod = $this->getSetterMethodFromKey($key); |
22
|
3 |
|
if (method_exists($methodHandler, $setterMethod) && !array_key_exists($key, $arguments)) { |
23
|
3 |
|
$methodHandler->$setterMethod($value); |
24
|
3 |
|
} |
25
|
3 |
|
} |
26
|
|
|
|
27
|
3 |
|
return $methodHandler; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* getSetterMethodFromKey |
32
|
|
|
* Insert description here. |
33
|
|
|
* |
34
|
|
|
* @param $key |
35
|
|
|
* |
36
|
|
|
* @return |
37
|
|
|
*/ |
38
|
4 |
|
private function getSetterMethodFromKey($key) |
39
|
|
|
{ |
40
|
4 |
|
return 'set'.ucfirst($key); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* fillNull |
45
|
|
|
* Insert description here. |
46
|
|
|
* |
47
|
|
|
* @param $arguments |
48
|
|
|
* @param $value |
49
|
|
|
* |
50
|
|
|
* @return |
51
|
|
|
*/ |
52
|
1 |
|
private function fillNull($arguments, $value) |
53
|
|
|
{ |
54
|
1 |
|
$missingArguments = []; |
55
|
1 |
|
$keys = array_keys($arguments); |
56
|
1 |
|
$commonKeys = array_keys(Config::getCommonData()); |
|
|
|
|
57
|
1 |
|
$properties = $this->getReflectionProperties($this->reflection); |
|
|
|
|
58
|
|
|
|
59
|
1 |
|
foreach ($properties as $key) { |
60
|
1 |
|
if (!in_array($key, $keys)) { |
61
|
1 |
|
$missingArguments[] = $key; |
62
|
1 |
|
} |
63
|
1 |
|
} |
64
|
|
|
|
65
|
1 |
|
foreach ($missingArguments as $key) { |
66
|
1 |
|
$arguments[$key] = $value; |
67
|
1 |
|
} |
68
|
|
|
|
69
|
1 |
|
return $arguments; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param $arguments |
74
|
|
|
* @param $class |
75
|
|
|
* |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
6 |
|
private function prepareMethodHandler($arguments, $class) |
79
|
|
|
{ |
80
|
6 |
|
$this->validate($arguments, $class); |
|
|
|
|
81
|
|
|
|
82
|
3 |
|
if ($this->nullValues !== null) { |
83
|
1 |
|
$arguments = $this->fillNull($arguments, $this->nullValues); |
|
|
|
|
84
|
1 |
|
} |
85
|
|
|
|
86
|
3 |
|
$methodHandler = new $class(); |
87
|
|
|
|
88
|
3 |
|
foreach ($arguments as $key => $value) { |
89
|
3 |
|
$setterMethod = $this->getSetterMethodFromKey($key); |
90
|
3 |
|
$methodHandler->$setterMethod($value); |
91
|
3 |
|
} |
92
|
|
|
|
93
|
3 |
|
$methodHandler = $this->mergeCommonData($methodHandler, $arguments); |
94
|
|
|
|
95
|
3 |
|
return $methodHandler; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
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.