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

Transpose   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B transpose() 0 26 7
1
<?php
2
3
namespace Spatie\CollectionMacros\Macros;
4
5
use Countable;
6
use LengthException;
7
use Illuminate\Support\Collection;
8
9
class Transpose
10
{
11
    /**
12
     * Transpose an array.
13
     *
14
     * @return \Illuminate\Support\Collection
15
     *
16
     * @throws \LengthException
17
     */
18
    public function transpose()
19
    {
20
        return function (): Collection {
21
            if ($this->isEmpty()) {
0 ignored issues
show
Bug introduced by
The method isEmpty() does not seem to exist on object<Spatie\CollectionMacros\Macros\Transpose>.

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...
22
                return new static();
23
            }
24
25
            $firstItem = $this->first();
0 ignored issues
show
Bug introduced by
The method first() does not seem to exist on object<Spatie\CollectionMacros\Macros\Transpose>.

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...
26
27
            $expectedLength = is_array($firstItem) || $firstItem instanceof Countable ? count($firstItem) : 0;
28
29
            array_walk($this->items, function ($row) use ($expectedLength) {
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...
30
                if ((is_array($row) || $row instanceof Countable) && count($row) !== $expectedLength) {
31
                    throw new LengthException("Element's length must be equal.");
32
                }
33
            });
34
35
            $items = array_map(function (...$items) {
36
                return new static($items);
0 ignored issues
show
Unused Code introduced by
The call to Transpose::__construct() has too many arguments starting with $items.

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...
37
            }, ...array_map(function ($items) {
38
                return $this->getArrayableItems($items);
0 ignored issues
show
Bug introduced by
The method getArrayableItems() does not seem to exist on object<Spatie\CollectionMacros\Macros\Transpose>.

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...
39
            }, array_values($this->items)));
40
41
            return new static($items);
0 ignored issues
show
Unused Code introduced by
The call to Transpose::__construct() has too many arguments starting with $items.

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...
42
        };
43
    }
44
}
45