Set   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 100
Duplicated Lines 64 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%
Metric Value
wmc 15
lcom 1
cbo 2
dl 64
loc 100
ccs 50
cts 50
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A hasValue() 0 4 1
A withValues() 0 9 1
A withValue() 13 13 2
A withoutValue() 13 13 2
A withValueAfter() 19 19 3
A withValueBefore() 19 19 3
A assertValid() 0 12 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 Set implements SetInterface
8
{
9
    use CanStructure;
10
11 5
    public function hasValue($value)
12
    {
13 5
        return in_array($value, $this->values, true);
14
    }
15
16 4
    public function withValues(array $values)
17
    {
18 4
        $this->assertValid($values);
19
20 3
        $copy = clone $this;
21 3
        $copy->values = array_unique($values, SORT_REGULAR);
22
23 3
        return $copy;
24
    }
25
26 3 View Code Duplication
    public function withValue($value)
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...
27
    {
28 3
        if ($this->hasValue($value)) {
29 1
            return $this;
30
        }
31
32 3
        $this->assertValid([$value]);
33
34 3
        $copy = clone $this;
35 3
        $copy->values[] = $value;
36
37 3
        return $copy;
38
    }
39
40 1 View Code Duplication
    public function withoutValue($value)
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...
41
    {
42 1
        $key = array_search($value, $this->values, true);
43
44 1
        if ($key === false) {
45 1
            return $this;
46
        }
47
48 1
        $copy = clone $this;
49 1
        unset($copy->values[$key]);
50
51 1
        return $copy;
52
    }
53
54 1 View Code Duplication
    public function withValueAfter($value, $search)
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...
55
    {
56 1
        if ($this->hasValue($value)) {
57 1
            return $this;
58
        }
59
60 1
        $this->assertValid([$value]);
61
62 1
        $copy = clone $this;
63
64 1
        $key = array_search($search, $this->values);
65 1
        if ($key === false) {
66 1
            array_push($copy->values, $value);
67 1
        } else {
68 1
            array_splice($copy->values, $key + 1, 0, $value);
69
        }
70
71 1
        return $copy;
72
    }
73
74 1 View Code Duplication
    public function withValueBefore($value, $search)
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...
75
    {
76 1
        if ($this->hasValue($value)) {
77 1
            return $this;
78
        }
79
80 1
        $this->assertValid([$value]);
81
82 1
        $copy = clone $this;
83
84 1
        $key = array_search($search, $this->values);
85 1
        if ($key === false) {
86 1
            array_unshift($copy->values, $value);
87 1
        } else {
88 1
            array_splice($copy->values, $key, 0, $value);
89
        }
90
91 1
        return $copy;
92
    }
93
94 9
    protected function assertValid(array $values)
95
    {
96 9
        if (empty($values)) {
97 3
            return;
98
        }
99
100 9
        if ($values !== array_values($values)) {
101 1
            throw ValidationException::invalid(
102
                'Set structures cannot have distinct keys'
103 1
            );
104
        }
105 9
    }
106
}
107