OrderedList   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 68
Duplicated Lines 41.18 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%
Metric Value
wmc 10
lcom 1
cbo 2
dl 28
loc 68
ccs 34
cts 34
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A hasValue() 0 4 1
A withValues() 14 14 2
A withValue() 0 10 1
A withoutValue() 14 14 2
A sortValues() 0 4 1
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 OrderedList implements ListInterface
8
{
9
    use CanStructure;
10
11 3
    public function hasValue($value)
12
    {
13 3
        return in_array($value, $this->values, true);
14
    }
15
16 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...
17
    {
18 5
        if ($this->values === $values) {
19 1
            return $this;
20
        }
21
22 5
        $this->assertValid($values);
23
24 4
        $copy = clone $this;
25 4
        $copy->values = $values;
26 4
        $copy->sortValues();
27
28 4
        return $copy;
29
    }
30
31 1
    public function withValue($value)
32
    {
33 1
        $this->assertValid([$value]);
34
35 1
        $copy = clone $this;
36 1
        $copy->values[] = $value;
37 1
        $copy->sortValues();
38
39 1
        return $copy;
40
    }
41
42 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...
43
    {
44 1
        $key = array_search($value, $this->values, true);
45
46 1
        if ($key === false) {
47 1
            return $this;
48
        }
49
50 1
        $copy = clone $this;
51 1
        unset($copy->values[$key]);
52 1
        $copy->sortValues();
53
54 1
        return $copy;
55
    }
56
57 6
    protected function sortValues()
58
    {
59 6
        sort($this->values, SORT_REGULAR);
60 6
    }
61
62 9
    protected function assertValid(array $values)
63
    {
64 9
        if (empty($values)) {
65 1
            return;
66
        }
67
68 9
        if ($values !== array_values($values)) {
69 1
            throw ValidationException::invalid(
70
                'List structures cannot have distinct keys'
71 1
            );
72
        }
73 9
    }
74
}
75