Completed
Push — master ( ea7ce0...85ac75 )
by Peter
06:16 queued 13s
created

Enum::setCacheArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
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
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
    public function __construct($enumValue)
26
    {
27
        if (!static::isPartFromTheClass($enumValue)) {
0 ignored issues
show
Bug introduced by
Since isPartFromTheClass() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of isPartFromTheClass() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
28
            throw new \InvalidArgumentException(
29
                sprintf('%s is not part from %s class', $enumValue, \get_called_class())
30
            );
31
        }
32
33
        $this->enumValue = $enumValue;
34
    }
35
36
    /**
37
     * @param $enumValue
38
     * @return bool
39
     */
40
    private static function isPartFromTheClass($enumValue)
41
    {
42
        static::setCacheArray();
0 ignored issues
show
Bug introduced by
Since setCacheArray() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of setCacheArray() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
43
        return in_array($enumValue, static::$cache[\get_called_class()], true);
44
    }
45
46
    /**
47
     * @return void
48
     * @throws \ReflectionException
49
     */
50
    private static function setCacheArray()
51
    {
52
        if (!isset(static::$cache[\get_called_class()])) {
53
            static::$cache[\get_called_class()] = (new \ReflectionClass(\get_called_class()))->getConstants();
54
        }
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function __toString()
61
    {
62
        return (string)$this->enumValue;
63
    }
64
}
65