Passed
Push — master ( b556a2...2820ae )
by Gabriel
01:39
created

FilterTrait::first()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
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
     * Get the first item from the collection passing the given truth test.
15
     *
16
     * @param  callable|null  $callback
17
     * @param  mixed  $default
18
     * @return mixed
19
     */
20 1
    public function first(callable $callback = null, $default = null)
21
    {
22 1
        return Arr::first($this->items, $callback, $default);
23
    }
24
25
    /**
26
     * Get the items with the specified keys.
27
     *
28
     * @param  mixed  $keys
29
     * @return static
30
     */
31 1
    public function only($keys)
32
    {
33 1
        if (is_null($keys)) {
34 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

34
            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...
35
        }
36 1
        $keys = is_array($keys) ? $keys : func_get_args();
37 1
        return new static(Arr::only($this->items, $keys));
38
    }
39
40
    /**
41
     * Splice a portion of the underlying collection array.
42
     *
43
     * @param int $offset
44
     * @param int|null $length
45
     * @param mixed $replacement
46
     * @return static
47
     */
48 1
    public function splice($offset, $length = null, $replacement = [])
49
    {
50 1
        if (func_num_args() === 1) {
51
            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

51
            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...
52
        }
53
54 1
        return new static(array_splice($this->items, $offset, $length, $replacement));
55
    }
56
}
57