FilterTrait   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
eloc 13
c 1
b 0
f 1
dl 0
loc 71
rs 10
ccs 14
cts 15
cp 0.9333

5 Methods

Rating   Name   Duplication   Size   Complexity  
A first() 0 3 1
A only() 0 7 3
A splice() 0 7 2
A last() 0 3 1
A filter() 0 7 2
1
<?php
2
3
namespace Nip\Collections\Traits;
4
5
use Nip\Collections\CollectionInterface;
6
use Nip\Utility\Arr;
7
8
/**
9
 * Trait FilterTrait
10
 * @package Nip\Collections\Traits
11
 */
12
trait FilterTrait
13
{
14
    /**
15
     * Run a filter over each of the items.
16
     *
17
     * @param callable|null $callback
18
     * @return static
19 1
     */
20
    public function filter(callable $callback = null): CollectionInterface
21 1
    {
22 1
        if ($callback) {
23
            return new static(Arr::where($this->items, $callback));
0 ignored issues
show
Unused Code introduced by
The call to Nip\Collections\Traits\FilterTrait::__construct() has too many arguments starting with Nip\Utility\Arr::where($this->items, $callback). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
            return /** @scrutinizer ignore-call */ new static(Arr::where($this->items, $callback));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug Best Practice introduced by
The expression return new static(Nip\Ut...his->items, $callback)) returns the type Nip\Collections\Traits\FilterTrait which is incompatible with the type-hinted return Nip\Collections\CollectionInterface.
Loading history...
24
        }
25 1
26
        return new static(array_filter($this->items));
0 ignored issues
show
Bug Best Practice introduced by
The expression return new static(array_filter($this->items)) returns the type Nip\Collections\Traits\FilterTrait which is incompatible with the type-hinted return Nip\Collections\CollectionInterface.
Loading history...
27
    }
28
29
    /**
30
     * Get the first item from the collection passing the given truth test.
31
     *
32
     * @param callable|null $callback
33
     * @param mixed $default
34
     * @return mixed
35 1
     */
36
    public function first(callable $callback = null, $default = null)
37 1
    {
38
        return Arr::first($this->items, $callback, $default);
39
    }
40
41
    /**
42
     * Get the last item from the collection.
43
     *
44
     * @param  callable|null  $callback
45
     * @param  mixed  $default
46 1
     * @return mixed
47
     */
48 1
    public function last(callable $callback = null, $default = null)
49 1
    {
50
        return Arr::last($this->items, $callback, $default);
51 1
    }
52 1
53
    /**
54
     * Get the items with the specified keys.
55
     *
56
     * @param  mixed  $keys
57
     * @return static
58
     */
59
    public function only($keys)
60
    {
61
        if (is_null($keys)) {
62
            return new static($this->items);
0 ignored issues
show
Unused Code introduced by
The call to Nip\Collections\Traits\FilterTrait::__construct() has too many arguments starting with $this->items. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
            return /** @scrutinizer ignore-call */ new static($this->items);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
63 1
        }
64
        $keys = is_array($keys) ? $keys : func_get_args();
65 1
        return new static(Arr::only($this->items, $keys));
66
    }
67
68
    /**
69 1
     * Splice a portion of the underlying collection array.
70
     *
71
     * @param int $offset
72
     * @param int|null $length
73
     * @param mixed $replacement
74
     * @return static
75
     */
76
    public function splice($offset, $length = null, $replacement = [])
77
    {
78
        if (func_num_args() === 1) {
79
            return new static(array_splice($this->items, $offset));
0 ignored issues
show
Unused Code introduced by
The call to Nip\Collections\Traits\FilterTrait::__construct() has too many arguments starting with array_splice($this->items, $offset). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
            return /** @scrutinizer ignore-call */ new static(array_splice($this->items, $offset));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
80
        }
81
82
        return new static(array_splice($this->items, $offset, $length, $replacement));
83
    }
84
}
85