Passed
Push — master ( de0918...331287 )
by Sebastian
03:34
created

ArrayListTrait::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: seboettg
5
 * Date: 09.05.18
6
 * Time: 17:50
7
 */
8
9
namespace Seboettg\Collection;
10
11
12
trait ArrayListTrait
13
{
14
    /**
15
     * internal array
16
     *
17
     * @var array
18
     */
19
    protected $array;
20
21
    /**
22
     * flush array list
23
     *
24
     * @return $this
25
     */
26
    public function clear()
27
    {
28
        $this->array = [];
29
        return $this;
30
    }
31
32
    /**
33
     * returns element with key $key
34
     * @param $key
35
     * @return mixed|null
36
     */
37
    public function get($key)
38
    {
39
        return isset($this->array[$key]) ? $this->array[$key] : null;
40
    }
41
42
    /**
43
     * Returns the value of the array element that's currently being pointed to by the
44
     * internal pointer. It does not move the pointer in any way. If the
45
     * internal pointer points beyond the end of the elements list or the array is
46
     * empty, current returns false.
47
     *
48
     * @return mixed|false
49
     */
50
    public function current()
51
    {
52
        return current($this->array);
53
    }
54
55
    /**
56
     * Advance the internal array pointer of an array.
57
     * Returns the array value in the next place that's pointed to by the
58
     * internal array pointer, or false if there are no more elements.
59
     *
60
     * @return mixed|false
61
     */
62
    public function next()
63
    {
64
        return next($this->array);
65
    }
66
67
    /**
68
     * Rewind the internal array pointer.
69
     * Returns the array value in the previous place that's pointed to by
70
     * the internal array pointer, or false if there are no more
71
     *
72
     * @return mixed|false
73
     */
74
    public function prev()
75
    {
76
        return prev($this->array);
77
    }
78
79
    /**
80
     * Inserts or replaces the element at the specified position in this list with the specified element.
81
     *
82
     * @param $key
83
     * @param $element
84
     * @return $this
85
     */
86
    public function set($key, $element)
87
    {
88
        $this->array[$key] = $element;
89
        return $this;
90
    }
91
92
    /**
93
     * overrides contents of ArrayList with the contents of $array
94
     * @param array $array
95
     * @return $this
96
     */
97
    public function setArray(array $array)
98
    {
99
        return $this->replace($array);
100
    }
101
102
    /**
103
     * Appends the specified element to the end of this list.
104
     *
105
     * @param $element
106
     * @return $this
107
     */
108
    public function append($element)
109
    {
110
        $this->array[] = $element;
111
        return $this;
112
    }
113
114
    /**
115
     * Inserts the specified element at the specified position in this list. If an other element already exist at the
116
     * specified position the affected positions will transformed into a numerated array. As well the existing element
117
     * as the specified element will be appended to this array.
118
     *
119
     * @param $key
120
     * @param $element
121
     * @return $this
122
     */
123
    public function add($key, $element)
124
    {
125
126
        if (!array_key_exists($key, $this->array)) {
127
            $this->array[$key] = $element;
128
        } elseif (is_array($this->array[$key])) {
129
            $this->array[$key][] = $element;
130
        } else {
131
            $this->array[$key] = [$this->array[$key], $element];
132
        }
133
134
        return $this;
135
    }
136
137
    /**
138
     * Removes the element at the specified position in this list.
139
     *
140
     * @param $key
141
     * @return $this
142
     */
143
    public function remove($key)
144
    {
145
        unset($this->array[$key]);
146
        return $this;
147
    }
148
149
    /**
150
     * Returns true if an element exists on the specified position.
151
     *
152
     * @param mixed $key
153
     * @return bool
154
     */
155
    public function hasKey($key)
156
    {
157
        return array_key_exists($key, $this->array);
158
    }
159
160
    /**
161
     * Returns true if the specified value exists in this list. Uses PHP's array_search function
162
     * @link http://php.net/manual/en/function.array-search.php
163
     *
164
     * @param string $value
165
     *
166
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use boolean.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
167
     */
168
    public function hasValue($value)
169
    {
170
        $result = array_search($value, $this->array, true);
171
        return ($result !== false);
172
    }
173
174
    /**
175
     * replaces this list by the specified array
176
     * @param array $data
177
     *
178
     * @return ArrayList
0 ignored issues
show
Documentation introduced by
Should the return type not be ArrayListTrait?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
179
     */
180
    public function replace(array $data)
181
    {
182
        $this->array = $data;
183
        return $this;
184
    }
185
186
    /**
187
     * {@inheritDoc}
188
     */
189
    public function getIterator()
190
    {
191
        return new \ArrayIterator($this->array);
192
    }
193
194
    /**
195
     * Offset to retrieve
196
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
197
     * @param mixed $offset The offset to retrieve.
198
     *
199
     * @return mixed Can return all value types.
200
     */
201
    public function offsetGet($offset)
202
    {
203
        return isset($this->array[$offset]) ? $this->array[$offset] : null;
204
    }
205
206
    /**
207
     * Offset to set
208
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
209
     * @param mixed $offset The offset to assign the value to.
210
     * @param mixed $value The value to set.
211
     */
212
    public function offsetSet($offset, $value)
213
    {
214
        $this->array[$offset] = $value;
215
    }
216
217
    /**
218
     * Whether a offset exists
219
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
220
     *
221
     * @param  mixed $offset
222
     * @return bool
223
     */
224
    public function offsetExists($offset)
0 ignored issues
show
Coding Style introduced by
function offsetExists() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
225
    {
226
        return isset($this->array[$offset]);
227
    }
228
229
    /**
230
     * Offset to unset
231
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
232
     * @param mixed $offset The offset to unset.
233
     */
234
    public function offsetUnset($offset)
235
    {
236
        unset($this->array[$offset]);
237
    }
238
239
    /**
240
     * {@inheritDoc}
241
     */
242
    public function toArray()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
243
    {
244
        return $this->array;
245
    }
246
247
    /**
248
     * {@inheritDoc}
249
     */
250
    public function count()
251
    {
252
        return count($this->array);
253
    }
254
255
    /**
256
     * Shuffles this list (randomizes the order of the elements in). It uses the PHP function shuffle
257
     * @see http://php.net/manual/en/function.shuffle.php
258
     * @return $this
259
     */
260
    public function shuffle() {
261
        shuffle($this->array);
262
        return $this;
263
    }
264
265
    /**
266
     * returns a clone of this ArrayList, filtered by the given closure function
267
     * @param \Closure $closure
268
     * @return ArrayList
0 ignored issues
show
Documentation introduced by
Should the return type not be ArrayListTrait?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
269
     */
270
    public function filter(\Closure $closure)
271
    {
272
        return new self(array_filter($this->array, $closure));
0 ignored issues
show
Unused Code introduced by
The call to ArrayListTrait::__construct() has too many arguments starting with array_filter($this->array, $closure).

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...
273
    }
274
275
    /**
276
     * returns a clone of this ArrayList, filtered by the given array keys
277
     * @param array $keys
278
     * @return ArrayList
279
     */
280
    public function filterByKeys(array $keys)
281
    {
282
        return new ArrayList(
283
            array_filter($this->array, function($key) use ($keys) {
284
                return array_search($key, $keys) !== false;
285
            }, ARRAY_FILTER_USE_KEY)
286
        );
287
    }
288
}
289