Completed
Pull Request — master (#156)
by Lucas
03:54 queued 01:43
created

SliceBefore::sliceBefore()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 9.0008
c 0
b 0
f 0
cc 5
nc 1
nop 0
1
<?php
2
3
namespace Spatie\CollectionMacros\Macros;
4
5
use Illuminate\Support\Collection;
6
7
class SliceBefore
8
{
9
    /**
10
     * Slice a collection before a given callback is met into separate chunks.
11
     *
12
     * @param callable $callback
0 ignored issues
show
Bug introduced by
There is no parameter named $callback. 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 $italy is not defined by the method finale(...).

/**
 * @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.

Loading history...
13
     * @param bool $preserveKeys
0 ignored issues
show
Bug introduced by
There is no parameter named $preserveKeys. 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 $italy is not defined by the method finale(...).

/**
 * @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.

Loading history...
14
     *
15
     * @return \Illuminate\Support\Collection
16
     */
17
    public function sliceBefore()
18
    {
19
        return function ($callback, bool $preserveKeys = false): Collection {
20
            if ($this->isEmpty()) {
0 ignored issues
show
Bug introduced by
The method isEmpty() does not seem to exist on object<Spatie\Collection...ros\Macros\SliceBefore>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
21
                return new static();
22
            }
23
24
            if (! $preserveKeys) {
25
                $sliced = new static([
0 ignored issues
show
Unused Code introduced by
The call to SliceBefore::__construct() has too many arguments starting with array(new static(array($this->first()))).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
26
                    new static([$this->first()]),
0 ignored issues
show
Bug introduced by
The method first() does not seem to exist on object<Spatie\Collection...ros\Macros\SliceBefore>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Unused Code introduced by
The call to SliceBefore::__construct() has too many arguments starting with array($this->first()).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
27
                ]);
28
29
                return $this->eachCons(2)->reduce(function ($sliced, $previousAndCurrent) use ($callback) {
0 ignored issues
show
Bug introduced by
The method eachCons() does not seem to exist on object<Spatie\Collection...ros\Macros\SliceBefore>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
                    list($previousItem, $item) = $previousAndCurrent;
31
32
                    $callback($item, $previousItem) ? $sliced->push(new static([$item])) : $sliced->last()->push($item);
0 ignored issues
show
Unused Code introduced by
The call to SliceBefore::__construct() has too many arguments starting with array($item).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
33
34
                    return $sliced;
35
                }, $sliced);
36
            }
37
38
            $sliced = new static([$this->take(1)]);
0 ignored issues
show
Bug introduced by
The method take() does not seem to exist on object<Spatie\Collection...ros\Macros\SliceBefore>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Unused Code introduced by
The call to SliceBefore::__construct() has too many arguments starting with array($this->take(1)).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
39
40
            return $this->eachCons(2, $preserveKeys)->reduce(function ($sliced, $previousAndCurrent) use ($callback) {
0 ignored issues
show
Bug introduced by
The method eachCons() does not seem to exist on object<Spatie\Collection...ros\Macros\SliceBefore>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
41
                $previousItem = $previousAndCurrent->take(1);
42
                $item = $previousAndCurrent->take(-1);
43
44
                $itemKey = $item->keys()->first();
45
                $valuesItem = $item->first();
46
                $valuesPreviousItem = $previousItem->first();
47
48
                $callback($valuesItem, $valuesPreviousItem) ? $sliced->push($item) : $sliced->last()
49
                                                                                            ->put($itemKey, $valuesItem);
50
51
                return $sliced;
52
            }, $sliced);
53
        };
54
    }
55
}
56