Completed
Pull Request — master (#7)
by Ondrej
01:16
created

ImmutableSet   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 64.81 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 35
loc 54
rs 10
c 0
b 0
f 0
ccs 21
cts 22
cp 0.9545

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 15 15 4
A add() 10 10 2
A remove() 10 10 2

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
namespace SpareParts\Enum\Set;
3
4
use SpareParts\Enum\Enum;
5
use SpareParts\Enum\Exception\EnumSetMustContainEnumsException;
6
7
class ImmutableSet implements ISet
8
{
9
    use SetTrait;
10
11
    /**
12
     * @param string $enumClass
13
     * @param Enum[] $set
14
     */
15 19 View Code Duplication
    public function __construct($enumClass, $set = [])
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...
16
    {
17
        // if enum class was sent, we must make sure it is valid
18 19
        $this->setEnumClass($enumClass);
19
20 18
        if (!is_array($set)) {
21
            throw new EnumSetMustContainEnumsException(sprintf("Enum set must be initialized with array of enums."));
22
        }
23 18
        foreach ($set as $enum) {
24 17
            if (!($enum instanceof $this->enumClass)) {
25 2
                throw new EnumSetMustContainEnumsException(sprintf("Expected %s, got %s", $this->enumClass, get_class($enum)));
26
            }
27 16
            $this->set[(string) $enum] = $enum;
28 17
        }
29 16
    }
30
31
    /**
32
     * @param Enum $enum
33
     * @return self
34
     */
35 3 View Code Duplication
    public function add(Enum $enum)
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...
36
    {
37 3
        if (!($enum instanceof $this->enumClass)) {
38 1
            throw new EnumSetMustContainEnumsException(sprintf("Expected %s, got %s", $this->enumClass, get_class($enum)));
39
        }
40 2
        $set = $this->set;
41 2
        $set[(string) $enum] = $enum;
42
43 2
        return new ImmutableSet($this->enumClass, $set);
44
    }
45
46
    /**
47
     * @param Enum $enum
48
     * @return self
49
     */
50 4 View Code Duplication
    public function remove(Enum $enum)
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...
51
    {
52 4
        if (!($enum instanceof $this->enumClass)) {
53 1
            throw new EnumSetMustContainEnumsException(sprintf("Expected %s, got %s", $this->enumClass, get_class($enum)));
54
        }
55 3
        $set = $this->set;
56 3
        unset($set[(string) $enum]);
57
58 3
        return new ImmutableSet($this->enumClass, $set);
59
    }
60
}
61