Passed
Pull Request — master (#25)
by
unknown
02:23
created

AbstractCollection   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 96%

Importance

Changes 0
Metric Value
wmc 20
eloc 35
c 0
b 0
f 0
dl 0
loc 106
ccs 48
cts 50
cp 0.96
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A of() 0 3 2
A toArray() 0 3 1
A getElements() 0 3 1
A merge() 0 3 1
A size() 0 3 1
A setElements() 0 5 1
A addAll() 0 13 3
A add() 0 9 2
A equals() 0 3 1
A clear() 0 3 1
A contains() 0 3 1
A isEmpty() 0 3 1
A copy() 0 3 1
A getIterator() 0 3 1
1
<?php
2
/**
3
 * @author Maxim Sokolovsky
4
 */
5
6
namespace WS\Utils\Collections;
7
8
abstract class AbstractCollection implements Collection
9
{
10
    protected $elements = [];
11
    protected $pointer = -1;
12
13 251
    public function __construct(?array $elements = null)
14
    {
15 251
        if ($elements === null) {
16 96
            return;
17
        }
18 222
        $this->setElements($elements);
19 214
    }
20
21 114
    public static function of(...$elements): self
22
    {
23 114
        return new static($elements ?: null);
24
    }
25
26 80
    public function add($element): bool
27
    {
28 80
        if ($this->pointer === PHP_INT_MAX) {
29
            return false;
30
        }
31 80
        $this->pointer++;
32 80
        $this->elements[] = $element;
33 80
        $this->afterElementAdd($element);
34 79
        return true;
35
    }
36
37 28
    public function addAll(iterable $elements): bool
38
    {
39 28
        foreach ($elements as $element) {
40 23
            $this->elements[] = $element;
41 23
            $this->afterElementAdd($element);
42
        }
43 28
        $newPointer = count($this->elements) - 1;
44 28
        if ($newPointer > PHP_INT_MAX) {
45
            $this->elements = array_slice($this->elements, 0, $this->pointer);
46
        } else {
47 28
            $this->pointer = $newPointer;
48
        }
49 28
        return true;
50
    }
51
52 6
    public function merge(Collection $collection): bool
53
    {
54 6
        return $this->addAll($collection->toArray());
55
    }
56
57 8
    public function clear(): void
58
    {
59 8
        $this->setElements([]);
60 8
    }
61
62 10
    public function contains($element): bool
63
    {
64 10
        return in_array($element, $this->elements, true);
65
    }
66
67 46
    public function equals(Collection $collection): bool
68
    {
69 46
        return $this->toArray() === $collection->toArray();
70
    }
71
72 74
    public function size(): int
73
    {
74 74
        return $this->pointer + 1;
75
    }
76
77 27
    public function isEmpty(): bool
78
    {
79 27
        return $this->pointer === -1;
80
    }
81
82 171
    public function toArray(): array
83
    {
84 171
        return $this->elements;
85
    }
86
87 76
    public function getIterator()
88
    {
89 76
        yield from $this->toArray();
90 62
    }
91
92 136
    public function copy(): Collection
93
    {
94 136
        return clone $this;
95
    }
96
97 222
    protected function setElements(array $elements): void
98
    {
99 222
        $this->elements = array_values($elements);
100 222
        $this->pointer = count($elements) - 1;
101 222
        $this->afterElementsSet();
102 214
    }
103
104 10
    protected function getElements(): array
105
    {
106 10
        return $this->elements;
107
    }
108
109
    abstract protected function afterElementAdd($element): void;
110
111
    abstract protected function afterElementsSet(): void;
112
113
    abstract public function stream(): Stream;
114
}
115