MabeEnum\Enum::detectConstants PHP Method

detectConstants() private static method

Detect all available constants by the given class
private static detectConstants ( string $class ) : array
$class string
return array
    private static function detectConstants($class)
    {
        if (!isset(self::$constants[$class])) {
            $reflection = new ReflectionClass($class);
            $constants = array();
            do {
                $scopeConstants = array();
                if (PHP_VERSION_ID >= 70100) {
                    // Since PHP-7.1 visibility modifiers are allowed for class constants
                    // for enumerations we are only interested in public once.
                    foreach ($reflection->getReflectionConstants() as $reflConstant) {
                        if ($reflConstant->isPublic()) {
                            $scopeConstants[$reflConstant->getName()] = $reflConstant->getValue();
                        }
                    }
                } else {
                    // In PHP < 7.1 all class constants were public by definition
                    $scopeConstants = $reflection->getConstants();
                }
                $constants = $scopeConstants + $constants;
            } while (($reflection = $reflection->getParentClass()) && $reflection->name !== __CLASS__);
            // Detect ambiguous values and report names
            $ambiguous = array();
            foreach ($constants as $value) {
                $names = array_keys($constants, $value, true);
                if (count($names) > 1) {
                    $ambiguous[var_export($value, true)] = $names;
                }
            }
            if (!empty($ambiguous)) {
                throw new LogicException('All possible values needs to be unique. The following are ambiguous: ' . implode(', ', array_map(function ($names) use($constants) {
                    return implode('/', $names) . '=' . var_export($constants[$names[0]], true);
                }, $ambiguous)));
            }
            self::$constants[$class] = $constants;
        }
        return self::$constants[$class];
    }