OrderedList::withValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
crap 1
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