Passed
Push — master ( f69ad4...8183fa )
by Paweł
02:50
created

Collection::getItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AardsGerds\Game\Shared;
6
7
abstract class Collection implements \IteratorAggregate, \Countable
8
{
9
    protected array $items;
10
11 5
    final public function __construct(iterable $items)
12
    {
13 5
        $this->items = $items instanceof \Traversable ? iterator_to_array($items) : $items;
14 5
        $this->assertType();
15 4
    }
16
17
    public function getItems(): array
18
    {
19
        return $this->items;
20
    }
21
22 2
    public function count(): int
23
    {
24 2
        return count($this->items);
25
    }
26
27 1
    public function filter(callable $filter): static
28
    {
29 1
        return new static(array_filter($this->items, $filter));
30
    }
31
32 2
    public function getIterator(): \ArrayIterator
33
    {
34 2
        return new \ArrayIterator($this->items);
35
    }
36
37
    abstract protected function getType(): string;
38
39 5
    private function assertType(): void
40
    {
41 5
        $type = $this->getType();
42
43 5
        foreach ($this->items as $item) {
44 5
            if (!$item instanceof $type) {
45 1
                throw CollectionException::invalidType(get_class($item), $type);
46
            }
47
        }
48 4
    }
49
}
50