Passed
Push — master ( f2c7f4...294cf3 )
by Paweł
10:56
created

Collection::filter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 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 6
    final public function __construct(iterable $items)
16
    {
17 6
        $this->items = $items instanceof \Traversable ? iterator_to_array($items) : $items;
18 6
        $this->assertType();
19 5
    }
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
    public function isEmpty(): bool
32
    {
33
        return count($this->items) < 1;
34
    }
35
36 1
    public function filter(callable $filter): static
37
    {
38 1
        return new static(array_filter($this->items, $filter));
39
    }
40
41 3
    public function getIterator(): \ArrayIterator
42
    {
43 3
        return new \ArrayIterator($this->items);
44
    }
45
46
    abstract protected function getType(): string;
47
48 6
    private function assertType(): void
49
    {
50 6
        $type = $this->getType();
51
52 6
        if (!all(instance_of($type), $this->items)) {
53 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

53
            throw CollectionException::invalidType(get_class(/** @scrutinizer ignore-type */ first($this->items)), $type);
Loading history...
54
        }
55 5
    }
56
}
57