Passed
Push — master ( dd9eb5...695493 )
by Thorsten
01:34
created

TypedMapTrait::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Daikon\DataStructures;
4
5
use Ds\Map;
6
7
trait TypedMapTrait
8
{
9
    private $compositeMap;
10
11
    private $itemFqcn;
12
13
    public function has(string $key): bool
14
    {
15
        return $this->compositeMap->hasKey($key);
16
    }
17
18
    public function get(string $key)
19
    {
20
        return $this->compositeMap->get($key);
21
    }
22
    
23
    public function set($key, $item): self
24
    {
25
        $this->assertItemType($item);
26
        $copy = clone $this;
27
        $copy->compositeMap->put($key, $item);
28
        return $copy;
29
    }
30
31
    public function count(): int
32
    {
33
        return count($this->compositeMap);
34
    }
35
36
    public function toArray(): array
37
    {
38
        return $this->compositeMap->toArray();
39
    }
40
41
    public function isEmpty(): bool
42
    {
43
        return $this->compositeMap->isEmpty();
44
    }
45
46
    public function getIterator(): \Iterator
47
    {
48
        return $this->compositeMap->getIterator();
49
    }
50
51
    public function getItemFqcn()
52
    {
53
        return $this->itemFqcn;
54
    }
55
56
    public function __get(string $key)
57
    {
58
        return $this->get($key);
59
    }
60
61
    private function init(array $items, string $itemFqcn)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
62
    {
63
        $this->itemFqcn = $itemFqcn;
64
        foreach ($items as $key => $item) {
65
            $this->assertItemKey($key);
66
            $this->assertItemType($item);
67
        }
68
        $this->compositeMap = new Map($items);
69
    }
70
71 View Code Duplication
    private function assertItemKey($key)
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...
72
    {
73
        if (!is_string($key)) {
74
            throw new \Exception(sprintf(
75
                'Invalid item-key given to %s. Expected string but was given %s',
76
                static::CLASS,
77
                is_object($key) ? get_class($key) : @gettype($key)
78
            ));
79
        }
80
    }
81
82 View Code Duplication
    private function assertItemType($item)
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...
83
    {
84
        if (!is_a($item, $this->itemFqcn)) {
85
            throw new \Exception(sprintf(
86
                'Invalid item-type given to %s. Expected %s but was given %s',
87
                static::CLASS,
88
                $this->itemFqcn,
89
                is_object($item) ? get_class($item) : @gettype($item)
90
            ));
91
        }
92
    }
93
94
    public function __clone()
95
    {
96
        $this->compositeMap = new Map($this->compositeMap->toArray());
97
    }
98
}
99