Passed
Push — master ( afdec1...60a2b8 )
by Chris
07:28
created

AbstractArrayDriver::deleteObjectIfLocated()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
rs 10
cc 2
nc 2
nop 2
crap 2.0185
1
<?php
2
3
namespace WebTheory\Collection\Driver\Abstracts;
4
5
use WebTheory\Collection\Contracts\ArrayDriverInterface;
6
use WebTheory\Collection\Contracts\ObjectComparatorInterface;
7
8
abstract class AbstractArrayDriver implements ArrayDriverInterface
9
{
10
    protected ObjectComparatorInterface $objectComparator;
11
12 219
    public function __construct(ObjectComparatorInterface $objectComparator)
13
    {
14 219
        $this->objectComparator = $objectComparator;
15
    }
16
17
    public function retrieve(array $array, $item)
18
    {
19
        return $array[$item];
20
    }
21
22 6
    public function remove(array &$array, $item): bool
23
    {
24 6
        if (is_object($item)) {
25 3
            return $this->deleteObjectIfLocated($array, $item);
26
        }
27
28 3
        if (isset($array[$item])) {
29 3
            unset($array[$item]);
30
31 3
            return true;
32
        }
33
34
        return false;
35
    }
36
37 12
    public function contains(array $array, $item): bool
38
    {
39 12
        return is_object($item)
40 6
            ? in_array($item, $array, true)
41 12
            : isset($array[$item]);
42
    }
43
44 219
    protected function arrayContainsObject(array $array, object $object): bool
45
    {
46 219
        foreach ($array as $item) {
47 219
            if ($this->objectComparator->matches($item, $object)) {
48 14
                return true;
49
            }
50
        }
51
52 219
        return false;
53
    }
54
55 6
    protected function deleteObjectIfLocated(array &$array, object $object): bool
56
    {
57 6
        $position = array_search($object, $array, true);
58
59 6
        if ($position === false) {
60
            return false;
61
        }
62
63 6
        unset($array[$position]);
64
65 6
        return true;
66
    }
67
}
68