1
|
|
|
<?php namespace Comodojo\Foundation\Base; |
2
|
|
|
|
3
|
|
|
use \InvalidArgumentException; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @package Comodojo Foundation |
7
|
|
|
* @author Marco Giovinazzi <[email protected]> |
8
|
|
|
* @license MIT |
9
|
|
|
* |
10
|
|
|
* LICENSE: |
11
|
|
|
* |
12
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
13
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
14
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
15
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
16
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
17
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
18
|
|
|
* THE SOFTWARE. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
trait ConfigurationParametersTrait { |
22
|
|
|
|
23
|
|
|
protected $parameters = []; |
24
|
|
|
|
25
|
5 |
|
public function get($parameter=null) { |
26
|
|
|
|
27
|
5 |
|
if ( is_null($parameter) ) return $this->parameters; |
28
|
|
|
|
29
|
4 |
|
return $this->getFromParts(self::splitParts($parameter)); |
30
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
2 |
|
public function set($parameter, $value) { |
34
|
|
|
|
35
|
2 |
|
$parts = self::splitParts($parameter); |
36
|
|
|
|
37
|
2 |
|
if ( empty($parts) ) throw new InvalidArgumentException("Invalid parameter $parameter"); |
38
|
|
|
|
39
|
2 |
|
$this->setFromParts($parts, $value); |
40
|
|
|
|
41
|
2 |
|
return $this; |
42
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
3 |
|
public function has($parameter) { |
46
|
|
|
|
47
|
3 |
|
return is_null($this->getFromParts(self::splitParts($parameter))) ? false : true; |
48
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
public function delete($parameter = null) { |
52
|
|
|
|
53
|
3 |
|
if ( is_null($parameter) ) { |
54
|
|
|
|
55
|
1 |
|
$this->parameters = []; |
56
|
1 |
|
return true; |
57
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
$parts = self::splitParts($parameter); |
61
|
|
|
|
62
|
2 |
|
if ( empty($parts) ) throw new InvalidArgumentException("Invalid parameter $parameter"); |
63
|
|
|
|
64
|
2 |
|
return $this->deleteFromParts($parts); |
65
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
6 |
|
protected function getFromParts(array $parts) { |
69
|
|
|
|
70
|
6 |
|
if ( empty($parts) ) return null; |
71
|
|
|
|
72
|
6 |
|
$reference = &$this->parameters; |
73
|
|
|
|
74
|
6 |
|
foreach ($parts as $part) { |
75
|
|
|
|
76
|
6 |
|
if ( !isset($reference[$part]) ) { |
77
|
4 |
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
5 |
|
$reference = &$reference[$part]; |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
5 |
|
$data = $reference; |
85
|
|
|
|
86
|
5 |
|
return $data; |
87
|
|
|
// |
88
|
|
|
// $data = $this->parameters; |
|
|
|
|
89
|
|
|
// |
90
|
|
|
// foreach ($parts as $part) { |
|
|
|
|
91
|
|
|
// |
92
|
|
|
// if ( isset($data[$part]) ) { |
|
|
|
|
93
|
|
|
// $data = $data[$part]; |
|
|
|
|
94
|
|
|
// } else { |
|
|
|
|
95
|
|
|
// return null; |
96
|
|
|
// } |
97
|
|
|
// |
98
|
|
|
// } |
99
|
|
|
// |
100
|
|
|
// return $data; |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
protected function setFromParts(array $parts, $value) { |
105
|
|
|
|
106
|
2 |
|
$reference = &$this->parameters; |
107
|
|
|
|
108
|
2 |
|
foreach ($parts as $part) { |
109
|
|
|
|
110
|
2 |
|
if ( !isset($reference[$part]) ) { |
111
|
2 |
|
$reference[$part] = []; |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
|
$reference = &$reference[$part]; |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
2 |
|
$reference = $value; |
119
|
|
|
|
120
|
2 |
|
return true; |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
protected function deleteFromParts(array $parts) { |
125
|
|
|
|
126
|
2 |
|
$reference = &$this->parameters; |
127
|
2 |
|
$l = count($parts); |
128
|
|
|
|
129
|
2 |
|
for ($i=0; $i < $l; $i++) { |
130
|
2 |
|
if ( !isset($reference[$parts[$i]]) ) { |
131
|
|
|
return false; |
132
|
|
|
} |
133
|
2 |
|
if ($i == $l-1) { |
134
|
2 |
|
unset($reference[$parts[$i]]); |
135
|
|
|
} else { |
136
|
1 |
|
$reference = &$reference[$parts[$i]]; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
2 |
|
return true; |
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
6 |
|
protected static function splitParts($parameter) { |
145
|
|
|
|
146
|
6 |
|
return preg_split('/(\s)?\.(\s)?/', $parameter, null, PREG_SPLIT_NO_EMPTY); |
147
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.