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

SetTrait::setEnumClass()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 3
eloc 4
nc 2
nop 1
crap 3
1
<?php
2
namespace SpareParts\Enum\Set;
3
4
use SpareParts\Enum\Enum;
5
use SpareParts\Enum\Exception\InvalidEnumClassException;
6
7
trait SetTrait
8
{
9
    /**
10
     * @var Enum[]
11
     */
12
    protected $set = [];
13
14
    /**
15
     * @var string
16
     */
17
    protected $enumClass;
18
19
    /**
20
     * @return \ArrayIterator
21
     */
22 12
    public function getIterator()
23
    {
24 12
        return new \ArrayIterator(array_values($this->set));
25
    }
26
27
    /**
28
     * @return int
29
     */
30 14
    public function count()
31
    {
32 14
        return count($this->set);
33
    }
34
35
    /**
36
     * @param Enum $value
37
     * @return bool
38
     */
39 16
    public function contains(Enum $value)
40
    {
41 16
        if (in_array($value, $this->set)) {
42 16
            return true;
43
        }
44 10
        return false;
45
    }
46
47
    /**
48
     * @param string $enumClass
49
     */
50 33 View Code Duplication
    protected function setEnumClass($enumClass)
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 33
        if (!is_null($enumClass) && !is_subclass_of($enumClass, Enum::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \SpareParts\Enum\Enum::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
53 2
            throw new InvalidEnumClassException("Class ${enumClass} does not implement Enum (as it should). Maybe you forgot to specify correct enum class in constructor?");
54
        }
55
        $this->enumClass = $enumClass;
56
    }
57
}
58