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

Collection   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 77.78%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
eloc 11
c 2
b 0
f 0
dl 0
loc 43
ccs 14
cts 18
cp 0.7778
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A count() 0 3 1
A __construct() 0 4 2
A getItems() 0 3 1
A isEmpty() 0 3 1
A getIterator() 0 3 1
A assertType() 0 6 2
A filter() 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 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