Collection   B
last analyzed

Complexity

Total Complexity 42

Size/Duplication

Total Lines 327
Duplicated Lines 14.68 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 99.12%

Importance

Changes 0
Metric Value
wmc 42
lcom 1
cbo 2
dl 48
loc 327
ccs 113
cts 114
cp 0.9912
rs 8.295
c 0
b 0
f 0

25 Methods

Rating   Name   Duplication   Size   Complexity  
B from() 18 18 5
A toArray() 0 4 1
A values() 0 4 1
A each() 0 7 2
A map() 0 4 1
A filter() 0 4 1
A select() 0 4 1
A reject() 0 10 1
A distinct() 0 4 1
A sort() 0 6 1
A slice() 0 4 1
A take() 0 4 1
A difference() 0 4 1
A intersection() 0 4 1
A merge() 0 7 1
B groupBy() 0 21 7
A concat() 0 12 2
A flatMap() 0 4 1
A mapcat() 0 4 1
A tail() 0 4 1
A head() 0 5 1
A reduce() 10 10 2
A every() 10 10 3
A some() 10 10 3
A none() 0 4 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Collection often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Collection, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Pitchart\Collection;
4
5
use Pitchart\Collection\Mixin\CallableUnifierTrait;
6
7
/**
8
 * @author Julien VITTE <[email protected]>
9
 */
