Test Failed
Pull Request — master (#5)
by
unknown
03:10
created

ArrayListTrait::first()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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