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

SetTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 13.73 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 1
dl 7
loc 51
rs 10
c 0
b 0
f 0
ccs 11
cts 11
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 4 1
A count() 0 4 1
A contains() 0 7 2
A setEnumClass() 7 7 3

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\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