Passed
Push — master ( 4fcdf3...7b3d54 )
by Gabriel
04:16
created

TransformMethodsTrait::unserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 6
rs 10
ccs 0
cts 0
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
use Nip\Utility\Arr;
8
9
/**
10
 * Class TransformMethodsTrait
11
 * @package Nip\Collections\Traits
12
 */
13
trait TransformMethodsTrait
14
{
15
    /**
16
     * Get the values of a given key.
17
     *
18
     * @param string|array|int|null $value
19
     * @param string|null $key
20
     * @return static
21
     */
22
    public function pluck($value, $key = null)
23
    {
24
        return new static(Arr::pluck($this->items, $value, $key));
0 ignored issues
show
Unused Code introduced by
The call to Nip\Collections\Traits\T...odsTrait::__construct() has too many arguments starting with Nip\Utility\Arr::pluck($...s->items, $value, $key). ( Ignorable by Annotation )

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

24
        return /** @scrutinizer ignore-call */ new static(Arr::pluck($this->items, $value, $key));

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...
25
    }
26
27
    /**
28
     * Run a map over each of the items.
29
     *
30
     * @param callable $callback
31
     * @return static
32
     */
33
    public function map(callable $callback)
34
    {
35
        $keys = array_keys($this->items);
36
37
        $items = array_map($callback, $this->items, $keys);
38
39
        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

39
        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...
40
    }
41
42
    /**
43
     * Concatenate values of a given key as a string.
44
     *
45
     * @param string $value
46
     * @param string|null $glue
47
     * @return string
48
     */
49
    public function implode($value, $glue = null)
50
    {
51
        $first = $this->first();
0 ignored issues
show
Bug introduced by
It seems like first() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

51
        /** @scrutinizer ignore-call */ 
52
        $first = $this->first();
Loading history...
52
53
        if (is_array($first) || is_object($first)) {
54
            return implode($glue, $this->pluck($value)->all());
0 ignored issues
show
Bug introduced by
It seems like all() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

54
            return implode($glue, $this->pluck($value)->/** @scrutinizer ignore-call */ all());
Loading history...
Bug introduced by
It seems like $glue can also be of type null; however, parameter $glue of implode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

54
            return implode(/** @scrutinizer ignore-type */ $glue, $this->pluck($value)->all());
Loading history...
55
        }
56
57
        return implode($value, $this->items);
58
    }
59
60
    /**
61
     * Transform each item in the collection using a callback.
62
     *
63
     * @param callable $callback
64
     * @return $this
65
     */
66
    public function transform(callable $callback)
67
    {
68
        $this->items = $this->map($callback)->all();
0 ignored issues
show
Bug Best Practice introduced by
The property items does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
69
70
        return $this;
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    public function toArray(): array
77
    {
78
        return array_map(function ($value) {
79
            return $value instanceof AbstractCollection ? $value->toArray() : $value;
80
        }, $this->items);
81
    }
82
83
    /**
84
     * Returns a serialized string representation of this array object.
85
     *
86
     * @link http://php.net/manual/en/serializable.serialize.php Serializable::serialize()
87
     *
88
     * @return string a PHP serialized string.
89
     */
90
    public function serialize(): string
91
    {
92
        return serialize($this->items);
93
    }
94
95
    /**
96
     * Converts a serialized string representation into an instance object.
97
     *
98
     * @link http://php.net/manual/en/serializable.unserialize.php Serializable::unserialize()
99
     *
100
     * @param string $serialized A PHP serialized string to unserialize.
101
     *
102
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
103
     */
104
    public function unserialize($serialized): void
105
    {
106
        /** @var array<array-key, T> $data */
107
        $data = unserialize($serialized, ['allowed_classes' => false]);
108
109
        $this->items = $data;
0 ignored issues
show
Bug Best Practice introduced by
The property items does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
110
    }
111
112
    /**
113
     * Specify data which should be serialized to JSON
114
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
115
     * @return mixed data which can be serialized by <b>json_encode</b>,
116
     * which is a value of any type other than a resource.
117
     * @since 5.4.0
118
     */
119
    public function jsonSerialize()
120
    {
121
        return array_map(function ($value) {
122
            if ($value instanceof JsonSerializable) {
123
                return $value->jsonSerialize();
124
            } else {
125
                return $value;
126
            }
127
        }, $this->items);
128
    }
129
}
130