Completed
Push — master ( 6bd308...0b7d57 )
by Antonio Carlos
07:27
created

Collection   D

Complexity

Total Complexity 224

Size/Duplication

Total Lines 1866
Duplicated Lines 2.14 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 96.74%

Importance

Changes 0
Metric Value
dl 40
loc 1866
ccs 504
cts 521
cp 0.9674
rs 4.4102
c 0
b 0
f 0
wmc 224
lcom 2
cbo 6

119 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A make() 0 4 1
A wrap() 0 6 2
A unwrap() 0 4 2
A times() 0 12 3
A all() 0 4 1
A avg() 0 6 2
A average() 0 4 1
A median() 0 21 4
B mode() 0 24 4
A collapse() 0 4 1
A contains() 0 14 3
A containsStrict() 0 14 3
A crossJoin() 0 6 1
A dd() 0 8 1
A dump() 0 10 1
A diff() 0 4 1
A diffUsing() 0 4 1
A diffAssoc() 0 4 1
A diffAssocUsing() 0 4 1
A diffKeys() 0 4 1
A diffKeysUsing() 0 4 1
A each() 0 10 3
A eachSpread() 0 8 1
A every() 0 16 4
A except() 0 10 3
A filter() 0 8 2
A when() 0 10 3
A unless() 0 4 1
A where() 0 4 1
C operatorForWhere() 0 34 16
A whereStrict() 0 4 1
A whereIn() 8 8 1
A whereInStrict() 0 4 1
A whereNotIn() 8 8 1
A whereNotInStrict() 0 4 1
A whereInstanceOf() 0 6 1
A first() 0 4 1
A firstWhere() 0 4 1
A flatten() 0 4 1
A flip() 0 4 1
A forget() 0 8 2
A get() 0 8 2
D groupBy() 0 38 9
A keyBy() 0 18 3
A has() 0 12 4
A implode() 0 10 3
A intersect() 0 4 1
A intersectByKeys() 0 6 1
A isEmpty() 0 4 1
A isNotEmpty() 0 4 1
A useAsCallable() 0 4 2
A keys() 0 4 1
A last() 0 4 1
A pluck() 0 4 1
A map() 0 8 1
A mapSpread() 0 8 1
A mapToDictionary() 0 20 3
A mapToGroups() 0 6 1
A mapWithKeys() 0 14 3
A flatMap() 0 4 1
A mapInto() 0 6 1
A max() 12 12 3
A merge() 0 4 1
A combine() 0 4 1
A union() 0 4 1
A min() 12 12 3
A nth() 0 16 3
A only() 0 14 4
A forPage() 0 6 1
A partition() 0 14 3
A pipe() 0 4 1
A pop() 0 4 1
A prepend() 0 6 1
A push() 0 6 1
A concat() 0 10 2
A pull() 0 4 1
A put() 0 6 1
A random() 0 8 2
A reduce() 0 4 1
A reject() 0 12 2
A reverse() 0 4 1
A search() 0 14 4
A shift() 0 4 1
A shuffle() 0 16 2
A slice() 0 4 1
A split() 0 10 2
A chunk() 0 14 3
A sort() 0 10 2
B sortBy() 0 25 4
A sortByDesc() 0 4 1
A sortKeys() 0 8 2
A sortKeysDesc() 0 4 1
A splice() 0 8 2
A sum() 0 12 2
A take() 0 8 2
A tap() 0 6 1
A transform() 0 6 1
A unique() 0 14 2
A uniqueStrict() 0 4 1
A values() 0 4 1
A valueRetriever() 0 10 2
A zip() 0 12 1
A pad() 0 4 1
A toArray() 0 6 2
A jsonSerialize() 0 14 4
A toJson() 0 4 1
A getIterator() 0 4 1
A getCachingIterator() 0 4 1
A count() 0 4 1
A toBase() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 8 2
A offsetUnset() 0 4 1
A __toString() 0 4 1
B getArrayableItems() 0 18 7
A proxy() 0 4 1
A __get() 0 8 2

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 IlluminateAgnostic\Arr\Support;
4
5
use stdClass;
6
use Countable;
7
use Exception;
8
use ArrayAccess;
9
use Traversable;
10
use ArrayIterator;
11
use CachingIterator;
12
use JsonSerializable;
13
use IteratorAggregate;
14
use IlluminateAgnostic\Arr\Support\Debug\Dumper;
15
use IlluminateAgnostic\Arr\Support\Traits\Macroable;
16
use IlluminateAgnostic\Arr\Contracts\Support\Jsonable;
17
use IlluminateAgnostic\Arr\Contracts\Support\Arrayable;
18
19
class Collection implements ArrayAccess, Arrayable, Countable, IteratorAggregate, Jsonable, JsonSerializable
20
{
21
    use Macroable;
22
23
    /**
24
     * The items contained in the collection.
25
     *
26
     * @var array
27
     */
28
    protected $items = [];
29
30
    /**
31
     * The methods that can be proxied.
32
     *
33
     * @var array
34
     */
35
    protected static $proxies = [
36
        'average', 'avg', 'contains', 'each', 'every', 'filter', 'first', 'flatMap',
37
        'keyBy', 'map', 'partition', 'reject', 'sortBy', 'sortByDesc', 'sum', 'unique',
38
    ];
39
40
    /**
41
     * Create a new collection.
42
     *
43
     * @param  mixed  $items
44
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by Antonio Carlos Ribeiro
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
45
     */
46 215
    public function __construct($items = [])
47
    {
48 215
        $this->items = $this->getArrayableItems($items);
49 215
    }
50
51
    /**
52
     * Create a new collection instance if the value isn't one already.
53
     *
54
     * @param  mixed  $items
55
     * @return static
56
     */
57 10
    public static function make($items = [])
58
    {
59 10
        return new static($items);
60
    }
61
62
    /**
63
     * Wrap the given value in a collection if applicable.
64
     *
65
     * @param  mixed  $value
66
     * @return static
67
     */
68 7
    public static function wrap($value)
69
    {
70 7
        return $value instanceof self
71 2
            ? new static($value)
72 7
            : new static(Arr::wrap($value));
73
    }
74
75
    /**
76
     * Get the underlying items from the given collection if applicable.
77
     *
78
     * @param  array|static  $value
79
     * @return array
80
     */
81 3
    public static function unwrap($value)
82
    {
83 3
        return $value instanceof self ? $value->all() : $value;
84
    }
85
86
    /**
87
     * Create a new collection by invoking the callback a given amount of times.
88
     *
89
     * @param  int  $number
90
     * @param  callable  $callback
91
     * @return static
92
     */
93 1
    public static function times($number, callable $callback = null)
94
    {
95 1
        if ($number < 1) {
96 1
            return new static;
97
        }
98
99 1
        if (is_null($callback)) {
100 1
            return new static(range(1, $number));
101
        }
102
103 1
        return (new static(range(1, $number)))->map($callback);
104
    }
105
106
    /**
107
     * Get all of the items in the collection.
108
     *
109
     * @return array
110
     */
111 109
    public function all()
112
    {
113 109
        return $this->items;
114
    }
115
116
    /**
117
     * Get the average value of a given key.
118
     *
119
     * @param  callable|string|null  $callback
120
     * @return mixed
121
     */
122 4
    public function avg($callback = null)
123
    {
124 4
        if ($count = $this->count()) {
125 4
            return $this->sum($callback) / $count;
126
        }
127 1
    }
128
129
    /**
130
     * Alias for the "avg" method.
131
     *
132
     * @param  callable|string|null  $callback
133
     * @return mixed
134
     */
135 3
    public function average($callback = null)
136
    {
137 3
        return $this->avg($callback);
138
    }
139
140
    /**
141
     * Get the median of a given key.
142
     *
143
     * @param  null $key
144
     * @return mixed
145
     */
146 5
    public function median($key = null)
147
    {
148 5
        $count = $this->count();
149
150 5
        if ($count == 0) {
151 1
            return;
152
        }
153
154 4
        $values = (isset($key) ? $this->pluck($key) : $this)
155 4
                    ->sort()->values();
156
157 4
        $middle = (int) ($count / 2);
158
159 4
        if ($count % 2) {
160 1
            return $values->get($middle);
161
        }
162
163 3
        return (new static([
164 3
            $values->get($middle - 1), $values->get($middle),
165 3
        ]))->average();
166
    }
167
168
    /**
169
     * Get the mode of a given key.
170
     *
171
     * @param  mixed  $key
172
     * @return array|null
173
     */
174 4
    public function mode($key = null)
175
    {
176 4
        $count = $this->count();
177
178 4
        if ($count == 0) {
179 1
            return;
180
        }
181
182 3
        $collection = isset($key) ? $this->pluck($key) : $this;
183
184 3
        $counts = new self;
185
186 3
        $collection->each(function ($value) use ($counts) {
187 3
            $counts[$value] = isset($counts[$value]) ? $counts[$value] + 1 : 1;
188 3
        });
189
190 3
        $sorted = $counts->sort();
191
192 3
        $highestValue = $sorted->last();
193
194 3
        return $sorted->filter(function ($value) use ($highestValue) {
195 3
            return $value == $highestValue;
196 3
        })->sort()->keys()->all();
197
    }
198
199
    /**
200
     * Collapse the collection of items into a single array.
201
     *
202
     * @return static
203
     */
204 3
    public function collapse()
205
    {
206 3
        return new static(Arr::collapse($this->items));
207
    }
208
209
    /**
210
     * Determine if an item exists in the collection.
211
     *
212
     * @param  mixed  $key
213
     * @param  mixed  $operator
214
     * @param  mixed  $value
215
     * @return bool
216
     */
217 3
    public function contains($key, $operator = null, $value = null)
218
    {
219 3
        if (func_num_args() == 1) {
220 3
            if ($this->useAsCallable($key)) {
221 3
                $placeholder = new stdClass;
222
223 3
                return $this->first($key, $placeholder) !== $placeholder;
224
            }
225
226 1
            return in_array($key, $this->items);
227
        }
228
229 2
        return $this->contains($this->operatorForWhere(...func_get_args()));
230
    }
231
232
    /**
233
     * Determine if an item exists in the collection using strict comparison.
234
     *
235
     * @param  mixed  $key
236
     * @param  mixed  $value
237
     * @return bool
238
     */
239 1
    public function containsStrict($key, $value = null)
240
    {
241 1
        if (func_num_args() == 2) {
242 1
            return $this->contains(function ($item) use ($key, $value) {
243 1
                return data_get($item, $key) === $value;
244 1
            });
245
        }
246
247 1
        if ($this->useAsCallable($key)) {
248 1
            return ! is_null($this->first($key));
249
        }
250
251 1
        return in_array($key, $this->items, true);
252
    }
253
254
    /**
255
     * Cross join with the given lists, returning all possible permutations.
256
     *
257
     * @param  mixed  ...$lists
258
     * @return static
259
     */
260 1
    public function crossJoin(...$lists)
261
    {
262 1
        return new static(Arr::crossJoin(
263 1
            $this->items, ...array_map([$this, 'getArrayableItems'], $lists)
264
        ));
265
    }
266
267
    /**
268
     * Dump the collection and end the script.
269
     *
270
     * @return void
271
     */
272
    public function dd(...$args)
273
    {
274
        http_response_code(500);
275
276
        call_user_func_array([$this, 'dump'], $args);
277
278
        die(1);
279
    }
280
281
    /**
282
     * Dump the collection.
283
     *
284
     * @return $this
285
     */
286
    public function dump()
287
    {
288
        (new static(func_get_args()))
289
            ->push($this)
290
            ->each(function ($item) {
291
                (new Dumper)->dump($item);
292
            });
293
294
        return $this;
295
    }
296
297
    /**
298
     * Get the items in the collection that are not present in the given items.
299
     *
300
     * @param  mixed  $items
301
     * @return static
302
     */
303 3
    public function diff($items)
304
    {
305 3
        return new static(array_diff($this->items, $this->getArrayableItems($items)));
306
    }
307
308
    /**
309
     * Get the items in the collection that are not present in the given items.
310
     *
311
     * @param  mixed  $items
312
     * @param  callable  $callback
313
     * @return static
314
     */
315 2
    public function diffUsing($items, callable $callback)
316
    {
317 2
        return new static(array_udiff($this->items, $this->getArrayableItems($items), $callback));
318
    }
319
320
    /**
321
     * Get the items in the collection whose keys and values are not present in the given items.
322
     *
323
     * @param  mixed  $items
324
     * @return static
325
     */
326 2
    public function diffAssoc($items)
327
    {
328 2
        return new static(array_diff_assoc($this->items, $this->getArrayableItems($items)));
329
    }
330
331
    /**
332
     * Get the items in the collection whose keys and values are not present in the given items.
333
     *
334
     * @param  mixed  $items
335
     * @param  callable  $callback
336
     * @return static
337
     */
338 1
    public function diffAssocUsing($items, callable $callback)
339
    {
340 1
        return new static(array_diff_uassoc($this->items, $this->getArrayableItems($items), $callback));
341
    }
342
343
    /**
344
     * Get the items in the collection whose keys are not present in the given items.
345
     *
346
     * @param  mixed  $items
347
     * @return static
348
     */
349 2
    public function diffKeys($items)
350
    {
351 2
        return new static(array_diff_key($this->items, $this->getArrayableItems($items)));
352
    }
353
354
    /**
355
     * Get the items in the collection whose keys are not present in the given items.
356
     *
357
     * @param  mixed   $items
358
     * @param  callable  $callback
359
     * @return static
360
     */
361 1
    public function diffKeysUsing($items, callable $callback)
362
    {
363 1
        return new static(array_diff_ukey($this->items, $this->getArrayableItems($items), $callback));
364
    }
365
366
    /**
367
     * Execute a callback over each item.
368
     *
369
     * @param  callable  $callback
370
     * @return $this
371
     */
372 7
    public function each(callable $callback)
373
    {
374 7
        foreach ($this->items as $key => $item) {
375 7
            if ($callback($item, $key) === false) {
376 7
                break;
377
            }
378
        }
379
380 7
        return $this;
381
    }
382
383
    /**
384
     * Execute a callback over each nested chunk of items.
385
     *
386
     * @param  callable  $callback
387
     * @return static
388
     */
389
    public function eachSpread(callable $callback)
390
    {
391 1
        return $this->each(function ($chunk, $key) use ($callback) {
392 1
            $chunk[] = $key;
393
394 1
            return $callback(...$chunk);
395 1
        });
396
    }
397
398
    /**
399
     * Determine if all items in the collection pass the given test.
400
     *
401
     * @param  string|callable  $key
402
     * @param  mixed  $operator
403
     * @param  mixed  $value
404
     * @return bool
405
     */
406 1
    public function every($key, $operator = null, $value = null)
407
    {
408 1
        if (func_num_args() == 1) {
409 1
            $callback = $this->valueRetriever($key);
410
411 1
            foreach ($this->items as $k => $v) {
412 1
                if (! $callback($v, $k)) {
413 1
                    return false;
414
                }
415
            }
416
417 1
            return true;
418
        }
419
420 1
        return $this->every($this->operatorForWhere(...func_get_args()));
421
    }
422
423
    /**
424
     * Get all items except for those with the specified keys.
425
     *
426
     * @param  \IlluminateAgnostic\Arr\Support\Collection|mixed  $keys
427
     * @return static
428
     */
429 2
    public function except($keys)
430
    {
431 2
        if ($keys instanceof self) {
432 2
            $keys = $keys->all();
433 1
        } elseif (! is_array($keys)) {
434 1
            $keys = func_get_args();
435
        }
436
437 2
        return new static(Arr::except($this->items, $keys));
438
    }
439
440
    /**
441
     * Run a filter over each of the items.
442
     *
443
     * @param  callable|null  $callback
444
     * @return static
445
     */
446 22
    public function filter(callable $callback = null)
447
    {
448 22
        if ($callback) {
449 22
            return new static(Arr::where($this->items, $callback));
450
        }
451
452 1
        return new static(array_filter($this->items));
453
    }
454
455
    /**
456
     * Apply the callback if the value is truthy.
457
     *
458
     * @param  bool  $value
459
     * @param  callable  $callback
460
     * @param  callable  $default
461
     * @return mixed
462
     */
463 4
    public function when($value, callable $callback, callable $default = null)
464
    {
465 4
        if ($value) {
466 2
            return $callback($this, $value);
467 4
        } elseif ($default) {
468 2
            return $default($this, $value);
469
        }
470
471 2
        return $this;
472
    }
473
474
    /**
475
     * Apply the callback if the value is falsy.
476
     *
477
     * @param  bool  $value
478
     * @param  callable  $callback
479
     * @param  callable  $default
480
     * @return mixed
481
     */
482 2
    public function unless($value, callable $callback, callable $default = null)
483
    {
484 2
        return $this->when(! $value, $callback, $default);
485
    }
486
487
    /**
488
     * Filter items by the given key value pair.
489
     *
490
     * @param  string  $key
491
     * @param  mixed  $operator
492
     * @param  mixed  $value
493
     * @return static
494
     */
495 2
    public function where($key, $operator, $value = null)
496
    {
497 2
        return $this->filter($this->operatorForWhere(...func_get_args()));
498
    }
499
500
    /**
501
     * Get an operator checker callback.
502
     *
503
     * @param  string  $key
504
     * @param  string  $operator
505
     * @param  mixed  $value
506
     * @return \Closure
507
     */
508 7
    protected function operatorForWhere($key, $operator, $value = null)
509
    {
510 7
        if (func_num_args() == 2) {
511 5
            $value = $operator;
512
513 5
            $operator = '=';
514
        }
515
516 7
        return function ($item) use ($key, $operator, $value) {
517 7
            $retrieved = data_get($item, $key);
518
519 7
            $strings = array_filter([$retrieved, $value], function ($value) {
520 7
                return is_string($value) || (is_object($value) && method_exists($value, '__toString'));
521 7
            });
522
523 7
            if (count($strings) < 2 && count(array_filter([$retrieved, $value], 'is_object')) == 1) {
524 1
                return in_array($operator, ['!=', '<>', '!==']);
525
            }
526
527 7
            switch ($operator) {
528
                default:
529 7
                case '=':
530 7
                case '==':  return $retrieved == $value;
0 ignored issues
show
Coding Style introduced by Antonio Carlos Ribeiro
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by Antonio Carlos Ribeiro
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
531 5
                case '!=':
532 5
                case '<>':  return $retrieved != $value;
0 ignored issues
show
Coding Style introduced by Antonio Carlos Ribeiro
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by Antonio Carlos Ribeiro
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
533 5
                case '<':   return $retrieved < $value;
0 ignored issues
show
Coding Style introduced by Antonio Carlos Ribeiro
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by Antonio Carlos Ribeiro
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
534 5
                case '>':   return $retrieved > $value;
0 ignored issues
show
Coding Style introduced by Antonio Carlos Ribeiro
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by Antonio Carlos Ribeiro
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
535 5
                case '<=':  return $retrieved <= $value;
0 ignored issues
show
Coding Style introduced by Antonio Carlos Ribeiro
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by Antonio Carlos Ribeiro
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
536 5
                case '>=':  return $retrieved >= $value;
0 ignored issues
show
Coding Style introduced by Antonio Carlos Ribeiro
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by Antonio Carlos Ribeiro
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
537 3
                case '===': return $retrieved === $value;
0 ignored issues
show
Coding Style introduced by Antonio Carlos Ribeiro
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by Antonio Carlos Ribeiro
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
538 1
                case '!==': return $retrieved !== $value;
0 ignored issues
show
Coding Style introduced by Antonio Carlos Ribeiro
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by Antonio Carlos Ribeiro
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
539
            }
540 7
        };
541
    }
542
543
    /**
544
     * Filter items by the given key value pair using strict comparison.
545
     *
546
     * @param  string  $key
547
     * @param  mixed  $value
548
     * @return static
549
     */
550 1
    public function whereStrict($key, $value)
551
    {
552 1
        return $this->where($key, '===', $value);
553
    }
554
555
    /**
556
     * Filter items by the given key value pair.
557
     *
558
     * @param  string  $key
559
     * @param  mixed  $values
560
     * @param  bool  $strict
561
     * @return static
562
     */
563 2 View Code Duplication
    public function whereIn($key, $values, $strict = false)
0 ignored issues
show
Duplication introduced by Antonio Carlos Ribeiro
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...
564
    {
565 2
        $values = $this->getArrayableItems($values);
566
567 2
        return $this->filter(function ($item) use ($key, $values, $strict) {
568 2
            return in_array(data_get($item, $key), $values, $strict);
569 2
        });
570
    }
571
572
    /**
573
     * Filter items by the given key value pair using strict comparison.
574
     *
575
     * @param  string  $key
576
     * @param  mixed  $values
577
     * @return static
578
     */
579 1
    public function whereInStrict($key, $values)
580
    {
581 1
        return $this->whereIn($key, $values, true);
582
    }
583
584
    /**
585
     * Filter items by the given key value pair.
586
     *
587
     * @param  string  $key
588
     * @param  mixed  $values
589
     * @param  bool  $strict
590
     * @return static
591
     */
592 2 View Code Duplication
    public function whereNotIn($key, $values, $strict = false)
0 ignored issues
show
Duplication introduced by Antonio Carlos Ribeiro
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...
593
    {
594 2
        $values = $this->getArrayableItems($values);
595
596 2
        return $this->reject(function ($item) use ($key, $values, $strict) {
597 2
            return in_array(data_get($item, $key), $values, $strict);
598 2
        });
599
    }
600
601
    /**
602
     * Filter items by the given key value pair using strict comparison.
603
     *
604
     * @param  string  $key
605
     * @param  mixed  $values
606
     * @return static
607
     */
608 1
    public function whereNotInStrict($key, $values)
609
    {
610 1
        return $this->whereNotIn($key, $values, true);
611
    }
612
613
    /**
614
     * Filter the items, removing any items that don't match the given type.
615
     *
616
     * @param  string  $type
617
     * @return static
618
     */
619
    public function whereInstanceOf($type)
620
    {
621 1
        return $this->filter(function ($value) use ($type) {
622 1
            return $value instanceof $type;
623 1
        });
624
    }
625
626
    /**
627
     * Get the first item from the collection.
628
     *
629
     * @param  callable|null  $callback
630
     * @param  mixed  $default
631
     * @return mixed
632
     */
633 11
    public function first(callable $callback = null, $default = null)
634
    {
635 11
        return Arr::first($this->items, $callback, $default);
636
    }
637
638
    /**
639
     * Get the first item by the given key value pair.
640
     *
641
     * @param  string  $key
642
     * @param  mixed  $operator
643
     * @param  mixed  $value
644
     * @return static
645
     */
646 1
    public function firstWhere($key, $operator, $value = null)
647
    {
648 1
        return $this->first($this->operatorForWhere(...func_get_args()));
649
    }
650
651
    /**
652
     * Get a flattened array of the items in the collection.
653
     *
654
     * @param  int  $depth
655
     * @return static
656
     */
657 3
    public function flatten($depth = INF)
658
    {
659 3
        return new static(Arr::flatten($this->items, $depth));
660
    }
661
662
    /**
663
     * Flip the items in the collection.
664
     *
665
     * @return static
666
     */
667 1
    public function flip()
668
    {
669 1
        return new static(array_flip($this->items));
670
    }
671
672
    /**
673
     * Remove an item from the collection by key.
674
     *
675
     * @param  string|array  $keys
676
     * @return $this
677
     */
678 2
    public function forget($keys)
679
    {
680 2
        foreach ((array) $keys as $key) {
681 2
            $this->offsetUnset($key);
682
        }
683
684 2
        return $this;
685
    }
686
687
    /**
688
     * Get an item from the collection by key.
689
     *
690
     * @param  mixed  $key
691
     * @param  mixed  $default
692
     * @return mixed
693
     */
694 5
    public function get($key, $default = null)
695
    {
696 5
        if ($this->offsetExists($key)) {
697 4
            return $this->items[$key];
698
        }
699
700 1
        return value($default);
701
    }
702
703
    /**
704
     * Group an associative array by a field or using a callback.
705
     *
706
     * @param  callable|string  $groupBy
707
     * @param  bool  $preserveKeys
708
     * @return static
709
     */
710 7
    public function groupBy($groupBy, $preserveKeys = false)
711
    {
712 7
        if (is_array($groupBy)) {
713 1
            $nextGroups = $groupBy;
714
715 1
            $groupBy = array_shift($nextGroups);
716
        }
717
718 7
        $groupBy = $this->valueRetriever($groupBy);
719
720 7
        $results = [];
721
722 7
        foreach ($this->items as $key => $value) {
723 7
            $groupKeys = $groupBy($value, $key);
724
725 7
            if (! is_array($groupKeys)) {
726 5
                $groupKeys = [$groupKeys];
727
            }
728
729 7
            foreach ($groupKeys as $groupKey) {
730 7
                $groupKey = is_bool($groupKey) ? (int) $groupKey : $groupKey;
731
732 7
                if (! array_key_exists($groupKey, $results)) {
733 7
                    $results[$groupKey] = new static;
734
                }
735
736 7
                $results[$groupKey]->offsetSet($preserveKeys ? $key : null, $value);
737
            }
738
        }
739
740 7
        $result = new static($results);
741
742 7
        if (! empty($nextGroups)) {
743 1
            return $result->map->groupBy($nextGroups, $preserveKeys);
744
        }
745
746 7
        return $result;
747
    }
748
749
    /**
750
     * Key an associative array by a field or using a callback.
751
     *
752
     * @param  callable|string  $keyBy
753
     * @return static
754
     */
755 3
    public function keyBy($keyBy)
756
    {
757 3
        $keyBy = $this->valueRetriever($keyBy);
758
759 3
        $results = [];
760
761 3
        foreach ($this->items as $key => $item) {
762 3
            $resolvedKey = $keyBy($item, $key);
763
764 3
            if (is_object($resolvedKey)) {
765
                $resolvedKey = (string) $resolvedKey;
766
            }
767
768 3
            $results[$resolvedKey] = $item;
769
        }
770
771 3
        return new static($results);
772
    }
773
774
    /**
775
     * Determine if an item exists in the collection by key.
776
     *
777
     * @param  mixed  $key
778
     * @return bool
779
     */
780 2
    public function has($key)
781
    {
782 2
        $keys = is_array($key) ? $key : func_get_args();
783
784 2
        foreach ($keys as $value) {
785 2
            if (! $this->offsetExists($value)) {
786 2
                return false;
787
            }
788
        }
789
790 2
        return true;
791
    }
792
793
    /**
794
     * Concatenate values of a given key as a string.
795
     *
796
     * @param  string  $value
797
     * @param  string  $glue
798
     * @return string
799
     */
800 1
    public function implode($value, $glue = null)
801
    {
802 1
        $first = $this->first();
803
804 1
        if (is_array($first) || is_object($first)) {
805 1
            return implode($glue, $this->pluck($value)->all());
806
        }
807
808 1
        return implode($value, $this->items);
809
    }
810
811
    /**
812
     * Intersect the collection with the given items.
813
     *
814
     * @param  mixed  $items
815
     * @return static
816
     */
817 2
    public function intersect($items)
818
    {
819 2
        return new static(array_intersect($this->items, $this->getArrayableItems($items)));
820
    }
821
822
    /**
823
     * Intersect the collection with the given items by key.