private static function detectConstants($class)
    {
        if (!isset(self::$constants[$class])) {
            $reflection = new ReflectionClass($class);
            $constants  = [];

            do {
                $scopeConstants = [];
                // Enumerators must be defined as public class constants
                foreach ($reflection->getReflectionConstants() as $reflConstant) {
                    if ($reflConstant->isPublic()) {
                        $scopeConstants[ $reflConstant->getName() ] = $reflConstant->getValue();
                    }
                }

                $constants = $scopeConstants + $constants;
            } while (($reflection = $reflection->getParentClass()) && $reflection->name !== __CLASS__);

            assert(
                self::noAmbiguousValues($constants),
                "Ambiguous enumerator values detected for {$class}"
            );

            self::$constants[$class] = $constants;
            self::$names[$class] = \array_keys($constants);
        }

        return self::$constants[$class];
    }