Completed
Push — master ( 197ad6...daa8c5 )
by ARCANEDEV
10s
created

Collection::unique()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 15
ccs 1
cts 1
cp 1
crap 3
rs 9.4285
1
<?php namespace Arcanedev\Support;
2
3
use Illuminate\Support\Collection as IlluminateCollection;
4
5
/**
6
 * Class     Collection
7
 *
8
 * @package  Arcanedev\Support
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class Collection extends IlluminateCollection
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Custom Functions
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /**
18
     * Return only unique items from the collection array.
19
     *
20
     * @param  string|callable|null  $key
21
     * @param  bool                  $strict
22 8
     *
23
     * @return static
24 8
     */
25
    public function unique($key = null, $strict = false)
26 8
    {
27
        if (is_null($key))
28
            return new static(array_unique($this->items, SORT_REGULAR));
29
30
        $key    = $this->valueRetriever($key);
0 ignored issues
show
Documentation introduced by
$key is of type callable, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
31
        $exists = [];
32
33
        return $this->reject(function ($item) use ($key, $strict, &$exists) {
34
            if (in_array($id = $key($item), $exists, $strict)) {
35
                return true;
36
            }
37
            $exists[] = $id;
38
        });
39
    }
40
41
    /**
42
     * Return only unique items from the collection array using strict comparison.
43
     *
44
     * @param  string|callable|null  $key
45
     *
46
     * @return static
47
     */
48
    public function uniqueStrict($key = null)
49
    {
50
        return $this->unique($key, true);
51
    }
52
53
    /**
54
     * Reset the collection.
55
     *
56
     * @return static
57
     */
58
    public function reset()
59
    {
60
        $this->items = [];
61
62
        return $this;
63
    }
64
}
65