AbstractEnum   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 100
Duplicated Lines 21 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 2
cbo 2
dl 21
loc 100
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 2
A get() 0 4 1
A __callStatic() 11 11 3
A __call() 10 10 3
A getValue() 0 4 1
A equals() 0 4 2
A __toString() 0 4 1
A isValid() 0 4 1
A getValidOptions() 0 4 1
A getClassName() 0 4 1
A getConstants() 0 4 1
A getValidOptionsAsString() 0 9 1

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
declare(strict_types=1);
4
5
namespace Werkspot\Enum;
6
7
use BadMethodCallException;
8
use Doctrine\Common\Inflector\Inflector;
9
use InvalidArgumentException;
10
use ReflectionClass;
11
use Werkspot\Enum\Util\ClassNameConverter;
12
13
abstract class AbstractEnum
14
{
15
    protected $value;
16
17 15
    protected function __construct($value)
18
    {
19 15
        if (!$this->isValid($value)) {
20 4
            $message = 'Value [%s] is not matching any valid value of class "%s". Valid values are [%s].';
21
22 4
            throw new InvalidArgumentException(sprintf(
23 4
                $message,
24 4
                $value,
25 4
                $this->getClassName(),
26 4
                self::getValidOptionsAsString()
27
            ));
28
        }
29
30 11
        $this->value = $value;
31 11
    }
32
33
    /**
34
     * @return static
35
     */
36 13
    public static function get($value): self
37
    {
38 13
        return new static($value);
39
    }
40
41 3 View Code Duplication
    public static function __callStatic(string $methodName, array $arguments): self
42
    {
43 3
        foreach (self::getConstants() as $option => $value) {
44 3
            $expectedMethodName = lcfirst(Inflector::classify(strtolower($option)));
45 3
            if ($expectedMethodName === $methodName) {
46 3
                return new static($value);
47
            }
48
        }
49
50 1
        throw new BadMethodCallException(sprintf('%s::%s() does not exist', static::class, $methodName));
51
    }
52
53 3 View Code Duplication
    public function __call(string $methodName, array $arguments)
54
    {
55 3
        foreach (self::getConstants() as $option => $value) {
56 3
            $isaMethodName = 'is' . ucfirst(Inflector::classify(strtolower($option)));
57 3
            if ($isaMethodName === $methodName) {
58 3
                return $this->equals(static::get($value));
59
            }
60
        }
61 1
        throw new BadMethodCallException(sprintf('%s::%s() does not exist', static::class, $methodName));
62
    }
63
64
65
    /**
66
     * @return mixed
67
     */
68 8
    public function getValue()
69
    {
70 8
        return $this->value;
71
    }
72
73 6
    public function equals(self $other): bool
74
    {
75 6
        return get_class($other) === get_class($this) && $other->getValue() === $this->value;
76
    }
77
78 3
    public function __toString(): string
79
    {
80 3
        return (string) $this->value;
81
    }
82
83 15
    protected function isValid($value): bool
84
    {
85 15
        return in_array($value, self::getValidOptions(), true);
86
    }
87
88 16
    public static function getValidOptions(): array
89
    {
90 16
        return array_values(self::getConstants());
91
    }
92
93 4
    protected function getClassName(): string
94
    {
95 4
        return ClassNameConverter::stripNameSpace(static::class);
96
    }
97
98 17
    private static function getConstants(): array
99
    {
100 17
        return (new ReflectionClass(static::class))->getConstants();
101
    }
102
103 4
    private static function getValidOptionsAsString(): string
104
    {
105 4
        return implode(
106 4
            ', ',
107 4
            array_map(function ($option) {
108 4
                return var_export($option, true);
109 4
            }, self::getValidOptions())
110
        );
111
    }
112
}
113