ArrayList::lastIndexOf()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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 158
    public function stream(): Stream
14
    {
15 158
        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 1
        $this->afterElementAdd($element);
31
32 1
        return $res;
33
    }
34
35 3
    public function indexOf($element): ?int
36
    {
37 3
        return array_search($element, $this->elements, true) ?: null;
38
    }
39
40 7
    public function remove($element): bool
41
    {
42 7
        if (is_object($element) && $element instanceof HashCodeAware) {
43 2
            return $this->removeThroughHashCode($element);
44
        }
45 6
        $key = array_search($element, $this->elements, true);
46 6
        if (false === $key) {
47 3
            return false;
48
        }
49 4
        $this->removeAt($key);
50 4
        return true;
51
    }
52
53 3
    public function lastIndexOf($element): ?int
54
    {
55 3
        $reverseIndex = array_search($element, array_reverse($this->elements), true);
56 3
        if ($reverseIndex === false) {
57 1
            return null;
58
        }
59
60 2
        return count($this->elements) - $reverseIndex - 1;
61
    }
62
63 7
    public function removeAt(int $index)
64
    {
65 7
        $size = $this->size();
66 7
        if ($index >= $size) {
67 1
            return null;
68
        }
69
70 6
        $el = $this->elements[$index];
71 6
        unset($this->elements[$index]);
72 6
        $this->pointer--;
73 6
        if ($this->pointer === -1) {
74
            return $el;
75
        }
76 6
        $this->elements = array_merge(
77 6
            array_slice($this->elements, 0, $index),
78 6
            array_slice($this->elements, $index)
79
        );
80 6
        return $el;
81
    }
82
83 2
    private function removeThroughHashCode(HashCodeAware $element): bool
84
    {
85 2
        foreach ($this->elements as $i => $iElement) {
86 2
            if ($iElement instanceof HashCodeAware && $iElement->getHashCode() === $element->getHashCode()) {
87 2
                $this->removeAt($i);
88 2
                return true;
89
            }
90
        }
91 1
        return false;
92
    }
93
94 98
    protected function afterElementAdd($element): void
95
    {
96 98
    }
97
98 238
    protected function afterElementsSet(): void
99
    {
100 238
    }
101
}
102