Passed
Push — master ( a4571b...162929 )
by Chris
07:18
created

AbstractArrayDriver::remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 2
crap 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 129
    public function __construct(ObjectComparatorInterface $objectComparator)
13
    {
14 129
        $this->objectComparator = $objectComparator;
15
    }
16
17 306
    public function insert(array &$array, object $item, $offset = null): bool
18
    {
19 306
        if ($this->arrayContainsObject($array, $item)) {
20 9
            return false;
21
        }
22
23 306
        $this->insertItem($array, $item, $offset);
24
25 306
        return true;
26
    }
27
28
    public function fetch(array $array, $item)
29
    {
30
        return $array[$item];
31
    }
32
33 9
    public function remove(array &$array, $item): bool
34
    {
35 9
        return is_object($item)
36 6
            ? $this->deleteObjectIfLocated($array, $item)
37 9
            : $this->maybeRemoveItem($array, $item);
38
    }
39
40 24
    public function contains(array $array, $item): bool
41
    {
42 24
        return is_object($item)
43 6
            ? $this->arrayContainsObject($array, $item)
44 24
            : $this->arrayContains($array, $item);
45
    }
46
47 3
    protected function maybeRemoveItem(array &$array, $item): bool
48
    {
49 3
        if (isset($array[$item])) {
50 3
            unset($array[$item]);
51
52 3
            return true;
53
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 49 is false. This is incompatible with the type-hinted return boolean. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
54
    }
55
56 12
    protected function arrayContains(array $array, $item): bool
57
    {
58 12
        return isset($array[$item]);
59
    }
60
61 306
    protected function arrayContainsObject(array $array, object $object): bool
62
    {
63 306
        foreach ($array as $item) {
64 306
            if ($this->objectComparator->matches($item, $object)) {
65 12
                return true;
66
            }
67
        }
68
69 306
        return false;
70
    }
71
72 6
    protected function deleteObjectIfLocated(array &$array, object $object): bool
73
    {
74 6
        $position = array_search($object, $array, true);
75
76 6
        if ($position === false) {
77
            return false;
78
        }
79
80 6
        unset($array[$position]);
81
82 6
        return true;
83
    }
84
85
    abstract protected function insertItem(array &$array, object $item, $offset = null): void;
86
}
87