Passed
Push — master ( 812b31...82efe3 )
by Paweł
02:52
created

Collection   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
eloc 13
c 2
b 0
f 0
dl 0
loc 50
ccs 22
cts 22
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A count() 0 3 1
A __construct() 0 4 2
A isEmpty() 0 3 1
A getIterator() 0 3 1
A assertType() 0 6 2
A makeSureNotEmpty() 0 4 2
A filter() 0 3 1
A getItems() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AardsGerds\Game\Shared;
6
7
use function Lambdish\Phunctional\first;
8
use function Lambdish\Phunctional\all;
9
use function Lambdish\Phunctional\instance_of;
10
11
abstract class Collection implements \IteratorAggregate, \Countable
12
{
13
    protected array $items;
14
15 14
    final public function __construct(iterable $items)
16
    {
17 14
        $this->items = $items instanceof \Traversable ? iterator_to_array($items) : $items;
18 14
        $this->assertType();
19 13
    }
20
21 3
    public function getItems(): array
22
    {
23 3
        return $this->items;
24
    }
25
26 5
    public function count(): int
27
    {
28 5
        return count($this->items);
29
    }
30
31 4
    public function isEmpty(): bool
32
    {
33 4
        return count($this->items) < 1;
34
    }
35
36 4
    public function makeSureNotEmpty(): void
37
    {
38 4
        if ($this->isEmpty()) {
39 1
            throw CollectionException::emptyCollection();
40
        }
41 3
    }
42
43 8
    public function filter(callable $filter): static
44
    {
45 8
        return new static(array_filter($this->items, $filter));
46
    }
47
48 10
    public function getIterator(): \ArrayIterator
49
    {
50 10
        return new \ArrayIterator($this->items);
51
    }
52
53
    abstract protected function getType(): string;
54
55 14
    private function assertType(): void
56
    {
57 14
        $type = $this->getType();
58
59 14
        if (!all(instance_of($type), $this->items)) {
60 1
            throw CollectionException::invalidType(get_class(first($this->items)), $type);
0 ignored issues
show
Bug introduced by
It seems like first($this->items) can also be of type null; however, parameter $object of get_class() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
            throw CollectionException::invalidType(get_class(/** @scrutinizer ignore-type */ first($this->items)), $type);
Loading history...
61
        }
62 13
    }
63
}
64