Bouhnosaure /
dogecoin-wallet-api
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 Bouhnosaure\Dogecoin; |
||
| 4 | |||
| 5 | trait ResponseArrayTrait |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * Gets data by using key with dotted notation. |
||
| 9 | * |
||
| 10 | * @param string|null $key |
||
| 11 | * |
||
| 12 | * @return mixed |
||
| 13 | */ |
||
| 14 | 36 | public function get($key = null) |
|
| 15 | { |
||
| 16 | 36 | $key = $this->constructKey($key); |
|
| 17 | |||
| 18 | 36 | if (is_null($key)) { |
|
| 19 | 21 | return $this->result(); |
|
| 20 | } |
||
| 21 | |||
| 22 | 16 | return $this->parseKey($key, function ($part, $result) { |
|
| 23 | 24 | if (isset($result[$part])) { |
|
| 24 | 24 | return $result[$part]; |
|
| 25 | } |
||
| 26 | 24 | }); |
|
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Checks if key exists. |
||
| 31 | * |
||
| 32 | * @param string|null $key |
||
| 33 | * |
||
| 34 | * @return bool |
||
| 35 | */ |
||
| 36 | 6 | public function exists($key = null) |
|
| 37 | { |
||
| 38 | 6 | $key = $this->constructKey($key); |
|
| 39 | |||
| 40 | 4 | return $this->parseKey($key, function ($part, $result) { |
|
| 41 | 6 | return array_key_exists($part, $result); |
|
| 42 | 6 | }); |
|
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Checks if key exists and not null. |
||
| 47 | * |
||
| 48 | * @param string|null $key |
||
| 49 | * |
||
| 50 | * @return bool |
||
| 51 | */ |
||
| 52 | 3 | public function has($key = null) |
|
| 53 | { |
||
| 54 | 3 | $key = $this->constructKey($key); |
|
| 55 | |||
| 56 | 3 | return $this->parseKey($key, function ($part, $result) { |
|
| 57 | 3 | return isset($result[$part]); |
|
| 58 | 3 | }); |
|
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Gets first element. |
||
| 63 | * |
||
| 64 | * @return mixed |
||
| 65 | */ |
||
| 66 | 3 | public function first() |
|
| 67 | { |
||
| 68 | 3 | $value = $this->get(); |
|
| 69 | |||
| 70 | 3 | if (is_array($value)) { |
|
| 71 | 3 | return reset($value); |
|
| 72 | } |
||
| 73 | |||
| 74 | 3 | return $value; |
|
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Gets last element. |
||
| 79 | * |
||
| 80 | * @return mixed |
||
| 81 | */ |
||
| 82 | 3 | public function last() |
|
| 83 | { |
||
| 84 | 3 | $value = $this->get(); |
|
| 85 | |||
| 86 | 3 | if (is_array($value)) { |
|
| 87 | 3 | return end($value); |
|
| 88 | } |
||
| 89 | |||
| 90 | 3 | return $value; |
|
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Checks if response contains value. |
||
| 95 | * |
||
| 96 | * @param mixed $value |
||
| 97 | * |
||
| 98 | * @return bool |
||
| 99 | */ |
||
| 100 | 3 | public function contains($value) |
|
| 101 | { |
||
| 102 | 3 | return in_array($value, $this->result()); |
|
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Set current key. |
||
| 107 | * |
||
| 108 | * @param string|null $key |
||
| 109 | * |
||
| 110 | * @return static |
||
| 111 | */ |
||
| 112 | 24 | public function key($key = null) |
|
| 113 | { |
||
| 114 | 24 | $new = clone $this; |
|
| 115 | 24 | $new->current = $key; |
|
|
0 ignored issues
–
show
|
|||
| 116 | |||
| 117 | 24 | return $new; |
|
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Gets response keys. |
||
| 122 | * |
||
| 123 | * @return array |
||
| 124 | */ |
||
| 125 | 3 | public function keys() |
|
| 126 | { |
||
| 127 | 3 | return array_keys($this->result()); |
|
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Gets response values. |
||
| 132 | * |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | 3 | public function values() |
|
| 136 | { |
||
| 137 | 3 | return array_values($this->result()); |
|
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Gets random value. |
||
| 142 | * |
||
| 143 | * @param int $number |
||
| 144 | * @param string|null $key |
||
| 145 | * |
||
| 146 | * @return mixed |
||
| 147 | */ |
||
| 148 | 3 | public function random($number = 1, $key = null) |
|
| 149 | { |
||
| 150 | 3 | $value = $this->get($key); |
|
| 151 | |||
| 152 | 3 | if (is_array($value)) { |
|
| 153 | 3 | $keys = array_keys($value); |
|
| 154 | 3 | $keysLength = count($keys); |
|
| 155 | |||
| 156 | 3 | shuffle($keys); |
|
| 157 | |||
| 158 | 3 | if ($number > $keysLength) { |
|
| 159 | 3 | $number = $keysLength; |
|
| 160 | 1 | } |
|
| 161 | |||
| 162 | 3 | for ($result = [], $count = 0; $count < $number; $count++) { |
|
| 163 | 3 | $result[$keys[$count]] = $value[$keys[$count]]; |
|
| 164 | 1 | } |
|
| 165 | |||
| 166 | 3 | return count($result) > 1 ? $result : current($result); |
|
| 167 | } |
||
| 168 | |||
| 169 | 3 | return $value; |
|
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Counts response items. |
||
| 174 | * |
||
| 175 | * @param string|null $key |
||
| 176 | * |
||
| 177 | * @return int |
||
| 178 | */ |
||
| 179 | 3 | public function count($key = null) |
|
| 180 | { |
||
| 181 | 3 | if (is_null($this->constructKey($key))) { |
|
| 182 | 3 | return count($this->result()); |
|
| 183 | } |
||
| 184 | |||
| 185 | 3 | if (!$this->exists($key)) { |
|
| 186 | 3 | return 0; |
|
| 187 | } |
||
| 188 | |||
| 189 | 3 | $value = $this->get($key); |
|
| 190 | |||
| 191 | 3 | if (is_array($value)) { |
|
| 192 | 3 | return count($value); |
|
| 193 | } |
||
| 194 | |||
| 195 | 3 | return 1; |
|
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Flattens multi-dimensional array. |
||
| 200 | * |
||
| 201 | * @param string|null $key |
||
| 202 | * |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | 6 | public function flatten($key = null) |
|
| 206 | { |
||
| 207 | 6 | $array = new \RecursiveIteratorIterator( |
|
| 208 | 6 | new \RecursiveArrayIterator((array) $this->get($key)) |
|
| 209 | 2 | ); |
|
| 210 | |||
| 211 | 6 | $tmp = []; |
|
| 212 | 6 | foreach ($array as $value) { |
|
| 213 | 6 | $tmp[] = $value; |
|
| 214 | 2 | } |
|
| 215 | |||
| 216 | 6 | return $tmp; |
|
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Gets sum of values. |
||
| 221 | * |
||
| 222 | * @param string|null $key |
||
| 223 | * |
||
| 224 | * @return float |
||
| 225 | */ |
||
| 226 | 3 | public function sum($key = null) |
|
| 227 | { |
||
| 228 | 3 | return array_sum($this->flatten($key)); |
|
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Get response item by key. |
||
| 233 | * |
||
| 234 | * @param string|null $key |
||
| 235 | * |
||
| 236 | * @return mixed |
||
| 237 | */ |
||
| 238 | 15 | public function __invoke($key = null) |
|
| 239 | { |
||
| 240 | 15 | return $this->key($key); |
|
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Constructs full key. |
||
| 245 | * |
||
| 246 | * @param string|null $key |
||
| 247 | * |
||
| 248 | * @return string|null |
||
| 249 | */ |
||
| 250 | 42 | protected function constructKey($key = null) |
|
| 251 | { |
||
| 252 | 42 | if (!is_null($key) && !is_null($this->current)) { |
|
| 253 | 9 | return $this->current.'.'.$key; |
|
| 254 | } |
||
| 255 | |||
| 256 | 42 | if (is_null($key) && !is_null($this->current)) { |
|
| 257 | 21 | return $this->current; |
|
| 258 | } |
||
| 259 | |||
| 260 | 39 | return $key; |
|
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Parses dotted notation. |
||
| 265 | * |
||
| 266 | * @param string $key |
||
| 267 | * @param callable $callback |
||
| 268 | * @param array|null $result |
||
| 269 | * |
||
| 270 | * @return mixed |
||
| 271 | */ |
||
| 272 | 30 | protected function parseKey($key, callable $callback, $result = null) |
|
| 273 | { |
||
| 274 | 30 | $parts = explode('.', trim($key, '.')); |
|
| 275 | 30 | $result = $result ?: $this->result(); |
|
| 276 | |||
| 277 | 30 | foreach ($parts as $index => $part) { |
|
| 278 | 30 | if ($part == '*') { |
|
| 279 | 6 | $sub = []; |
|
| 280 | |||
| 281 | 6 | foreach (array_keys($result) as $subKey) { |
|
| 282 | 6 | $path = $subKey; |
|
| 283 | |||
| 284 | 6 | if (isset($parts[$index + 1])) { |
|
| 285 | 6 | $pathParts = array_slice($parts, $index + 1); |
|
| 286 | 6 | $path .= '.'.implode('.', $pathParts); |
|
| 287 | 2 | } |
|
| 288 | |||
| 289 | 6 | $sub[$subKey] = $this->parseKey($path, $callback, $result); |
|
| 290 | 2 | } |
|
| 291 | |||
| 292 | 6 | return $sub; |
|
| 293 | } |
||
| 294 | |||
| 295 | 30 | if (!$return = $callback($part, $result)) { |
|
| 296 | 9 | return $return; |
|
| 297 | } |
||
| 298 | |||
| 299 | 30 | $result = $result[$part]; |
|
| 300 | 10 | } |
|
| 301 | |||
| 302 | 30 | 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
Loading history...
|
|||
| 303 | } |
||
| 304 | } |
||
| 305 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: