Completed
Push — master ( 2820ae...deb522 )
by Gabriel
02:22
created

FilterTrait::filter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Nip\Collections\Traits;
4
5
use Nip\Utility\Arr;
6
7
/**
8
 * Trait FilterTrait
9
 * @package Nip\Collections\Traits
10
 */
11
trait FilterTrait
12
{
13
    /**
14
     * Run a filter over each of the items.
15
     *
16
     * @param callable|null $callback
17
     * @return static
18
     */
19 1
    public function filter(callable $callback = null)
20
    {
21 1
        if ($callback) {
22 1
            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

22
            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...
23
        }
24
25 1
        return new static(array_filter($this->items));
26
    }
27
28
    /**
29
     * Get the first item from the collection passing the given truth test.
30
     *
31
     * @param callable|null $callback
32
     * @param mixed $default
33
     * @return mixed
34
     */
35 1
    public function first(callable $callback = null, $default = null)
36
    {
37 1
        return Arr::first($this->items, $callback, $default);
38
    }
39
40
    /**
41
     * Get the items with the specified keys.
42
     *
43
     * @param  mixed  $keys
44
     * @return static
45
     */
46 1
    public function only($keys)
47
    {
48 1
        if (is_null($keys)) {
49 1
            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

49
            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...
50
        }
51 1
        $keys = is_array($keys) ? $keys : func_get_args();
52 1
        return new static(Arr::only($this->items, $keys));
53
    }
54
55
    /**
56
     * Splice a portion of the underlying collection array.
57
     *
58
     * @param int $offset
59
     * @param int|null $length
60
     * @param mixed $replacement
61
     * @return static
62
     */
63 1
    public function splice($offset, $length = null, $replacement = [])
64
    {
65 1
        if (func_num_args() === 1) {
66
            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

66
            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...
67
        }
68
69 1
        return new static(array_splice($this->items, $offset, $length, $replacement));
70
    }
71
}
72