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 | class Nip_Helper_Arrays extends Nip\Helpers\AbstractHelper |
||
0 ignored issues
–
show
|
|||
4 | { |
||
5 | |||
6 | /** |
||
7 | * Determine whether the given value is array accessible. |
||
8 | * |
||
9 | * @param mixed $value |
||
10 | * @return bool |
||
11 | */ |
||
12 | public static function accessible($value) |
||
13 | { |
||
14 | return is_array($value) || $value instanceof ArrayAccess; |
||
15 | } |
||
16 | |||
17 | /** |
||
18 | * Determine if the given key exists in the provided array. |
||
19 | * |
||
20 | * @param \ArrayAccess|array $array |
||
21 | * @param string|int $key |
||
22 | * @return bool |
||
23 | */ |
||
24 | public static function exists($array, $key) |
||
25 | { |
||
26 | if ($array instanceof ArrayAccess) { |
||
27 | return $array->offsetExists($key); |
||
28 | } |
||
29 | return array_key_exists($key, $array); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Return the first element in an array passing a given truth test. |
||
34 | * |
||
35 | * @param array $array |
||
36 | * @param callable|null $callback |
||
37 | * @param mixed $default |
||
38 | * @return mixed |
||
39 | */ |
||
40 | public static function first($array, callable $callback = null, $default = null) |
||
41 | { |
||
42 | if (is_null($callback)) { |
||
43 | if (empty($array)) { |
||
44 | return value($default); |
||
45 | } |
||
46 | foreach ($array as $item) { |
||
47 | return $item; |
||
48 | } |
||
49 | } |
||
50 | foreach ($array as $key => $value) { |
||
51 | if (call_user_func($callback, $value, $key)) { |
||
52 | return $value; |
||
53 | } |
||
54 | } |
||
55 | return value($default); |
||
56 | } |
||
57 | |||
58 | public function toXLS($array, $filename, $labels = array()) |
||
59 | { |
||
60 | $xls = new Spreadsheet_Excel_Writer(); |
||
61 | $xls->setVersion(8); |
||
62 | |||
63 | $sheet = $xls->addWorksheet(); |
||
64 | $sheet->setInputEncoding("UTF-8"); |
||
65 | |||
66 | $heading = $xls->addFormat(['bold' => '1', 'align' => 'center']); |
||
67 | |||
68 | if ($array && !$labels) { |
||
0 ignored issues
–
show
The expression
$labels of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using ![]() |
|||
69 | $labels = array_keys(reset($array)); |
||
70 | } |
||
71 | |||
72 | $i = 0; |
||
73 | foreach ($labels as $label) { |
||
74 | $sheet->write(0, $i, $label, $heading); |
||
75 | $i++; |
||
76 | } |
||
77 | |||
78 | if (count($array)) { |
||
79 | $line = 1; |
||
80 | foreach ($array as $item) { |
||
81 | $column = 0; |
||
82 | foreach ($labels as $label) { |
||
83 | $sheet->write($line, $column, html_entity_decode($item[$label], ENT_QUOTES, 'UTF-8')); |
||
84 | $column++; |
||
85 | } |
||
86 | $line++; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | header("Cache-Control: private, max-age=1, pre-check=1", true); |
||
91 | header("Pragma: none", true); |
||
92 | |||
93 | $xls->send($filename); |
||
94 | $xls->close(); |
||
95 | exit(); |
||
0 ignored issues
–
show
The method
toXLS() contains an exit expression.
An exit expression should only be used in rare cases. For example, if you write a short command line script. In most cases however, using an ![]() |
|||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Get an item from an array using "dot" notation. |
||
100 | * |
||
101 | * @param \ArrayAccess|array $array |
||
102 | * @param string $key |
||
103 | * @param mixed $default |
||
104 | * @return mixed |
||
105 | */ |
||
106 | public static function get($array, $key, $default = null) |
||
107 | { |
||
108 | if (!static::accessible($array)) { |
||
109 | return value($default); |
||
110 | } |
||
111 | if (is_null($key)) { |
||
112 | return $array; |
||
113 | } |
||
114 | if (static::exists($array, $key)) { |
||
115 | return $array[$key]; |
||
116 | } |
||
117 | foreach (explode('.', $key) as $segment) { |
||
118 | if (static::accessible($array) && static::exists($array, $segment)) { |
||
119 | $array = $array[$segment]; |
||
120 | } else { |
||
121 | return value($default); |
||
122 | } |
||
123 | } |
||
124 | return $array; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Produces a new version of the array that does not contain any of the specified values |
||
129 | * |
||
130 | * @param array $array |
||
131 | * @return array |
||
132 | */ |
||
133 | View Code Duplication | public function without($array) |
|
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. ![]() |
|||
134 | { |
||
135 | $values = func_get_args(); |
||
136 | unset($values[0]); |
||
137 | |||
138 | if ($values) { |
||
0 ignored issues
–
show
The expression
$values of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using ![]() |
|||
139 | foreach ($values as $value) { |
||
140 | unset($array[array_search($value, $array)]); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | return $array; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Produces a new version of the array that does not contain any of the specified keys |
||
149 | * |
||
150 | * @param array $array |
||
151 | * @return array |
||
152 | */ |
||
153 | View Code Duplication | public function withoutKeys($array) |
|
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. ![]() |
|||
154 | { |
||
155 | $values = func_get_args(); |
||
156 | unset($values[0]); |
||
157 | |||
158 | if ($values) { |
||
0 ignored issues
–
show
The expression
$values of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using ![]() |
|||
159 | foreach ($values as $value) { |
||
160 | unset($array[$value]); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | return $array; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Fetch the same property for all the elements. |
||
169 | * |
||
170 | * @param array $array |
||
171 | * @param string $property |
||
172 | * @return array The property values |
||
173 | */ |
||
174 | public function changeKey($array, $property) |
||
175 | { |
||
176 | $return = []; |
||
177 | |||
178 | if (count($array) > 0) { |
||
179 | foreach ($array as $item) { |
||
180 | $return[$item->$property] = $item; |
||
181 | } |
||
182 | } |
||
183 | |||
184 | return $return; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Fetch the same property for all the elements. |
||
189 | * |
||
190 | * @param array|\Nip\Records\Collections\Collection $array |
||
191 | * @param string $property |
||
192 | * @param bool|string $return |
||
193 | * @return array The property values |
||
194 | */ |
||
195 | public function pluck($array, $property, &$return = false) |
||
196 | { |
||
197 | $return = []; |
||
198 | |||
199 | if (count($array) > 0) { |
||
200 | foreach ($array as $item) { |
||
201 | if (is_array($item)) { |
||
202 | $this->pluck($array, $property, $return); |
||
203 | } |
||
204 | |||
205 | $return[] = $item->$property; |
||
206 | } |
||
207 | } |
||
208 | |||
209 | return $return; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Fetch the same property for all the elements. |
||
214 | * |
||
215 | * @param array $array |
||
216 | * @param string $property |
||
217 | */ |
||
218 | public function pluckFromArray($array, $property) |
||
219 | { |
||
220 | if (is_array($array)) { |
||
221 | foreach ($array as $item) { |
||
222 | $return[] = $item[$property]; |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$return was never initialized. Although not strictly required by PHP, it is generally a good practice to add $return = array(); before regardless.
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code. Let’s take a look at an example: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop. ![]() |
|||
223 | } |
||
224 | } |
||
225 | |||
226 | return $return; |
||
0 ignored issues
–
show
The variable
$return does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
![]() |
|||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Finds array item that matches $params |
||
231 | * |
||
232 | * @param ArrayAccess $array |
||
233 | * @param array $params |
||
234 | * @return mixed |
||
235 | */ |
||
236 | public function find($array, $params) |
||
237 | { |
||
238 | if (count($array)) { |
||
239 | foreach ($array as $item) { |
||
240 | $found = true; |
||
241 | foreach ($params as $key => $value) { |
||
242 | |||
243 | if ($item->$key != $value) { |
||
244 | $found = false; |
||
245 | } |
||
246 | } |
||
247 | if ($found) { |
||
248 | return $item; |
||
249 | } |
||
250 | } |
||
251 | } |
||
252 | |||
253 | return null; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Finds all array items that match $params |
||
258 | * |
||
259 | * @param array $array |
||
260 | * @param array $params |
||
261 | * @param string $key |
||
0 ignored issues
–
show
There is no parameter named
$key . Was it maybe removed?
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. Consider the following example. The parameter /**
* @param array $germany
* @param array $island
* @param array $italy
*/
function finale($germany, $island) {
return "2:1";
}
The most likely cause is that the parameter was removed, but the annotation was not. ![]() |
|||
262 | * @return array |
||
263 | */ |
||
264 | public function findAll($array, $params, $returnKey = false) |
||
265 | { |
||
266 | $return = []; |
||
267 | |||
268 | if (count($array)) { |
||
269 | foreach ($array as $item) { |
||
270 | $found = true; |
||
271 | foreach ($params as $key => $value) { |
||
272 | if ($item->$key != $value) { |
||
273 | $found = false; |
||
274 | } |
||
275 | } |
||
276 | if ($found) { |
||
277 | if ($returnKey) { |
||
278 | $return[$item->$returnKey] = $item; |
||
279 | } else { |
||
280 | $return[] = $item; |
||
281 | } |
||
282 | } |
||
283 | } |
||
284 | } |
||
285 | |||
286 | return $return; |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Transposes a bidimensional array (matrix) |
||
291 | * |
||
292 | * @param array $array |
||
293 | * @return array |
||
294 | */ |
||
295 | public function transpose($array) |
||
296 | { |
||
297 | $return = []; |
||
298 | |||
299 | if (count($array)) { |
||
300 | foreach ($array as $key => $values) { |
||
301 | foreach ($values as $subkey => $value) { |
||
302 | $return[$subkey][$key] = $value; |
||
303 | } |
||
304 | } |
||
305 | } |
||
306 | |||
307 | return $return; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Pass in a multi dimensional array and this recrusively loops through and builds up an XML document. |
||
312 | * |
||
313 | * @param array $data |
||
314 | * @param string $rootNodeName - what you want the root node to be - defaults to data |
||
315 | * @param SimpleXMLElement $xml - should only be used recursively |
||
316 | * @return string XML |
||
317 | */ |
||
318 | public function toXML($data, $rootNodeName = 'ResultSet', &$xml = null) |
||
319 | { |
||
320 | // turn off compatibility mode as simple xml throws a wobbly if you don't. |
||
321 | if (ini_get('zend.ze1_compatibility_mode') == 1) |
||
322 | ini_set('zend.ze1_compatibility_mode', 0); |
||
323 | |||
324 | if (is_null($xml)) { |
||
325 | $xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />"); |
||
326 | } |
||
327 | |||
328 | // loop through the data passed in. |
||
329 | foreach ($data as $key => $value) { |
||
330 | // no numeric keys in our xml please! |
||
331 | if (is_numeric($key)) { |
||
332 | $numeric = 1; |
||
333 | $key = $rootNodeName; |
||
334 | } |
||
335 | |||
336 | // delete any char not allowed in XML element names |
||
337 | $key = preg_replace('/[^a-z0-9\-\_\.\:]/i', '', $key); |
||
338 | |||
339 | // if there is another array found recrusively call this function |
||
340 | if (is_array($value)) { |
||
341 | $node = $this->isAssoc($value) || $numeric ? $xml->addChild($key) : $xml; |
||
0 ignored issues
–
show
The variable
$numeric does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
![]() |
|||
342 | |||
343 | // recursive call |
||
344 | if ($numeric) |
||
345 | $key = 'anon'; |
||
346 | $this->toXML($value, $key, $node); |
||
347 | } else { |
||
348 | // add single node. |
||
349 | $value = htmlentities($value); |
||
350 | // $xml->addChild($key, $value); |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
73% of this comment could be valid code. Did you maybe forget this after debugging?
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. ![]() |
|||
351 | $xml->addAttribute($key, $value); |
||
352 | } |
||
353 | } |
||
354 | |||
355 | // pass back as XML |
||
356 | // return $xml->asXML(); |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
67% of this comment could be valid code. Did you maybe forget this after debugging?
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. ![]() |
|||
357 | // if you want the XML to be formatted, use the below instead to return the XML |
||
358 | $doc = new DOMDocument('1.0'); |
||
359 | $doc->preserveWhiteSpace = false; |
||
360 | $doc->loadXML($xml->asXML()); |
||
361 | $doc->formatOutput = true; |
||
362 | return $doc->saveXML(); |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Determine if a variable is an associative array |
||
367 | * |
||
368 | * @param array $array |
||
369 | * @return bool |
||
370 | */ |
||
371 | public static function isAssoc($array) |
||
372 | { |
||
373 | return (is_array($array) && 0 !== count(array_diff_key($array, array_keys(array_keys($array))))); |
||
374 | } |
||
375 | |||
376 | function merge_distinct(array &$array1, array &$array2) |
||
0 ignored issues
–
show
|
|||
377 | { |
||
378 | $merged = $array1; |
||
379 | |||
380 | foreach ($array2 as $key => &$value) { |
||
381 | if (is_array($value) && isset($merged [$key]) && is_array($merged [$key])) { |
||
382 | $merged [$key] = $this->merge_distinct($merged [$key], $value); |
||
383 | } else { |
||
384 | $merged [$key] = $value; |
||
385 | } |
||
386 | } |
||
387 | |||
388 | return $merged; |
||
389 | } |
||
390 | } |
||
391 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.