Issues (19)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Iterator.php (19 issues)

Upgrade to new PHP Analysis Engine

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
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
Consider making the return type a bit more specific; maybe use false|integer|string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
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
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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