Dictionary   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 74
Duplicated Lines 17.57 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%
Metric Value
wmc 13
lcom 1
cbo 2
dl 13
loc 74
ccs 35
cts 35
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getValue() 0 8 2
A hasValue() 0 4 1
A withValues() 13 13 2
A withValue() 0 13 3
A withoutValue() 0 11 2
A assertValid() 0 15 3

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 Equip\Structure;
4
5
use Equip\Structure\Traits\CanStructure;
6
7
class Dictionary implements DictionaryInterface
8
{
9
    use CanStructure;
10
11 4
    public function getValue($key, $default = null)
12
    {
13 4
        if ($this->hasValue($key)) {
14 4
            return $this->values[$key];
15
        }
16
17 2
        return $default;
18
    }
19
20 8
    public function hasValue($key)
21
    {
22 8
        return array_key_exists($key, $this->values);
23
    }
24
25 5 View Code Duplication
    public function withValues(array $values)
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...
26
    {
27 5
        if ($this->values === $values) {
28 2
            return $this;
29
        }
30
31 5
        $this->assertValid($values);
32
33 3
        $copy = clone $this;
34 3
        $copy->values = $values;
35
36 3
        return $copy;
37
    }
38
39 4
    public function withValue($key, $value)
40
    {
41 4
        if ($this->hasValue($key) && $this->getValue($key) === $value) {
42 2
            return $this;
43
        }
44
45 4
        $this->assertValid([$key => $value]);
46
47 4
        $copy = clone $this;
48 4
        $copy->values[$key] = $value;
49
50 4
        return $copy;
51
    }
52
53 2
    public function withoutValue($key)
54
    {
55 2
        if (!$this->hasValue($key)) {
56 2
            return $this;
57
        }
58
59 2
        $copy = clone $this;
60 2
        unset($copy->values[$key]);
61
62 2
        return $copy;
63
    }
64
65 15
    protected function assertValid(array $values)
66
    {
67 15
        if (empty($values)) {
68 1
            return;
69
        }
70
71 15
        $keys = array_keys($values);
72 15
        $vals = array_values($values);
73
74 15
        if ($keys === array_keys($vals)) {
75 2
            throw ValidationException::invalid(
76
                'Dictionary values must have distinct keys'
77 2
            );
78
        }
79 15
    }
80
}
81