Passed
Push — master ( edd249...4fdc6b )
by Thorsten
01:36
created

TypedMapTrait::assertItemType()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 13
cts 13
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 12
nc 6
nop 1
crap 5
1
<?php
2
declare(strict_types=1);
3
4
namespace Daikon\DataStructure;
5
6
use Ds\Map;
7
use InvalidArgumentException;
8
use Iterator;
9
10
trait TypedMapTrait
11
{
12
    /**
13
     * @var Map internal map to store items
14
     */
15
    private $compositeMap;
16
17
    /**
18
     * @var string[] fully qualified class name of acceptable types
19
     */
20
    private $itemFqcns;
21
22 2
    public function has(string $key): bool
23
    {
24 2
        return $this->compositeMap->hasKey($key);
25
    }
26
27
    /**
28
     * @throws \OutOfBoundsException
29
     */
30 6
    public function get(string $key)
31
    {
32 6
        return $this->compositeMap->get($key);
33
    }
34
35
    /**
36
     * @throws InvalidArgumentException
37
     */
38 2
    public function set($key, $item): self
39
    {
40 2
        $this->assertItemType($item);
41 1
        $copy = clone $this;
42 1
        $copy->compositeMap->put($key, $item);
43 1
        return $copy;
44
    }
45
46 3
    public function count(): int
47
    {
48 3
        return count($this->compositeMap);
49
    }
50
51 1
    public function toArray(): array
52
    {
53 1
        return $this->compositeMap->toArray();
54
    }
55
56 1
    public function isEmpty(): bool
57
    {
58 1
        return $this->compositeMap->isEmpty();
59
    }
60
61 1
    public function getIterator(): Iterator
62
    {
63 1
        return $this->compositeMap->getIterator();
64
    }
65
66 1
    public function getItemFqcn(): array
67
    {
68 1
        return $this->itemFqcns;
69
    }
70
71
    /**
72
     * @throws \OutOfBoundsException
73
     */
74 3
    public function __get(string $key)
75
    {
76 3
        return $this->get($key);
77
    }
78
79 18
    private function init(iterable $items, $itemFqcns): void
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
80
    {
81 18
        $this->itemFqcns = (array)$itemFqcns;
82 18
        foreach ($items as $key => $item) {
83 16
            $this->assertItemKey($key);
84 14
            $this->assertItemType($item);
85
        }
86 16
        $this->compositeMap = new Map($items);
87 16
    }
88
89 16 View Code Duplication
    private function assertItemKey($key): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91 16
        if (!is_string($key)) {
92 2
            throw new InvalidArgumentException(sprintf(
93 2
                'Invalid item key given to %s. Expected string but was given %s.',
94 2
                static::CLASS,
95 2
                is_object($key) ? get_class($key) : @gettype($key)
96
            ));
97
        }
98 14
    }
99
100 14
    private function assertItemType($item): void
101
    {
102 14
        $itemIsValid = false;
103 14
        foreach ($this->itemFqcns as $fqcn) {
104 14
            if (is_a($item, $fqcn)) {
105 14
                $itemIsValid = true;
106 14
                break;
107
            }
108
        }
109 14
        if (!$itemIsValid) {
110 1
            throw new InvalidArgumentException(sprintf(
111 1
                'Invalid item type given to %s. Expected one of %s but was given %s.',
112 1
                static::class,
113 1
                implode(', ', $this->itemFqcns),
114 1
                is_object($item) ? get_class($item) : @gettype($item)
115
            ));
116
        }
117 14
    }
118
119 1
    public function __clone()
120
    {
121 1
        $this->compositeMap = new Map($this->compositeMap->toArray());
122 1
    }
123
}
124