Passed
Pull Request — master (#18)
by Maxim
03:40
created

ArrayList::removeAt()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.004

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 18
ccs 12
cts 13
cp 0.9231
rs 9.8666
cc 3
nc 3
nop 1
crap 3.004
1
<?php
2
/**
3
 * @author Maxim Sokolovsky
4
 */
5
6
namespace WS\Utils\Collections;
7
8
use OutOfRangeException;
9
10
class ArrayList extends AbstractCollection implements ListSequence
11
{
12
13 132
    public function stream(): Stream
14
    {
15 132
        return new SerialStream($this);
16
    }
17
18 5
    public function get(int $index)
19
    {
20 5
        return $this->elements[$index];
21
    }
22
23 2
    public function set($element, int $index)
24
    {
25 2
        if (!isset($this->elements[$index])) {
26 1
            throw new OutOfRangeException("Index $index is out of list range with size: {$this->size()} ");
27
        }
28 1
        $res = $this->elements[$index];
29 1
        $this->elements[$index] = $element;
30
31 1
        return $res;
32
    }
33
34 3
    public function indexOf($element): ?int
35
    {
36 3
        return array_search($element, $this->elements, true) ?: null;
37
    }
38
39 7
    public function remove($element): bool
40
    {
41 7
        if (is_object($element) && $element instanceof HashCodeAware) {
42 2
            return $this->removeThroughHashCode($element);
43
        }
44 6
        $key = array_search($element, $this->elements, true);
45 6
        if (false === $key) {
46 3
            return false;
47
        }
48 4
        $this->removeAt($key);
49 4
        return true;
50
    }
51
52 3
    public function lastIndexOf($element): ?int
53
    {
54 3
        $reverseIndex = array_search($element, array_reverse($this->elements), true);
55 3
        if ($reverseIndex === false) {
56 1
            return null;
57
        }
58
59 2
        return count($this->elements) - $reverseIndex - 1;
60
    }
61
62 7
    public function removeAt(int $index)
63
    {
64 7
        $size = $this->size();
65 7
        if ($index >= $size) {
66 1
            return null;
67
        }
68
69 6
        $el = $this->elements[$index];
70 6
        unset($this->elements[$index]);
71 6
        $this->pointer--;
72 6
        if ($this->pointer === -1) {
73
            return $el;
74
        }
75 6
        $this->elements = array_merge(
76 6
            array_slice($this->elements, 0, $index),
77 6
            array_slice($this->elements, $index)
78
        );
79 6
        return $el;
80
    }
81
82 2
    private function removeThroughHashCode(HashCodeAware $element): bool
83
    {
84 2
        foreach ($this->elements as $i => $iElement) {
85 2
            if ($iElement instanceof HashCodeAware && $iElement->getHashCode() === $element->getHashCode()) {
86 2
                $this->removeAt($i);
87 2
                return true;
88
            }
89
        }
90 1
        return false;
91
    }
92
}
93