This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Cocur\Vale; |
||
4 | |||
5 | use InvalidArgumentException; |
||
6 | |||
7 | /** |
||
8 | * Vale. |
||
9 | * |
||
10 | * @author Florian Eckerstorfer <[email protected]> |
||
11 | * @copyright 2015 Florian Eckerstorfer |
||
12 | * @license http://opensource.org/licenses/MIT The MIT License |
||
13 | */ |
||
14 | class Vale |
||
15 | { |
||
16 | /** |
||
17 | * @var Vale |
||
18 | */ |
||
19 | private static $instance; |
||
20 | |||
21 | /** |
||
22 | * @return Vale |
||
23 | */ |
||
24 | 1 | public static function instance() |
|
25 | { |
||
26 | 1 | if (null === self::$instance) { |
|
27 | 1 | self::$instance = new self(); |
|
28 | 1 | } |
|
29 | |||
30 | 1 | return self::$instance; |
|
31 | } |
||
32 | |||
33 | /** |
||
34 | * @param mixed $data |
||
35 | * @param array|string|int $keys |
||
36 | * @param mixed $default |
||
37 | * |
||
38 | * @return mixed |
||
39 | */ |
||
40 | 1 | public static function get($data, $keys, $default = null) |
|
41 | { |
||
42 | 1 | return self::instance()->getValue($data, $keys, $default); |
|
43 | } |
||
44 | |||
45 | /** |
||
46 | * @param mixed $data |
||
47 | * @param array|string|int $keys |
||
48 | * @param mixed $value |
||
49 | * |
||
50 | * @return mixed |
||
51 | */ |
||
52 | 1 | public static function set($data, $keys, $value) |
|
53 | { |
||
54 | 1 | return self::instance()->setValue($data, $keys, $value); |
|
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param mixed $data |
||
59 | * @param array|string|int $keys |
||
60 | * |
||
61 | * @return bool |
||
62 | */ |
||
63 | 1 | public static function has($data, $keys) |
|
64 | { |
||
65 | 1 | return self::instance()->hasValue($data, $keys); |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param mixed $data |
||
70 | * @param array|string|int $keys |
||
71 | * |
||
72 | * @return mixed |
||
73 | */ |
||
74 | 1 | public static function remove($data, $keys) |
|
75 | { |
||
76 | 1 | return self::instance()->removeValue($data, $keys); |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param mixed $data |
||
81 | * @param array|string|int $keys |
||
82 | * @param mixed|null $default |
||
83 | * |
||
84 | * @return mixed |
||
85 | */ |
||
86 | 4 | public function getValue($data, $keys, $default = null) |
|
87 | { |
||
88 | 4 | if ($this->isKeysEmpty($keys)) { |
|
89 | 1 | return $data; |
|
90 | } |
||
91 | 3 | if (!is_array($keys)) { |
|
92 | 1 | $keys = [$keys]; |
|
93 | 1 | } |
|
94 | |||
95 | 3 | $accessor = new Accessor($data); |
|
96 | 3 | foreach ($keys as $key) { |
|
97 | 3 | if ($accessor->to($key) === false) { |
|
98 | 1 | return $default; |
|
99 | } |
||
100 | 2 | } |
|
101 | |||
102 | 2 | return $accessor->getCurrent(); |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param mixed $data |
||
107 | * @param array|string|int $keys |
||
108 | * @param mixed $value |
||
109 | * |
||
110 | * @return mixed |
||
111 | */ |
||
112 | 5 | View Code Duplication | public function setValue($data, $keys, $value) |
0 ignored issues
–
show
|
|||
113 | { |
||
114 | 5 | if ($this->isKeysEmpty($keys)) { |
|
115 | 1 | return $data; |
|
116 | } |
||
117 | 4 | if (!is_array($keys)) { |
|
118 | 1 | $keys = [$keys]; |
|
119 | 1 | } |
|
120 | |||
121 | 4 | $accessor = new Accessor($data); |
|
122 | 4 | $depth = 0; |
|
123 | 4 | $keyCount = count($keys); |
|
124 | 4 | foreach ($keys as $key) { |
|
125 | 4 | if ($depth + 1 === $keyCount) { |
|
126 | 3 | if ($accessor->set($key, $value) === false) { |
|
127 | 1 | throw new InvalidArgumentException(sprintf( |
|
128 | 1 | 'Did not set path %s in structure %s', |
|
129 | 1 | json_encode($keys), |
|
130 | 1 | json_encode($data) |
|
131 | 1 | )); |
|
132 | } |
||
133 | 4 | } elseif ($accessor->to($key) === false) { |
|
134 | 1 | throw new InvalidArgumentException(sprintf( |
|
135 | 1 | 'Did not find path %s in structure %s', |
|
136 | 1 | json_encode($keys), |
|
137 | 1 | json_encode($data) |
|
138 | 1 | )); |
|
139 | } |
||
140 | |||
141 | 3 | ++$depth; |
|
142 | 3 | } |
|
143 | |||
144 | 2 | return $accessor->getData(); |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * @param mixed $data |
||
149 | * @param array $keys |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | 4 | public function hasValue($data, $keys) |
|
154 | { |
||
155 | 4 | if ($this->isKeysEmpty($keys)) { |
|
156 | 1 | return true; |
|
157 | } |
||
158 | 3 | if (!is_array($keys)) { |
|
159 | 1 | $keys = [$keys]; |
|
160 | 1 | } |
|
161 | |||
162 | 3 | $accessor = new Accessor($data); |
|
163 | 3 | $keyCount = count($keys); |
|
164 | 3 | $depth = 0; |
|
165 | 3 | foreach ($keys as $key) { |
|
166 | 3 | if (($depth + 1 === $keyCount && $accessor->has($key) === false) || $accessor->to($key) === false) { |
|
167 | 1 | return false; |
|
168 | } |
||
169 | |||
170 | 3 | ++$depth; |
|
171 | 3 | } |
|
172 | |||
173 | 2 | return true; |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * @param mixed $data |
||
178 | * @param array|string|int $keys |
||
179 | * |
||
180 | * @return mixed |
||
181 | */ |
||
182 | 5 | View Code Duplication | public function removeValue($data, $keys) |
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
183 | { |
||
184 | 5 | if ($this->isKeysEmpty($keys)) { |
|
185 | 1 | return; |
|
186 | } |
||
187 | 4 | if (!is_array($keys)) { |
|
188 | 1 | $keys = [$keys]; |
|
189 | 1 | } |
|
190 | |||
191 | 4 | $accessor = new Accessor($data); |
|
192 | 4 | $keyCount = count($keys); |
|
193 | 4 | $depth = 0; |
|
194 | 4 | foreach ($keys as $key) { |
|
195 | 4 | if ($depth + 1 === $keyCount) { |
|
196 | 3 | if ($accessor->remove($key) === false) { |
|
197 | 1 | throw new InvalidArgumentException(sprintf( |
|
198 | 1 | 'Did not remove path %s in structure %s', |
|
199 | 1 | json_encode($keys), |
|
200 | 1 | json_encode($data) |
|
201 | 1 | )); |
|
202 | } |
||
203 | 4 | } elseif ($accessor->to($key) === false) { |
|
204 | 1 | throw new InvalidArgumentException(sprintf( |
|
205 | 1 | 'Did not find path %s in structure %s', |
|
206 | 1 | json_encode($keys), |
|
207 | 1 | json_encode($data) |
|
208 | 1 | )); |
|
209 | } |
||
210 | |||
211 | 3 | ++$depth; |
|
212 | 3 | } |
|
213 | |||
214 | 2 | return $accessor->getData(); |
|
215 | } |
||
216 | |||
217 | /** |
||
218 | * @param string[]|string|null $keys |
||
219 | * |
||
220 | * @return bool |
||
221 | */ |
||
222 | 1 | protected function isKeysEmpty($keys) |
|
223 | { |
||
224 | 1 | return $keys === null || $keys === '' || count($keys) === 0; |
|
225 | } |
||
226 | } |
||
227 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.