Completed
Push — master ( 0e4bc5...7f10cd )
by Ivannis Suárez
02:27
created

Enum.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Cubiche\Core\Enum;
13
14
use Cubiche\Core\Equatable\EquatableInterface;
15
use MyCLabs\Enum\Enum as BaseEnum;
16
17
/**
18
 * Enum class.
19
 *
20
 * @method static static __DEFAULT()
21
 *
22
 * @author Karel Osorio Ramírez <[email protected]>
23
 */
24
abstract class Enum extends BaseEnum implements EquatableInterface
25
{
26
    /**
27
     * @var array
28
     */
29
    protected static $defaultCache = array();
30
31
    /**
32
     * @param mixed $value
33
     *
34
     * @return bool
35
     */
36
    public function is($value)
37
    {
38
        return $this->value === $value;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function equals($other)
45
    {
46
        return \get_class($this) === \get_class($other) && $this->getValue() === $other->getValue();
47
    }
48
49
    /**
50
     * @param string $name
51
     * @param array  $arguments
52
     *
53
     * @return static
54
     *
55
     * @throws \BadMethodCallException
56
     */
57
    public static function __callStatic($name, $arguments)
58
    {
59
        $array = static::toArray();
60
        if (\strtoupper($name) === '__DEFAULT' && isset(static::$defaultCache[static::class])) {
61
            return new static(static::$defaultCache[static::class]);
62
        }
63
64
        if (isset($array[$name])) {
65
            return new static($array[$name]);
66
        }
67
68
        throw new \BadMethodCallException("No static method or enum constant '$name' in class ".static::class);
69
    }
70
71
    /**
72
     * @return array
73
     */
74
    public static function toArray()
75
    {
76
        if (!isset(static::$cache[static::class])) {
77
            static::$cache[static::class] = static::constants();
0 ignored issues
show
Since constants() 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 constants() 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...
78
            if (!isset(static::$defaultCache[static::class]) && !empty(static::$cache[static::class])) {
79
                static::$defaultCache[static::class] = \array_values(static::$cache[static::class])[0];
80
            }
81
        }
82
83
        return static::$cache[static::class];
84
    }
85
86
    /**
87
     * @return array
88
     */
89
    private static function constants()
90
    {
91
        $reflection = new \ReflectionClass(static::class);
92
        $constants = $reflection->getConstants();
93
        foreach ($constants as $name => $value) {
94
            if (\strtoupper($name) === '__DEFAULT') {
95
                static::$defaultCache[static::class] = $value;
96
                unset($constants[$name]);
97
            }
98
        }
99
100
        return $constants;
101
    }
102
}
103