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

AbstractArrayDriver   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 84.62%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 22
c 1
b 0
f 0
dl 0
loc 58
ccs 22
cts 26
cp 0.8462
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A remove() 0 13 3
A arrayContainsObject() 0 9 3
A retrieve() 0 3 1
A __construct() 0 3 1
A contains() 0 5 2
A deleteObjectIfLocated() 0 11 2
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