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

BitMaskConverter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 85
Duplicated Lines 8.24 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 91.43%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 7
loc 85
rs 10
c 0
b 0
f 0
ccs 32
cts 35
cp 0.9143

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 23 6
A convertToEnumSet() 0 12 3
A convertFromEnumSet() 0 9 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\Converter;
3
4
use SpareParts\Enum\Enum;
5
use SpareParts\Enum\Exception\EnumSetMustContainEnumsException;
6
use SpareParts\Enum\Exception\InvalidEnumClassException;
7
use SpareParts\Enum\Set\ISet;
8
use SpareParts\Enum\Set\ImmutableSet;
9
10
class BitMaskConverter implements IEnumSetConverter
11
{
12
    /**
13
     * @var string
14
     */
15
    private $enumClass;
16
17
    /**
18
     * @var array
19
     */
20
    private $mapping;
21
22
23
    /**
24
     * @param Enum[] $enumValuesMap
25
     */
26 9
    public function __construct($enumValuesMap)
27 4
    {
28 9
        if (!is_array($enumValuesMap) || !count($enumValuesMap)) {
29
            throw new EnumSetMustContainEnumsException('You have to provide converter mapping.');
30
        }
31
32
        // translate enum values to bit values mapping
33 9
        $innerMapping = [];
34 9
        $shift = 0;
35 9
        foreach ($enumValuesMap as $value) {
36
            // try to guess enum class
37 9
            if (is_null($this->enumClass)) {
38 9
                $this->setEnumClass(get_class($value));
39 9
            }
40 9
            if (!($value instanceof $this->enumClass)) {
41
                throw new EnumSetMustContainEnumsException(sprintf("Expected %s, got %s", $this->enumClass, get_class($value)));
42
            }
43 9
            $innerMapping[(string) $value] = 1 << $shift;
44 9
            $shift++;
45 9
        }
46
47 9
        $this->mapping = $innerMapping;
48 9
    }
49
50
51
    /**
52
     * @param int $values
53
     * @return ISet
54
     */
55 5
    public function convertToEnumSet($values)
56
    {
57 5
        $bitValue = (int) $values;
58
59 5
        $set = [];
60 5
        foreach ($this->mapping as $value => $bit) {
61 5
            if ($bitValue & $bit) {
62 4
                $set[] = call_user_func([$this->enumClass, 'instance'], $value);
63 4
            }
64 5
        }
65 5
        return new ImmutableSet($this->enumClass, $set);
66
    }
67
68
69
    /**
70
     * @param ISet $enumSet
71
     * @return int
72
     */
73 4
    public function convertFromEnumSet(ISet $enumSet)
74
    {
75 4
        $bitValue = 0;
76
        /** @var Enum $value */
77 4
        foreach ($enumSet as $value) {
78 3
            $bitValue = $bitValue | $this->mapping[(string) $value];
79 4
        }
80 4
        return $bitValue;
81
    }
82
83
84
    /**
85
     * @param string $enumClass
86
     */
87 9 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...
88
    {
89 9
        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...
90
            throw new InvalidEnumClassException("Class ${enumClass} does not implement Enum (as it should).");
91
        }
92
        $this->enumClass = $enumClass;
93
    }
94
}
95