10
class Collection extends \ArrayObject implements CollectionInterface, Checkable
11
{
12
13
    use CallableUnifierTrait;
14
15
    /**
16
     * @param iterable $iterable
17
     * @return static
18
     */
19 69 View Code Duplication
    public static function from($iterable)
20
    {
21 69
        if ($iterable instanceof \Iterator) {
22 11
            return new static(iterator_to_array($iterable));
23
        }
24 60
        if (is_array($iterable)
25
            || $iterable instanceof \IteratorAggregate
26 60
        ) {
27 55
            return new static($iterable);
28
        }
29
30 5
        throw new \InvalidArgumentException(
31 5
            sprintf(
32 5
                'Argument 1 must be an instance of Traversable or an array, %s given',
33 5
                is_object($iterable) ? get_class($iterable) : gettype($iterable)
34 5
            )
35 5
        );
36
    }
37
38
    /**
39
     * Alias for getArrayCopy()
40
     *
41
     * @return array
42
     * @see    getArrayCopy
43
     */
44 24
    public function toArray()
45
    {
46 24
        return $this->getArrayCopy();
47
    }
48
49
    /**
50
     * Return reindexed items
51
     *
52
     * @return array
53
     */
54 44
    public function values()
55
    {
56 44
        return array_values($this->getArrayCopy());
57
    }
58
59
    /**
60
     * Execute a callback function on each item
61
     *
62
     * @param callable $callable
63
     */
64 2
    public function each(callable $callable)
65
    {
66 2
        $function = $this->normalizeAsCallables($callable);
67 2
        foreach ($this->getArrayCopy() as $item) {
68 2
            $function($item);
69 2
        }
70 2
    }
71
72
    /**
73
     * Map a function over a collection
74
     *
75
     * @param  callable $callable
76
     * @return static
77
     */
78 14
    public function map(callable $callable)
79
    {
80 14
        return new static(array_map($this->normalizeAsCallables($callable), $this->getArrayCopy()));
81
    }
82
83
    /**
84
     * @param callable $callable
85
     * @return static
86
     */
87 11
    public function filter(callable $callable)
88
    {
89 11
        return new static(array_filter($this->getArrayCopy(), $this->normalizeAsCallables($callable)));
90
    }
91
92
    /**
93
     * Alias for filter()
94
     *
95
     * @see filter()
96
     *
97
     * @param  callable $callable
98
     * @return static
99
     */
100 3
    public function select(callable $callable)
101
    {
102 3
        return self::filter($callable);
103
    }
104
105
    /**
106
     * @param callable $callable
107
     * @return static
108
     */
109 9
    public function reject(callable $callable)
110
    {
111 9
        $function = $this->normalizeAsCallables($callable);
112 9
        return new static(array_filter(
113 9
            $this->getArrayCopy(),
114
            function ($item) use ($function) {
115 9
                return !$function($item);
116
            }
117 9
        ));
118
    }
119
120
    /**
121
     * Remove duplicate elements
122
     *
123
     * @return static
124
     */
125 1
    public function distinct()
126
    {
127 1
        return new static(array_unique($this->values()));
128
    }
129
130
    /**
131
     *
132
     * @param callable $callable
133
     * @return static
134
     */
135 2
    public function sort(callable $callable)
136
    {
137 2
        $sorted = $this->values();
138 2
        usort($sorted, $this->normalizeAsCallables($callable));
139 2
        return new static($sorted);
140
    }
141
142
    /**
143
     * @param int  $offset
144
     * @param int  $length
145
     * @param bool $preserveKeys
146
     * @return static
147
     */
148 6
    public function slice($offset, $length = null, $preserveKeys = false)
149
    {
150 6
        return new static(array_slice($this->getArrayCopy(), $offset, $length, $preserveKeys));
151
    }
152
153
    /**
154
     * @param int  $length
155
     * @param bool $preserveKeys
156
     * @return static
157
     */
158 2
    public function take($length, $preserveKeys = false)
159
    {
160 2
        return $this->slice(0, $length, $preserveKeys);
161
    }
162
163
    /**
164
     * @param Collection $collection
165
     * @return static
166
     */
167 2
    public function difference(self $collection)
168
    {
169 2
        return new static(array_values(array_diff($this->values(), $collection->values())));
170
    }
171
172
    /**
173
     * @param Collection $collection
174
     * @return static
175
     */
176 2
    public function intersection(self $collection)
177
    {
178 2
        return new static(array_values(array_intersect($this->values(), $collection->values())));
179
    }
180
181
    /**
182
     * @param Collection ...$collections
183
     * @return static
184
     */
185 9
    public function merge(...$collections)
186
    {
187
        $values = array_map(function (CollectionInterface $collection) {
188 9
            return $collection->values();
189 9
        }, $collections);
190 9
        return new static(array_merge($this->values(), ...$values));
191
    }
192
193
    /**
194
     * Group a collection using a \Closure
195
     */
196 1
    public function groupBy(callable $groupBy, $preserveKeys = false)
197
    {
198 1
        $function = $this->normalizeAsCallables($groupBy);
199 1
        $results = [];
200 1
        foreach ($this->values() as $key => $value) {
201 1
            $groupKeys = $function($value, $key);
202 1
            if (! is_array($groupKeys)) {
203 1
                $groupKeys = [$groupKeys];
204 1
            }
205 1
            foreach ($groupKeys as $groupKey) {
206 1
                if (!in_array(gettype($groupKey), ['string', 'int'])) {
207 1
                    $groupKey = (int) $groupKey;
208 1
                }
209 1
                if (! array_key_exists($groupKey, $results)) {
210 1
                    $results[$groupKey] = new static;
211 1
                }
212 1
                $results[$groupKey]->offsetSet($preserveKeys ? $key : null, $value);
213 1
            }
214 1
        }
215 1
        return new static($results);
216
    }
217
218
    /**
219
     * Concatenates collections into a single collection
220
     *
221
     * @return static
222
     */
223 4
    public function concat()
224
    {
225 4
        return $this->reduce(
226 4
            function (CollectionInterface $accumulator, Collection $item) {
227 4
                if ($item instanceof Collection) {
228 4
                    $accumulator = $accumulator->merge($item);
229 4
                }
230 4
                return $accumulator;
231 4
            },
232 4
            new static([])
233 4
        );
234
    }
235
236
    /**
237
     * Map a function over a collection and flatten the result by one-level
238
     *
239
     * @param  callable $callable
240
     * @return static
241
     */
242 3
    public function flatMap(callable $callable)
243
    {
244 3
        return $this->map($callable)->concat();
245
    }
246
247
    /**
248
     * Alias for flatMap()
249
     *
250
     * @see flatMap
251
     */
252 2
    public function mapcat(callable $callable)
253
    {
254 2
        return $this->flatMap($callable);
255
    }
256
257
    /**
258
     * Get all items but the first
259
     *
260
     * @return static
261
     */
262 1
    public function tail()
263
    {
264 1
        return new static(array_slice($this->values(), 1));
265
    }
266
267
268
    /**
269
     * Get the first item
270
     *
271
     * @return mixed
272
     */
273 1
    public function head()
274
    {
275 1
        $values = $this->values();
276 1
        return array_shift($values);
277
    }
278
279
    /**
280
     * @param callable $callable
281
     * @param mixed    $initial
282
     * @return mixed
283
     */
284 14 View Code Duplication
    public function reduce(callable $callable, $initial)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
285
    {
286 14
        $accumulator = $initial;
287 14
        $function = $this->normalizeAsCallables($callable);
288
289 14
        foreach ($this->getArrayCopy() as $item) {
290 14
            $accumulator = $function($accumulator, $item);
291 14
        }
292 14
        return $accumulator;
293
    }
294
295
    /**
296
     * Returns true if all items satisfy the callable condition
297
     *
298
     * @param callable $callable
299
     */
300 2 View Code Duplication
    public function every(callable $callable)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
301
    {
302 2
        $callable = $this->normalizeAsCallables($callable);
303 2
        foreach ($this->values() as $item) {
304 2
            if (!$callable($item)) {
305 1
                return false;
306
            }
307 2
        }
308 1
        return true;
309
    }
310
311
    /**
312
     * Returns true if at least one item satisfies the callable condition
313
     *
314
     * @param callable $callable
315
     */
316 5 View Code Duplication
    public function some(callable $callable)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
317
    {
318 5
        $callable = $this->normalizeAsCallables($callable);
319 5
        foreach ($this->values() as $item) {
320 5
            if ($callable($item)) {
321 3
                return true;
322
            }
323 3
        }
324 2
        return false;
325
    }
326
327
    /**
328
     * Returns true no item satisfies the callable condition
329
     *
330
     * @param callable $callable
331
     */
332 2
    public function none(callable $callable)
333
    {
334 2
        return !$this->some($callable);
335
    }
336
}
337