Passed
Push — master ( 141597...812b31 )
by Paweł
11:43
created

Collection::makeSureNotEmpty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
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 7
    final public function __construct(iterable $items)
16
    {
17 7
        $this->items = $items instanceof \Traversable ? iterator_to_array($items) : $items;
18 7
        $this->assertType();
19 6
    }
20
21
    public function getItems(): array
22
    {
23
        return $this->items;
24
    }
25
26 2
    public function count(): int
27
    {
28 2
        return count($this->items);
29
    }
30
31 1
    public function isEmpty(): bool
32
    {
33 1
        return count($this->items) < 1;
34
    }
35
36 1
    public function makeSureNotEmpty(): void
37
    {
38 1
        if ($this->isEmpty()) {
39 1
            throw CollectionException::emptyCollection();
40
        }
41
    }
42
43 1
    public function filter(callable $filter): static
44
    {
45 1
        return new static(array_filter($this->items, $filter));
46
    }
47
48 3
    public function getIterator(): \ArrayIterator
49
    {
50 3
        return new \ArrayIterator($this->items);
51
    }
52
53
    abstract protected function getType(): string;
54
55 7
    private function assertType(): void
56
    {
57 7
        $type = $this->getType();
58
59 7
        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 6
    }
63
}
64