Chris@16: class = $class; Chris@16: $this->name = $name; Chris@16: Chris@16: $constants = $class->getConstants(); Chris@17: if (!\array_key_exists($name, $constants)) { Chris@16: throw new \InvalidArgumentException('Unknown constant: ' . $name); Chris@16: } Chris@16: Chris@16: $this->value = $constants[$name]; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Exports a reflection. Chris@16: * Chris@16: * @param string|object $class Chris@16: * @param string $name Chris@16: * @param bool $return pass true to return the export, as opposed to emitting it Chris@16: * Chris@16: * @return null|string Chris@16: */ Chris@16: public static function export($class, $name, $return = false) Chris@16: { Chris@16: $refl = new self($class, $name); Chris@16: $value = $refl->getValue(); Chris@16: Chris@17: $str = \sprintf('Constant [ public %s %s ] { %s }', \gettype($value), $refl->getName(), $value); Chris@16: Chris@16: if ($return) { Chris@16: return $str; Chris@16: } Chris@16: Chris@16: echo $str . "\n"; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Gets the declaring class. Chris@16: * Chris@16: * @return \ReflectionClass Chris@16: */ Chris@16: public function getDeclaringClass() Chris@16: { Chris@16: $parent = $this->class; Chris@16: Chris@16: // Since we don't have real reflection constants, we can't see where Chris@16: // it's actually defined. Let's check for a constant that is also Chris@16: // available on the parent class which has exactly the same value. Chris@16: // Chris@16: // While this isn't _technically_ correct, it's prolly close enough. Chris@16: do { Chris@16: $class = $parent; Chris@16: $parent = $class->getParentClass(); Chris@16: } while ($parent && $parent->hasConstant($this->name) && $parent->getConstant($this->name) === $this->value); Chris@16: Chris@16: return $class; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Get the constant's docblock. Chris@16: * Chris@16: * @return false Chris@16: */ Chris@16: public function getDocComment() Chris@16: { Chris@16: return false; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Gets the class constant modifiers. Chris@16: * Chris@16: * Since this is only used for PHP < 7.1, we can just return "public". All Chris@16: * the fancier modifiers are only available on PHP versions which have their Chris@16: * own ReflectionClassConstant class :) Chris@16: * Chris@16: * @return int Chris@16: */ Chris@16: public function getModifiers() Chris@16: { Chris@16: return \ReflectionMethod::IS_PUBLIC; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Gets the constant name. Chris@16: * Chris@16: * @return string Chris@16: */ Chris@16: public function getName() Chris@16: { Chris@16: return $this->name; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Gets the value of the constant. Chris@16: * Chris@16: * @return mixed Chris@16: */ Chris@16: public function getValue() Chris@16: { Chris@16: return $this->value; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Checks if class constant is private. Chris@16: * Chris@16: * @return bool false Chris@16: */ Chris@16: public function isPrivate() Chris@16: { Chris@16: return false; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Checks if class constant is protected. Chris@16: * Chris@16: * @return bool false Chris@16: */ Chris@16: public function isProtected() Chris@16: { Chris@16: return false; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Checks if class constant is public. Chris@16: * Chris@16: * @return bool true Chris@16: */ Chris@16: public function isPublic() Chris@16: { Chris@16: return true; Chris@16: } Chris@16: Chris@16: /** Chris@16: * To string. Chris@16: * Chris@16: * @return string Chris@16: */ Chris@16: public function __toString() Chris@16: { Chris@16: return $this->getName(); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Gets the constant's file name. Chris@16: * Chris@16: * Currently returns null, because if it returns a file name the signature Chris@16: * formatter will barf. Chris@16: */ Chris@16: public function getFileName() Chris@16: { Chris@16: return; Chris@16: // return $this->class->getFileName(); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Get the code start line. Chris@16: * Chris@16: * @throws \RuntimeException Chris@16: */ Chris@16: public function getStartLine() Chris@16: { Chris@16: throw new \RuntimeException('Not yet implemented because it\'s unclear what I should do here :)'); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Get the code end line. Chris@16: * Chris@16: * @throws \RuntimeException Chris@16: */ Chris@16: public function getEndLine() Chris@16: { Chris@16: return $this->getStartLine(); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Get a ReflectionClassConstant instance. Chris@16: * Chris@16: * In PHP >= 7.1, this will return a \ReflectionClassConstant from the Chris@16: * standard reflection library. For older PHP, it will return this polyfill. Chris@16: * Chris@16: * @param string|object $class Chris@16: * @param string $name Chris@16: * Chris@16: * @return ReflectionClassConstant|\ReflectionClassConstant Chris@16: */ Chris@16: public static function create($class, $name) Chris@16: { Chris@17: if (\class_exists('\\ReflectionClassConstant')) { Chris@16: return new \ReflectionClassConstant($class, $name); Chris@16: } Chris@16: Chris@16: return new self($class, $name); Chris@16: } Chris@16: }