Passed
Push — master ( deb522...4fcdf3 )
by Gabriel
02:32
created

TransformMethodsTrait::map()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
rs 10
ccs 0
cts 4
cp 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Nip\Collections\Traits;
4
5
use JsonSerializable;
6
use Nip\Collections\AbstractCollection;
7
8
/**
9
 * Class TransformMethodsTrait
10
 * @package Nip\Collections\Traits
11
 */
12
trait TransformMethodsTrait
13
{
14
    /**
15
     * Run a map over each of the items.
16
     *
17
     * @param  callable  $callback
18
     * @return static
19
     */
20
    public function map(callable $callback)
21
    {
22
        $keys = array_keys($this->items);
23
24
        $items = array_map($callback, $this->items, $keys);
25
26
        return new static(array_combine($keys, $items));
0 ignored issues
show
Unused Code introduced by
The call to Nip\Collections\Traits\T...odsTrait::__construct() has too many arguments starting with array_combine($keys, $items). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
        return /** @scrutinizer ignore-call */ new static(array_combine($keys, $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. Please note the @ignore annotation hint above.

Loading history...
27
    }
28
29
    /**
30
     * @return array
31
     */
32
    public function toArray()
33
    {
34
        return array_map(function ($value) {
35
            return $value instanceof AbstractCollection ? $value->toArray() : $value;
36
        }, $this->items);
37
    }
38
39
    /**
40
     * Specify data which should be serialized to JSON
41
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
42
     * @return mixed data which can be serialized by <b>json_encode</b>,
43
     * which is a value of any type other than a resource.
44
     * @since 5.4.0
45
     */
46
    public function jsonSerialize()
47
    {
48
        return array_map(function ($value) {
49
            if ($value instanceof JsonSerializable) {
50
                return $value->jsonSerialize();
51
            } else {
52
                return $value;
53
            }
54
        }, $this->items);
55
    }
56
}
57