Dgame /
php-iterator
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 Dgame\Iterator; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Class Iterator |
||
| 7 | * @package Dgame\Iterator |
||
| 8 | */ |
||
| 9 | final class Iterator |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var array |
||
| 13 | */ |
||
| 14 | private $values = []; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Iterator constructor. |
||
| 18 | * |
||
| 19 | * @param array $values |
||
| 20 | */ |
||
| 21 | 34 | public function __construct(array $values) |
|
| 22 | { |
||
| 23 | 34 | $this->values = $values; |
|
| 24 | 34 | } |
|
| 25 | |||
| 26 | /** |
||
| 27 | * @param string $glue |
||
| 28 | * |
||
| 29 | * @return string |
||
| 30 | */ |
||
| 31 | 10 | public function implode(string $glue = ''): string |
|
| 32 | { |
||
| 33 | 10 | return implode($glue, $this->values); |
|
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @return array |
||
| 38 | */ |
||
| 39 | 12 | public function collect(): array |
|
| 40 | { |
||
| 41 | 12 | return $this->values; |
|
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @return bool |
||
| 46 | */ |
||
| 47 | public function isEmpty(): bool |
||
| 48 | { |
||
| 49 | return empty($this->values); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @return int |
||
| 54 | */ |
||
| 55 | 1 | public function length(): int |
|
| 56 | { |
||
| 57 | 1 | return count($this->values); |
|
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @return Iterator |
||
|
0 ignored issues
–
show
|
|||
| 62 | */ |
||
| 63 | 1 | public function values(): self |
|
| 64 | { |
||
| 65 | 1 | return new self(array_values($this->values)); |
|
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @return Iterator |
||
|
0 ignored issues
–
show
|
|||
| 70 | */ |
||
| 71 | 1 | public function keys(): self |
|
| 72 | { |
||
| 73 | 1 | return new self(array_keys($this->values)); |
|
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @return mixed |
||
| 78 | */ |
||
| 79 | 3 | public function first() |
|
| 80 | { |
||
| 81 | 3 | return reset($this->values); |
|
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return mixed |
||
| 86 | */ |
||
| 87 | 1 | public function last() |
|
| 88 | { |
||
| 89 | 1 | return end($this->values); |
|
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return mixed |
||
| 94 | */ |
||
| 95 | 3 | public function next() |
|
| 96 | { |
||
| 97 | 3 | return next($this->values); |
|
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return mixed |
||
| 102 | */ |
||
| 103 | public function previous() |
||
| 104 | { |
||
| 105 | return prev($this->values); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return mixed |
||
| 110 | */ |
||
| 111 | public function peek() |
||
| 112 | { |
||
| 113 | $value = $this->next(); |
||
| 114 | $this->previous(); |
||
| 115 | |||
| 116 | return $value; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @return mixed |
||
| 121 | */ |
||
| 122 | 1 | public function popBack() |
|
| 123 | { |
||
| 124 | 1 | return array_pop($this->values); |
|
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return mixed |
||
| 129 | */ |
||
| 130 | 1 | public function popFront() |
|
| 131 | { |
||
| 132 | 1 | return array_shift($this->values); |
|
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param int $amount |
||
| 137 | * |
||
| 138 | * @return Iterator |
||
|
0 ignored issues
–
show
|
|||
| 139 | */ |
||
| 140 | 6 | public function take(int $amount): self |
|
| 141 | { |
||
| 142 | 6 | return new self(array_slice($this->values, 0, $amount)); |
|
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param int $amount |
||
| 147 | * |
||
| 148 | * @return Iterator |
||
|
0 ignored issues
–
show
|
|||
| 149 | */ |
||
| 150 | 6 | public function skip(int $amount): self |
|
| 151 | { |
||
| 152 | 6 | return new self(array_slice($this->values, $amount)); |
|
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param int $times |
||
| 157 | * |
||
| 158 | * @return Iterator |
||
|
0 ignored issues
–
show
|
|||
| 159 | */ |
||
| 160 | 1 | public function repeat(int $times): self |
|
| 161 | { |
||
| 162 | 1 | $values = []; |
|
| 163 | 1 | for ($i = 0; $i < $times; $i++) { |
|
| 164 | 1 | $values = array_merge($values, $this->values); |
|
| 165 | } |
||
| 166 | |||
| 167 | 1 | return new self($values); |
|
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param int $from |
||
| 172 | * @param int $too |
||
| 173 | * |
||
| 174 | * @return Iterator |
||
|
0 ignored issues
–
show
|
|||
| 175 | */ |
||
| 176 | 1 | public function slice(int $from, int $too): self |
|
| 177 | { |
||
| 178 | 1 | return new self(array_slice($this->values, $from, $too - $from)); |
|
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param int $size |
||
| 183 | * |
||
| 184 | * @return Iterator |
||
|
0 ignored issues
–
show
|
|||
| 185 | */ |
||
| 186 | 1 | public function chunks(int $size): self |
|
| 187 | { |
||
| 188 | 1 | return new self(array_chunk($this->values, $size)); |
|
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param callable $callback |
||
| 193 | * @param null $initial |
||
| 194 | * |
||
| 195 | * @return mixed |
||
| 196 | */ |
||
| 197 | 1 | public function fold(callable $callback, $initial = null) |
|
| 198 | { |
||
| 199 | 1 | return array_reduce($this->values, $callback, $initial); |
|
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param callable|null $callback |
||
| 204 | * |
||
| 205 | * @return Iterator |
||
|
0 ignored issues
–
show
|
|||
| 206 | */ |
||
| 207 | 2 | public function filter(callable $callback = null): self |
|
| 208 | { |
||
| 209 | 2 | if ($callback === null) { |
|
| 210 | 2 | return new self(array_filter($this->values)); |
|
| 211 | } |
||
| 212 | |||
| 213 | return new self(array_filter($this->values, $callback)); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param callable $callback |
||
| 218 | * |
||
| 219 | * @return Iterator |
||
|
0 ignored issues
–
show
|
|||
| 220 | */ |
||
| 221 | public function map(callable $callback): self |
||
| 222 | { |
||
| 223 | return new self(array_map($callback, $this->values)); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @return Iterator |
||
|
0 ignored issues
–
show
|
|||
| 228 | */ |
||
| 229 | public function unique(): self |
||
| 230 | { |
||
| 231 | return new self(array_unique($this->values)); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @return Iterator |
||
|
0 ignored issues
–
show
|
|||
| 236 | */ |
||
| 237 | public function reverse(): self |
||
| 238 | { |
||
| 239 | return new self(array_reverse($this->values)); |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param callable $callback |
||
| 244 | * |
||
| 245 | * @return int |
||
| 246 | */ |
||
| 247 | 2 | private function countWhile(callable $callback): int |
|
| 248 | { |
||
| 249 | 2 | $index = 0; |
|
| 250 | 2 | foreach ($this->values as $key => $value) { |
|
| 251 | 2 | if (!$callback($value, $key)) { |
|
| 252 | 2 | break; |
|
| 253 | } |
||
| 254 | |||
| 255 | 2 | $index++; |
|
| 256 | } |
||
| 257 | |||
| 258 | 2 | return $index; |
|
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param callable $callback |
||
| 263 | * |
||
| 264 | * @return int |
||
| 265 | */ |
||
| 266 | 8 | private function countUntil(callable $callback): int |
|
| 267 | { |
||
| 268 | 8 | $index = 0; |
|
| 269 | 8 | foreach ($this->values as $key => $value) { |
|
| 270 | 8 | if ($callback($value, $key)) { |
|
| 271 | 8 | break; |
|
| 272 | } |
||
| 273 | |||
| 274 | 8 | $index++; |
|
| 275 | } |
||
| 276 | |||
| 277 | 8 | return $index; |
|
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param callable $callback |
||
| 282 | * |
||
| 283 | * @return Iterator |
||
|
0 ignored issues
–
show
|
|||
| 284 | */ |
||
| 285 | 1 | public function takeWhile(callable $callback): self |
|
| 286 | { |
||
| 287 | 1 | return $this->take($this->countWhile($callback)); |
|
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param callable $callback |
||
| 292 | * |
||
| 293 | * @return Iterator |
||
|
0 ignored issues
–
show
|
|||
| 294 | */ |
||
| 295 | 1 | public function skipWhile(callable $callback): self |
|
| 296 | { |
||
| 297 | 1 | return $this->skip($this->countWhile($callback)); |
|
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param $value |
||
| 302 | * |
||
| 303 | * @return Iterator |
||
|
0 ignored issues
–
show
|
|||
| 304 | */ |
||
| 305 | 4 | public function from($value): self |
|
| 306 | { |
||
| 307 | $amount = $this->countUntil(function ($val) use ($value) { |
||
| 308 | 4 | return $val === $value; |
|
| 309 | 4 | }); |
|
| 310 | |||
| 311 | 4 | return $this->skip($amount); |
|
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param $value |
||
| 316 | * |
||
| 317 | * @return Iterator |
||
|
0 ignored issues
–
show
|
|||
| 318 | */ |
||
| 319 | public function until($value): self |
||
| 320 | { |
||
| 321 | 4 | $amount = $this->countUntil(function ($val) use ($value) { |
|
| 322 | 4 | return $val === $value; |
|
| 323 | 4 | }); |
|
| 324 | |||
| 325 | 4 | return $this->take($amount + 1); |
|
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param $value |
||
| 330 | * |
||
| 331 | * @return Iterator |
||
|
0 ignored issues
–
show
|
|||
| 332 | */ |
||
| 333 | 2 | public function before($value): self |
|
| 334 | { |
||
| 335 | 2 | return $this->until($value)->take(-1); |
|
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param $value |
||
| 340 | * |
||
| 341 | * @return Iterator |
||
|
0 ignored issues
–
show
|
|||
| 342 | */ |
||
| 343 | 2 | public function after($value): self |
|
| 344 | { |
||
| 345 | 2 | return $this->from($value)->skip(1); |
|
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param $value |
||
| 350 | * |
||
| 351 | * @return mixed |
||
|
0 ignored issues
–
show
|
|||
| 352 | */ |
||
| 353 | 2 | public function keyOf($value) |
|
| 354 | { |
||
| 355 | 2 | return array_search($value, $this->values); |
|
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @param $value |
||
| 360 | * |
||
| 361 | * @return Iterator |
||
|
0 ignored issues
–
show
|
|||
| 362 | */ |
||
| 363 | 1 | public function keysOf($value): self |
|
| 364 | { |
||
| 365 | 1 | return new self(array_keys($this->values, $value)); |
|
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @param callable $callback |
||
| 370 | * |
||
| 371 | * @return bool |
||
| 372 | */ |
||
| 373 | 1 | public function all(callable $callback): bool |
|
| 374 | { |
||
| 375 | 1 | foreach ($this->values as $key => $value) { |
|
| 376 | 1 | if (!$callback($value, $key)) { |
|
| 377 | 1 | return false; |
|
| 378 | } |
||
| 379 | } |
||
| 380 | |||
| 381 | 1 | return true; |
|
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param callable $callback |
||
| 386 | * |
||
| 387 | * @return bool |
||
| 388 | */ |
||
| 389 | 1 | public function any(callable $callback): bool |
|
| 390 | { |
||
| 391 | 1 | foreach ($this->values as $key => $value) { |
|
| 392 | 1 | if ($callback($value, $key)) { |
|
| 393 | 1 | return true; |
|
| 394 | } |
||
| 395 | } |
||
| 396 | |||
| 397 | 1 | return false; |
|
| 398 | } |
||
| 399 | } |
||
| 400 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.