Completed
Pull Request — master (#11)
by Ondrej
01:47 queued 44s
created

BitMaskConverter   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 93
Duplicated Lines 12.9 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 12
loc 93
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A convertToEnumSet() 12 12 3
A convertFromEnumSet() 0 9 2
A prepareInnerMapping() 0 13 2
A updateEnumClass() 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(iterable $enumValuesMap)
25
    {
26 14
        $this->mapping = $this->prepareInnerMapping($enumValuesMap);
27 2
    }
28
29 12
    /**
30 9
     * @param int $values A number representing a bit mask
31
     * @return ISet
32
     */
33 View Code Duplication
    public function convertToEnumSet($values) : ISet
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...
34
    {
35
        $bitValue = (int) $values;
36 5
37
        $set = [];
38 5
        foreach ($this->mapping as $value => $bit) {
39
            if ($bitValue & $bit) {
40 5
                $set[] = call_user_func([$this->enumClass, 'instance'], $value);
41 5
            }
42 5
        }
43 5
        return new ImmutableSet($this->enumClass, $set);
44
    }
45
46 5
    public function convertFromEnumSet(ISet $enumSet): int
47
    {
48
        $bitValue = 0;
49
        /** @var Enum $value */
50
        foreach ($enumSet as $value) {
51
            $bitValue = $bitValue | $this->mapping[(string) $value];
52
        }
53 4
        return $bitValue;
54
    }
55 4
56
    private function prepareInnerMapping(iterable $enumValuesMap): array
57 4
    {
58 3
        // translate enum values to bit values mapping
59
        $innerMapping = [];
60 4
        $shift = 0;
61
        foreach ($enumValuesMap as $value) {
62
            $this->updateEnumClass($value);
63
64
            $innerMapping[(string)$value] = 1 << $shift;
65
            $shift++;
66
        }
67 12
        return $innerMapping;
68
    }
69
70 12
    private function updateEnumClass(string $value): void
71 12
    {
72 12
        // try to guess enum class
73 12
        if (is_null($this->enumClass)) {
74
            $this->setEnumClass($value);
75 11
        }
76 11
        if (!is_object($value) || !($value instanceof $this->enumClass)) {
77
            throw new EnumSetMustContainEnumsException(sprintf("Expected `%s`, got `%s`", $this->enumClass, $this->printVar($value)));
78 9
        }
79
    }
80
81
    /**
82
     * @param string|Enum $enum Class name or directly any Enum instance
83
     * @throws EnumSetMustContainEnumsException
84 12
     */
85
    private function setEnumClass($enum): void
86
    {
87 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...
88 12
            throw new EnumSetMustContainEnumsException(sprintf("Class %s does not implement Enum (as it should).", $this->printVar($enum)));
89
        }
90 11
        $this->enumClass = get_class($enum);
91 2
    }
92
93 11
    /**
94
     * @param mixed $value
95
     * @return string
96
     */
97
    private function printVar($value): string
98 12
    {
99
        return is_object($value) ? get_class($value) : var_export($value, true);
100 12
    }
101
}
102