Completed
Push — master ( 785bde...8b4d11 )
by Freek
01:29
created

Transpose::__invoke()   B

Complexity

Conditions 7
Paths 1

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.5706
c 0
b 0
f 0
cc 7
nc 1
nop 0
1
<?php
2
3
namespace Spatie\CollectionMacros\Macros;
4
5
use Illuminate\Support\Collection;
6
7
/*
8
 * Transpose an array.
9
 *
10
 * @return \Illuminate\Support\Collection
11
 *
12
 * @throws \LengthException
13
 */
14
class Transpose
15
{
16
    public function __invoke()
17
    {
18
        return function (): Collection {
19
            if ($this->isEmpty()) {
0 ignored issues
show
Bug introduced by Lucas Michot
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...
20
                return new static();
21
            }
22
23
            $firstItem = $this->first();
0 ignored issues
show
Bug introduced by Lucas Michot
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...
24
25
            $expectedLength = is_array($firstItem) || $firstItem instanceof Countable ? count($firstItem) : 0;
0 ignored issues
show
Bug introduced by Rod Elias
The class Spatie\CollectionMacros\Macros\Countable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
26
27
            array_walk($this->items, function ($row) use ($expectedLength) {
0 ignored issues
show
Bug introduced by Lucas Michot
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...
28
                if ((is_array($row) || $row instanceof Countable) && count($row) !== $expectedLength) {
0 ignored issues
show
Bug introduced by Rod Elias
The class Spatie\CollectionMacros\Macros\Countable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
29
                    throw new \LengthException("Element's length must be equal.");
30
                }
31
            });
32
33
            $items = array_map(function (...$items) {
34
                return new static($items);
0 ignored issues
show
Unused Code introduced by Lucas Michot
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...
35
            }, ...array_map(function ($items) {
36
                return $this->getArrayableItems($items);
0 ignored issues
show
Bug introduced by Lucas Michot
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...
37
            }, array_values($this->items)));
38
39
            return new static($items);
0 ignored issues
show
Unused Code introduced by Lucas Michot
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...
40
        };
41
    }
42
}
43