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

SectionBy   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 36
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A sectionBy() 0 23 5
1
<?php
2
3
namespace Spatie\CollectionMacros\Macros;
4
5
use Illuminate\Support\Collection;
6
7
class SectionBy
8
{
9
    /**
10
     * Splits a collection into sections grouped by a given key.
11
     *
12
     * @param mixed $key
0 ignored issues
show
Bug introduced by
There is no parameter named $key. 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
     * @param mixed $sectionKey
0 ignored issues
show
Bug introduced by
There is no parameter named $sectionKey. 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...
15
     * @param mixed $itemsKey
0 ignored issues
show
Bug introduced by
There is no parameter named $itemsKey. 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...
16
     *
17
     * @return \Illuminate\Support\Collection
18
     */
19
    public function sectionBy()
20
    {
21
        return function ($key, bool $preserveKeys = false, $sectionKey = 0, $itemsKey = 1): Collection {
22
            $sectionNameRetriever = $this->valueRetriever($key);
0 ignored issues
show
Bug introduced by
The method valueRetriever() does not seem to exist on object<Spatie\CollectionMacros\Macros\SectionBy>.

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...
23
24
            $results = new Collection();
25
26
            foreach ($this->items as $key => $value) {
0 ignored issues
show
Bug introduced by
The property items does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
                $sectionName = $sectionNameRetriever($value);
28
29
                if (! $results->last() || $results->last()->get($sectionKey) !== $sectionName) {
30
                    $results->push(new Collection([
31
                        $sectionKey => $sectionName,
32
                        $itemsKey => new Collection(),
33
                    ]));
34
                }
35
36
                $results->last()->get($itemsKey)->offsetSet($preserveKeys ? $key : null, $value);
37
            }
38
39
            return $results;
40
        };
41
    }
42
}
43