Completed
Push — master ( 6d0dd7...30241b )
by Sebastian
02:33
created

src/macros/transpose.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

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
 * Transpose an array.
7
 *
8
 * @return \Illuminate\Support\Collection
9
 *
10
 * @throws \LengthException
11
 */
12
Collection::macro('transpose', function (): Collection {
13
    if ($this->isEmpty()) {
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

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.

Loading history...
14
        return new static();
15
    }
16
17
    $firstItem = $this->first();
18
19
    $expectedLength = is_array($firstItem) || $firstItem instanceof Countable ? count($firstItem) : 0;
20
21
    array_walk($this->items, function ($row) use ($expectedLength) {
22
        if ((is_array($row) || $row instanceof Countable) && count($row) !== $expectedLength) {
23
            throw new \LengthException("Element's length must be equal.");
24
        }
25
    });
26
27
    $items = array_map(function (...$items) {
28
        return new static($items);
29
    }, ...array_map(function ($items) {
30
        return $this->getArrayableItems($items);
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

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.

Loading history...
31
    }, array_values($this->items)));
32
33
    return new static($items);
34
});
35