Completed
Push — master ( ea1ae0...adbf7c )
by Ondrej
14s
created

BitMaskConverter   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 106
Duplicated Lines 11.32 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 2
dl 12
loc 106
ccs 43
cts 43
cp 1
rs 10
c 0
b 0
f 0

7 Methods

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