Passed
Pull Request — master (#24)
by Igor
02:46
created

ArrayList::remove()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 10
cc 4
nc 3
nop 1
crap 4
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 10
    public function parallelStream($workersPool = null): Stream
19
    {
20 10
        return new ParallelStream($this, $workersPool);
21
    }
22
23 5
    public function get(int $index)
24
    {
25 5
        return $this->elements[$index];
26
    }
27
28 2
    public function set($element, int $index)
29
    {
30 2
        if (!isset($this->elements[$index])) {
31 1
            throw new OutOfRangeException("Index $index is out of list range with size: {$this->size()} ");
32
        }
33 1
        $res = $this->elements[$index];
34 1
        $this->elements[$index] = $element;
35
36 1
        return $res;
37
    }
38
39 3
    public function indexOf($element): ?int
40
    {
41 3
        return array_search($element, $this->elements, true) ?: null;
42
    }
43
44 7
    public function remove($element): bool
45
    {
46 7
        if (is_object($element) && $element instanceof HashCodeAware) {
47 2
            return $this->removeThroughHashCode($element);
48
        }
49 6
        $key = array_search($element, $this->elements, true);
50 6
        if (false === $key) {
51 3
            return false;
52
        }
53 4
        $this->removeAt($key);
54 4
        return true;
55
    }
56
57 3
    public function lastIndexOf($element): ?int
58
    {
59 3
        $reverseIndex = array_search($element, array_reverse($this->elements), true);
60 3
        if ($reverseIndex === false) {
61 1
            return null;
62
        }
63
64 2
        return count($this->elements) - $reverseIndex - 1;
65
    }
66
67 7
    public function removeAt(int $index)
68
    {
69 7
        $size = $this->size();
70 7
        if ($index >= $size) {
71 1
            return null;
72
        }
73
74 6
        $el = $this->elements[$index];
75 6
        unset($this->elements[$index]);
76 6
        $this->pointer--;
77 6
        if ($this->pointer === -1) {
78
            return $el;
79
        }
80 6
        $this->elements = array_merge(
81 6
            array_slice($this->elements, 0, $index),
82 6
            array_slice($this->elements, $index)
83
        );
84 6
        return $el;
85
    }
86
87 2
    private function removeThroughHashCode(HashCodeAware $element): bool
88
    {
89 2
        foreach ($this->elements as $i => $iElement) {
90 2
            if ($iElement instanceof HashCodeAware && $iElement->getHashCode() === $element->getHashCode()) {
91 2
                $this->removeAt($i);
92 2
                return true;
93
            }
94
        }
95 1
        return false;
96
    }
97
}
98