Enum   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 56
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A isPartFromTheClass() 0 5 1
A setCacheArray() 0 7 2
A __toString() 0 4 1
1
<?php
2
3
namespace Klamius\Enum;
4
5
/**
6
 * Class Enum
7
 * @package Klamius\Enum
8
 */
9
abstract class Enum
10
{
11
    /**
12
     * @var mixed
13
     */
14
    protected $enumValue;
15
16
    /**
17
     * @var array
18
     */
19
    protected static $cache = array();
20
21
    /**
22
     * Enum constructor.
23
     * @param $enumValue
24
     */
25 4
    public function __construct($enumValue)
26
    {
27 4
        if (!self ::isPartFromTheClass($enumValue)) {
28 3
            throw new \InvalidArgumentException(
29 3
                sprintf('%s is not part from %s class', $enumValue, \get_called_class())
30
            );
31
        }
32
33 1
        $this->enumValue = $enumValue;
34 1
    }
35
36
    /**
37
     * @param $enumValue
38
     * @return bool
39
     */
40 4
    private function isPartFromTheClass($enumValue)
41
    {
42 4
        self::setCacheArray();
43 4
        return in_array($enumValue, static::$cache[\get_called_class()], true);
44
    }
45
46
    /**
47
     * @return void
48
     */
49 4
    private function setCacheArray()
50
    {
51 4
        if (!isset(static::$cache[\get_called_class()])) {
52 1
            $reflectionClass = new \ReflectionClass(\get_called_class());
53 1
            static::$cache[\get_called_class()] = $reflectionClass->getConstants();
54
        }
55 4
    }
56
57
    /**
58
     * @return string
59
     */
60 1
    public function __toString()
61
    {
62 1
        return (string)$this->enumValue;
63
    }
64
}
65