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

StringSerializeConverter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 14.58 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 2
A convertToEnumSet() 0 11 2
A convertFromEnumSet() 0 8 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
3
namespace SpareParts\Enum\Set\Converter;
4
5
6
use SpareParts\Enum\Enum;
7
use SpareParts\Enum\Exception\InvalidEnumClassException;
8
use SpareParts\Enum\Set\ISet;
9
use SpareParts\Enum\Set\ImmutableSet;
10
11
class StringSerializeConverter implements IEnumSetConverter
12
{
13
    /**
14
     * @var string
15
     */
16
    private $enumClass;
17
18
    /**
19
     * @param string $enumClass
20
     */
21 View Code Duplication
    public function __construct($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...
22
    {
23
        if (!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...
24
            throw new InvalidEnumClassException("Class ${enumClass} does not implement Enum (as it should).");
25
        }
26
        $this->enumClass = $enumClass;
27
    }
28
29
    /**
30
     * @param string $serialized
31
     * @return ImmutableSet
32
     */
33
    public function convertToEnumSet($serialized)
34
    {
35
        $serialized = (string) $serialized;
36
        $stringList = explode("|", unserialize($serialized));
37
38
        $set = [];
39
        foreach ($stringList as $value) {
40
            $set[] = call_user_func([$this->enumClass, 'instance'], $value);
41
        }
42
        return new ImmutableSet($this->enumClass, $set);
43
    }
44
45
    /**
46
     * @param ISet $enumSet
47
     * @return string
48
     */
49
    public function convertFromEnumSet(ISet $enumSet)
50
    {
51
        $stringList = [];
52
        foreach ($enumSet as $enum) {
53
            $stringList[] = (string) $enum;
54
        }
55
        return serialize(implode("|", $stringList));
56
    }
57
58
}
59