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

ImmutableSet::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.2
c 0
b 0
f 0
ccs 9
cts 10
cp 0.9
cc 4
eloc 8
nc 4
nop 2
crap 4.016
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