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

Set   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 30 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 15
loc 50
rs 10
c 0
b 0
f 0
ccs 19
cts 20
cp 0.95

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 15 15 4
A add() 0 8 2
A remove() 0 8 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 Set implements ISet
8
{
9
    use SetTrait;
10
11
    /**
12
     * @param string $enumClass
13
     * @param Enum[] $set
14
     */
15 14 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 14
        $this->setEnumClass($enumClass);
19
20 13
        if (!is_array($set)) {
21
            throw new EnumSetMustContainEnumsException(sprintf("Enum set must be initialized with array of enums."));
22
        }
23 13
        foreach ($set as $enum) {
24 13
            if (!($enum instanceof $this->enumClass)) {
25 2
                throw new EnumSetMustContainEnumsException(sprintf("Expected %s, got %s", $this->enumClass, get_class($enum)));
26
            }
27 12
            $this->set[(string) $enum] = $enum;
28 12
        }
29 11
    }
30
31
    /**
32
     * @param Enum $enum
33
     * @return $this
34
     */
35 3
    public function add(Enum $enum)
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
        $this->set[(string) $enum] = $enum;
41 2
        return $this;
42
    }
43
44
    /**
45
     * @param Enum $enum
46
     * @return $this
47
     */
48 4
    public function remove(Enum $enum)
49
    {
50 4
        if (!($enum instanceof $this->enumClass)) {
51 1
            throw new EnumSetMustContainEnumsException(sprintf("Expected %s, got %s", $this->enumClass, get_class($enum)));
52
        }
53 4
        unset($this->set[(string) $enum]);
54 3
        return $this;
55
    }
56
}
57