ArrayInspector::exists()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CCT\Component\Collections\Interactors;
6
7
class ArrayInspector extends AbstractInteractor
8
{
9
    /**
10
     * Gets the index/key of a given element. The comparison of two elements is strict,
11
     * that means not only the value but also the type must match.
12
     * For objects this means reference equality.
13
     *
14
     * @param mixed $element The element to search for.
15
     *
16
     * @return int|string|bool The key/index of the element or FALSE if the element was not found.
17
     */
18 4
    public function indexOf($element)
19
    {
20 4
        return array_search($element, $this->elements, true);
21
    }
22
23
    /**
24
     * Tests for the existence of an element that satisfies the given predicate.
25
     *
26
     * @param \Closure $p The predicate.
27
     *
28
     * @return bool TRUE if the predicate is TRUE for at least one element, FALSE otherwise.
29
     */
30 1
    public function exists(\Closure $p): bool
31
    {
32 1
        foreach ($this->elements as $key => $element) {
33 1
            if ($p($key, $element)) {
34 1
                return true;
35
            }
36
        }
37
38 1
        return false;
39
    }
40
}
41