Passed
Push — master ( d2cc23...a71a12 )
by Gabriel
03:43
created

TransformMethodsTrait::__sleep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 0
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
    protected $serializable = ['items'];
16
17
    /**
18
     * Get the values of a given key.
19
     *
20
     * @param string|array|int|null $value
21
     * @param string|null $key
22
     * @return static
23
     */
24
    public function pluck($value, $key = null)
25
    {
26
        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

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

41
        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...
42
    }
43
44
    /**
45
     * Concatenate values of a given key as a string.
46
     *
47
     * @param string $value
48
     * @param string|null $glue
49
     * @return string
50
     */
51
    public function implode($value, $glue = null)
52
    {
53
        $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

53
        /** @scrutinizer ignore-call */ 
54
        $first = $this->first();
Loading history...
54
55
        if (is_array($first) || is_object($first)) {
56
            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

56
            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

56
            return implode(/** @scrutinizer ignore-type */ $glue, $this->pluck($value)->all());
Loading history...
57
        }
58
59
        return implode($value, $this->items);
60
    }
61
62
    /**
63
     * Transform each item in the collection using a callback.
64
     *
65
     * @param callable $callback
66
     * @return $this
67
     */
68
    public function transform(callable $callback)
69
    {
70
        $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...
71
72
        return $this;
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    public function toArray(): array
79
    {
80
        return array_map(function ($value) {
81
            return $value instanceof AbstractCollection ? $value->toArray() : $value;
82
        }, $this->items);
83
    }
84
85
    /**
86
     * Returns a serialized string representation of this array object.
87
     *
88
     * @link http://php.net/manual/en/serializable.serialize.php Serializable::serialize()
89
     *
90
     * @return string a PHP serialized string.
91
     */
92
    public function serialize(): string
93
    {
94
        $properties = $this->__sleep();
95
        $data = [];
96
        foreach ($properties as $property) {
97
            $data[$property] = $this->{$property};
98
        }
99
        return serialize($data);
100
    }
101
102
    /**
103
     * @return array
104
     */
105
    public function __sleep()
106
    {
107
        return $this->serializable;
108
    }
109
110
    /**
111
     * Converts a serialized string representation into an instance object.
112
     *
113
     * @link http://php.net/manual/en/serializable.unserialize.php Serializable::unserialize()
114
     *
115
     * @param string $serialized A PHP serialized string to unserialize.
116
     *
117
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
118
     */
119
    public function unserialize($serialized): void
120
    {
121
        /** @var array<array-key, T> $data */
122
        $data = unserialize($serialized, ['allowed_classes' => $this->unserializeAllowedClasses()]);
123
        if (!is_array($data)) {
124
            return;
125
        }
126
        foreach ($data as $property => $value) {
127
            $this->{$property} = $value;
128
        }
129
    }
130
131
    /**
132
     * Specify data which should be serialized to JSON
133
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
134
     * @return mixed data which can be serialized by <b>json_encode</b>,
135
     * which is a value of any type other than a resource.
136
     * @since 5.4.0
137
     */
138
    public function jsonSerialize()
139
    {
140
        return array_map(function ($value) {
141
            if ($value instanceof JsonSerializable) {
142
                return $value->jsonSerialize();
143
            } else {
144
                return $value;
145
            }
146
        }, $this->items);
147
    }
148
149
    protected function unserializeAllowedClasses()
150
    {
151
        return false;
152
    }
153
}
154