AbstractArrayDriver   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 93.94%

Importance

Changes 0
Metric Value
wmc 16
eloc 26
dl 0
loc 73
ccs 31
cts 33
cp 0.9394
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A fetch() 0 3 1
A maybeRemoveItem() 0 9 2
A contains() 0 5 2
A arrayContains() 0 3 1
A remove() 0 5 2
A deleteObjectIfLocated() 0 11 2
A collect() 0 4 2
A arrayContainsObject() 0 9 3
1
<?php
2
3
namespace WebTheory\Collection\Access\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 187
    public function __construct(ObjectComparatorInterface $objectComparator)
13
    {
14 187
        $this->objectComparator = $objectComparator;
15
    }
16
17 30
    public function fetch(array $array, $item)
18
    {
19 30
        return $array[$item];
20
    }
21
22 187
    public function collect(array &$array, array $items): void
23
    {
24 187
        foreach ($items as $offset => $item) {
25 187
            $this->insert($array, $item, $offset);
26
        }
27
    }
28
29 6
    public function remove(array &$array, $item): bool
30
    {
31 6
        return is_object($item)
32 4
            ? $this->deleteObjectIfLocated($array, $item)
33 6
            : $this->maybeRemoveItem($array, $item);
34
    }
35
36 8
    public function contains(array $array, $item): bool
37
    {
38 8
        return is_object($item)
39 2
            ? $this->arrayContainsObject($array, $item)
40 8
            : $this->arrayContains($array, $item);
41
    }
42
43 2
    protected function maybeRemoveItem(array &$array, $item): bool
44
    {
45 2
        if (isset($array[$item])) {
46 2
            unset($array[$item]);
47
48 2
            return true;
49
        }
50
51
        return false;
52
    }
53
54 4
    protected function arrayContains(array $array, $item): bool
55
    {
56 4
        return isset($array[$item]);
57
    }
58
59 187
    protected function arrayContainsObject(array $array, object $object): bool
60
    {
61 187
        foreach ($array as $item) {
62 187
            if ($this->objectComparator->matches($item, $object)) {
63 7
                return true;
64
            }
65
        }
66
67 187
        return false;
68
    }
69
70 4
    protected function deleteObjectIfLocated(array &$array, object $object): bool
71
    {
72 4
        $position = array_search($object, $array, true);
73
74 4
        if ($position === false) {
75
            return false;
76
        }
77
78 4
        unset($array[$position]);
79
80 4
        return true;
81
    }
82
}
83