These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | use Illuminate\Support\Collection; |
||
4 | |||
5 | /* |
||
6 | * Slice a collection before a given callback is met into separate chunks |
||
7 | * |
||
8 | * @param callable $callback |
||
9 | * @param bool $preserveKeys |
||
10 | * |
||
11 | * @return \Illuminate\Support\Collection |
||
12 | */ |
||
13 | Collection::macro('sliceBefore', function ($callback, bool $preserveKeys = false): Collection { |
||
14 | if ($this->isEmpty()) { |
||
0 ignored issues
–
show
|
|||
15 | return new static(); |
||
16 | } |
||
17 | |||
18 | if (! $preserveKeys) { |
||
19 | $sliced = new static([ |
||
20 | new static([$this->first()]), |
||
21 | ]); |
||
22 | |||
23 | return $this->eachCons(2)->reduce(function ($sliced, $previousAndCurrent) use ($callback) { |
||
24 | list($previousItem, $item) = $previousAndCurrent; |
||
25 | |||
26 | $callback($item, $previousItem) |
||
27 | ? $sliced->push(new static([$item])) |
||
28 | : $sliced->last()->push($item); |
||
29 | |||
30 | return $sliced; |
||
31 | }, $sliced); |
||
32 | } |
||
33 | |||
34 | $sliced = new static([$this->take(1)]); |
||
35 | |||
36 | return $this->eachCons(2, $preserveKeys)->reduce(function ($sliced, $previousAndCurrent) use ($callback) { |
||
37 | $previousItem = $previousAndCurrent->take(1); |
||
38 | $item = $previousAndCurrent->take(-1); |
||
39 | |||
40 | $itemKey = $item->keys()->first(); |
||
41 | $valuesItem = $item->first(); |
||
42 | $valuesPreviousItem = $previousItem->first(); |
||
43 | |||
44 | $callback($valuesItem, $valuesPreviousItem) |
||
45 | ? $sliced->push($item) |
||
46 | : $sliced->last()->put($itemKey, $valuesItem); |
||
47 | |||
48 | return $sliced; |
||
49 | }, $sliced); |
||
50 | }); |
||
51 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.