Passed
Push — master ( 9a224a...3e0fbd )
by Sebastian
02:02
created

ArrayListTrait::shuffle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * Copyright (C) 2018 Sebastian Böttger <[email protected]>
4
 * You may use, distribute and modify this code under the
5
 * terms of the MIT license.
6
 *
7
 * You should have received a copy of the MIT license with
8
 * this file. If not, please visit: https://opensource.org/licenses/mit-license.php
9
 */
10
11
namespace Seboettg\Collection\ArrayList;
12
13
use closure;
14
15
/**
16
 * Trait ArrayListTrait
17
 * @package Seboettg\Collection
18
 * @author Sebastian Böttger <[email protected]>
19
 * @property $array Base array of this data structure
20
 */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $array at position 0 could not be parsed: Unknown type name '$array' at position 0 in $array.
Loading history...
21
trait ArrayListTrait
22
{
23
    use ArrayAccessTrait;
24
25
    /**
26
     * flush array list
27
     *
28
     * @return $this
29
     */
30 1
    public function clear()
31
    {
32 1
        $this->array = [];
0 ignored issues
show
Bug Best Practice introduced by
The property array does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
33 1
        return $this;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 6
    public function get($key)
40
    {
41 6
        return isset($this->array[$key]) ? $this->array[$key] : null;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 2
    public function current()
48
    {
49 2
        return current($this->array);
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 3
    public function next()
56
    {
57 3
        return next($this->array);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 1
    public function prev()
64
    {
65 1
        return prev($this->array);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 1
    public function set($key, $element)
72
    {
73 1
        $this->array[$key] = $element;
0 ignored issues
show
Bug Best Practice introduced by
The property array does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
74 1
        return $this;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 3
    public function append($element)
81
    {
82 3
        $this->array[] = $element;
0 ignored issues
show
Bug Best Practice introduced by
The property array does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
83 3
        return $this;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 1
    public function add($key, $element)
90
    {
91
92 1
        if (!array_key_exists($key, $this->array)) {
93 1
            $this->array[$key] = $element;
0 ignored issues
show
Bug Best Practice introduced by
The property array does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
94 1
        } elseif (is_array($this->array[$key])) {
95
            $this->array[$key][] = $element;
96
        } else {
97 1
            $this->array[$key] = [$this->array[$key], $element];
98
        }
99
100 1
        return $this;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 1
    public function remove($key)
107
    {
108 1
        unset($this->array[$key]);
109 1
        return $this;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 4
    public function hasKey($key)
116
    {
117 4
        return array_key_exists($key, $this->array);
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 2
    public function hasElement($value)
124
    {
125 2
        $result = array_search($value, $this->array, true);
126 2
        return ($result !== false);
127
    }
128
129
    /**
130
     * Returns the first element
131
     * @return mixed
132
     */
133 1
    public function first()
134
    {
135 1
        reset($this->array);
136 1
        return $this->array[key($this->array)];
137
    }
138
139
    /**
140
     * Returns the last element
141
     * @return mixed
142
     */
143 1
    public function last()
144
    {
145 1
        $item = end($this->array);
146 1
        reset($this->array);
147 1
        return $item;
148
    }
149
150
    /**
151
     * {@inheritDoc}
152
     */
153 12
    public function toArray()
154
    {
155 12
        return $this->array;
156
    }
157
158
    /**
159
     * Shuffles this list (randomizes the order of the elements in). It uses the PHP function shuffle
160
     * @see http://php.net/manual/en/function.shuffle.php
161
     * @return ArrayListInterface|ArrayListTrait
162
     */
163 1
    public function shuffle()
164
    {
165 1
        shuffle($this->array);
166 1
        return $this;
167
    }
168
169
    /**
170
     * returns a clone of this ArrayList, filtered by the given closure function
171
     * @param closure $closure
172
     * @return ArrayListInterface|ArrayListTrait
173
     */
174 1
    public function filter(closure $closure)
175
    {
176 1
        $newInstance = new self();
177 1
        $newInstance->setArray(array_filter($this->array, $closure));
178 1
        return $newInstance;
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184 25
    public function setArray(array $array)
185
    {
186 25
        return $this->replace($array);
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     */
192 27
    public function replace(array $data)
193
    {
194 27
        $this->array = $data;
0 ignored issues
show
Bug Best Practice introduced by
The property array does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
195 27
        return $this;
196
    }
197
198
    /**
199
     * returns a clone of this ArrayList, filtered by the given array keys
200
     * @param array $keys
201
     * @return ArrayListInterface|ArrayListTrait
202
     */
203 1
    public function filterByKeys(array $keys)
204
    {
205 1
        $newInstance = new self();
206 1
        $newInstance->setArray(array_filter($this->array, function ($key) use ($keys) {
207 1
            return array_search($key, $keys) !== false;
208 1
        }, ARRAY_FILTER_USE_KEY));
209 1
        return $newInstance;
210
    }
211
212
    /**
213
     * returns a new ArrayList containing all the elements of this ArrayList after applying the callback function to each one.
214
     * @param closure $mapFunction
215
     * @return ArrayListInterface|ArrayListTrait
216
     */
217 1
    public function map(closure $mapFunction)
218
    {
219 1
        $newInstance = new self();
220 1
        $newInstance->setArray(array_map($mapFunction, $this->array));
221 1
        return $newInstance;
222
    }
223
224
    /**
225
     * Returns a new ArrayList containing an one-dimensional array of all elements of this ArrayList. Keys are going lost.
226
     * @return ArrayListInterface|ArrayListTrait
227
     */
228 1
    public function flatten()
229
    {
230 1
        $flattenedArray = [];
231 1
        array_walk_recursive($this->array, function ($item) use (&$flattenedArray) {
232 1
            $flattenedArray[] = $item;
233 1
        });
234 1
        $newInstance = new self();
235 1
        $newInstance->setArray($flattenedArray);
236 1
        return $newInstance;
237
    }
238
239
    /**
240
     * Merges the elements of the passed list together with this list so that the values of the passed list are appended
241
     * to the end of the this list
242
     * @param ArrayListInterface $list
243
     * @return void
244
     */
245 1
    public function merge(ArrayListInterface $list)
246
    {
247 1
        $this->array = array_merge($this->array, $list->toArray());
0 ignored issues
show
Bug Best Practice introduced by
The property array does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
248 1
    }
249
}
250