SearchableTrait::contains()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * File SearchableTrait.php
4
 *
5
 * @author Edward Pfremmer <[email protected]>
6
 */
7
namespace Epfremme\Collection\Traits;
8
9
use Closure;
10
11
/**
12
 * Trait SearchableTrait
13
 *
14
 * @property array|mixed[] $elements
15
 * @package Epfremme\Collection\Traits
16
 */
17
trait SearchableTrait
18
{
19
    /**
20
     * Return index of element
21
     *
22
     * @param mixed $element
23
     * @return mixed
24
     */
25 2
    public function indexOf($element)
26
    {
27 2
        return array_search($element, $this->elements, true);
28
    }
29
30
    /**
31
     * Test if element in collection
32
     *
33
     * @param mixed $element
34
     * @return bool
35
     */
36 7
    public function contains($element)
37
    {
38 7
        return in_array($element, $this->elements, true);
39
    }
40
41
    /**
42
     * Test if key exists in collection
43
     *
44
     * @param mixed $key
45
     * @return bool
46
     */
47 2
    public function keyExists($key)
48
    {
49 2
        return array_key_exists($key, $this->elements);
50
    }
51
52
    /**
53
     * Return new collection with elements filtered by function
54
     *
55
     * @param Closure $fn
56
     * @return static
57
     */
58 1
    public function filter(Closure $fn)
59
    {
60 1
        return new static(array_filter($this->elements, $fn, ARRAY_FILTER_USE_BOTH));
0 ignored issues
show
Unused Code introduced by
The call to SearchableTrait::__construct() has too many arguments starting with array_filter($this->elem... ARRAY_FILTER_USE_BOTH).

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
61
    }
62
}
63