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

TypedMapTrait   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 92
Duplicated Lines 22.83 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 1
dl 21
loc 92
ccs 0
cts 73
cp 0
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A has() 0 4 1
A get() 0 4 1
A set() 0 7 1
A count() 0 4 1
A toArray() 0 4 1
A isEmpty() 0 4 1
A getIterator() 0 4 1
A getItemFqcn() 0 4 1
A __get() 0 4 1
A init() 0 9 2
A assertItemKey() 10 10 3
A assertItemType() 11 11 3
A __clone() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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