Completed
Pull Request — master (#7)
by Ondrej
05:55
created

StringSerializeConverter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 38.3 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 18
loc 47
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 2
A convertToEnumSet() 11 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 9 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 9
        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 1
            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 View Code Duplication
    public function convertToEnumSet($serialized)
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 4
        $serialized = (string) $serialized;
36 4
        $stringList = unserialize($serialized);
37
38 4
        $set = [];
39 4
        foreach ($stringList as $value) {
40 3
            $set[] = call_user_func([$this->enumClass, 'instance'], $value);
41 4
        }
42 4
        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 4
        $stringList = [];
52 4
        foreach ($enumSet as $enum) {
53 3
            $stringList[] = (string) $enum;
54 4
        }
55 4
        return serialize($stringList);
56
    }
57
}
58