Passed
Pull Request — master (#5)
by
unknown
02:51 queued 12s
created

ArrayListTrait::collect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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
     * @return ArrayListInterface|ArrayListTrait
30
     */
31 1
    public function clear(): ArrayListInterface
32
    {
33 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...
34 1
        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
     * {@inheritdoc}
39
     */
40 6
    public function get($key)
41
    {
42 6
        return isset($this->array[$key]) ? $this->array[$key] : null;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 2
    public function current()
49
    {
50 2
        return current($this->array);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 3
    public function next()
57
    {
58 3
        return next($this->array);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 1
    public function prev()
65
    {
66 1
        return prev($this->array);
67
    }
68
69
    /**
70
     * @param $key
71
     * @param $element
72
     * @return ArrayListInterface|ArrayListTrait
73
     */
74 1
    public function set($key, $element): ArrayListInterface
75
    {
76 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...
77 1
        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
     * @return ArrayListInterface|ArrayListTrait
83
     */
84 3
    public function append($element): ArrayListInterface
85
    {
86 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...
87 3
        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
     * @return ArrayListInterface|ArrayListTrait
94
     */
95 1
    public function add($key, $element): ArrayListInterface
96
    {
97
98 1
        if (!array_key_exists($key, $this->array)) {
99 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...
100 1
        } elseif (is_array($this->array[$key])) {
101
            $this->array[$key][] = $element;
102
        } else {
103 1
            $this->array[$key] = [$this->array[$key], $element];
104
        }
105
106 1
        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
     * @return ArrayListInterface|ArrayListTrait
112
     */
113 1
    public function remove($key): ArrayListInterface
114
    {
115 1
        unset($this->array[$key]);
116 1
        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
     * {@inheritdoc}
121
     */
122 4
    public function hasKey($key): bool
123
    {
124 4
        return array_key_exists($key, $this->array);
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 2
    public function hasElement($value): bool
131
    {
132 2
        $result = array_search($value, $this->array, true);
133 2
        return ($result !== false);
134
    }
135
136
    /**
137
     * Returns the first element
138
     * @return mixed
139
     */
140 1
    public function first()
141
    {
142 1
        reset($this->array);
143 1
        return $this->array[key($this->array)];
144
    }
145
146
    /**
147
     * Returns the last element
148
     * @return mixed
149
     */
150 1
    public function last()
151
    {
152 1
        $item = end($this->array);
153 1
        reset($this->array);
154 1
        return $item;
155
    }
156
157
    /**
158
     * {@inheritDoc}
159
     */
160 15
    public function toArray(): array
161
    {
162 15
        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
     * @return ArrayListInterface|ArrayListTrait
169
     */
170 1
    public function shuffle(): ArrayListInterface
171
    {
172 1
        shuffle($this->array);
173 1
        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
     * @return ArrayListInterface|ArrayListTrait
180
     */
181 1
    public function filter(callable $filterFunction): ArrayListInterface
182
    {
183 1
        $newInstance = new static();
184 1
        $newInstance->setArray(array_filter($this->array, $filterFunction));
185 1
        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
     * @return ArrayListInterface|ArrayListTrait
191
     */
192 29
    public function setArray(array $array): ArrayListInterface
193
    {
194 29
        return $this->replace($array);
195
    }
196
197
    /**
198
     * @param array $data
199
     * @return ArrayListInterface|ArrayListTrait
200
     */
201 31
    public function replace(array $data): ArrayListInterface
202
    {
203 31
        $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 31
        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
     * @return ArrayListInterface|ArrayListTrait
211
     */
212 1
    public function filterByKeys(array $keys): ArrayListInterface
213
    {
214 1
        $newInstance = new static();
215
        $newInstance->setArray(array_filter($this->array, function ($key) use ($keys) {
216 1
            return array_search($key, $keys) !== false;
217 1
        }, ARRAY_FILTER_USE_KEY));
218 1
        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
     * @return ArrayListInterface|ArrayListTrait
225
     */
226 4
    public function map(callable $mapFunction): ArrayListInterface
227
    {
228 4
        $newInstance = new static();
229 4
        $newInstance->setArray(array_map($mapFunction, $this->array));
230 4
        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
     * @return ArrayListInterface|ArrayListTrait
236
     */
237 1
    public function flatten(): ArrayListInterface
238
    {
239 1
        $flattenedArray = [];
240
        array_walk_recursive($this->array, function ($item) use (&$flattenedArray) {
241 1
            $flattenedArray[] = $item;
242 1
        });
243 1
        $newInstance = new static();
244 1
        $newInstance->setArray($flattenedArray);
245 1
        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
     * @param ArrayListInterface $list
251
     */
252 1
    public function merge(ArrayListInterface $list): void
253
    {
254 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...
255 1
    }
256
257
    /**
258
     * @inheritDoc
259
     * @param callable $collectFunction
260
     * @return mixed
261
     */
262 1
    public function collect(callable $collectFunction)
263
    {
264 1
        return $collectFunction($this->array);
265
    }
266
267
    /**
268
     * @inheritDoc
269
     * @param string $delimiter
270
     * @return string
271
     */
272 3
    public function collectToString(string $delimiter): string
273
    {
274
        return implode($delimiter, $this->map(function ($item) {
275 3
            if (is_scalar($item)) {
276 2
                return strval($item);
277 1
            } else if (is_object($item)) {
278 1
                if (method_exists($item, "toString")) {
279 1
                    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 3
        })->toArray());
286
    }
287
}
288
289