/** * Constructor. * * @param string $name Constant name * @param mixed $value Constant value * @param \TokenReflection\Broker $broker Reflection broker * @param \TokenReflection\Php\ReflectionClass $parent Defining class reflection * * @throws \TokenReflection\Exception\RuntimeException If real parent class could not be determined. */ public function __construct($name, $value, Broker $broker, ReflectionClass $parent = null) { $this->name = $name; $this->value = $value; $this->broker = $broker; if (null !== $parent) { $realParent = null; if (array_key_exists($name, $parent->getOwnConstants())) { $realParent = $parent; } if (null === $realParent) { foreach ($parent->getParentClasses() as $grandParent) { if (array_key_exists($name, $grandParent->getOwnConstants())) { $realParent = $grandParent; break; } } } if (null === $realParent) { foreach ($parent->getInterfaces() as $interface) { if (array_key_exists($name, $interface->getOwnConstants())) { $realParent = $interface; break; } } } if (null === $realParent) { throw new Exception\RuntimeException('Could not determine constant real parent class.', Exception\RuntimeException::DOES_NOT_EXIST, $this); } $this->declaringClassName = $realParent->getName(); $this->userDefined = $realParent->isUserDefined(); } else { if (!array_key_exists($name, get_defined_constants(false))) { $this->userDefined = true; } else { $declared = get_defined_constants(true); $this->userDefined = array_key_exists($name, $declared['user']); } } }
/** * Returns the declaring class reflection. * * @return \TokenReflection\IReflectionClass */ public function getDeclaringClass() { $class = parent::getDeclaringClass(); return $class ? ReflectionClass::create($class, $this->broker) : null; }
/** * Creates a new class instance without using a constructor. * * @return object * @throws \TokenReflection\Exception\RuntimeException If the class inherits from an internal class. */ public function newInstanceWithoutConstructor() { if (!class_exists($this->name, true)) { throw new Exception\RuntimeException('Could not create an instance; class does not exist.', Exception\RuntimeException::DOES_NOT_EXIST, $this); } $reflection = new \TokenReflection\Php\ReflectionClass($this->name, $this->getBroker()); return $reflection->newInstanceWithoutConstructor(); }
/** * Returns the declaring class reflection. * * @return \TokenReflection\IReflectionClass */ public function getDeclaringClass() { return ReflectionClass::create(parent::getDeclaringClass(), $this->broker); }
/** * Creates a new class instance without using a constructor. * * @return object * @throws \TokenReflection\Exception\RuntimeException If the class inherits from an internal class. */ public function newInstanceWithoutConstructor() { if ($this->isInternal()) { throw new Exception\RuntimeException('Could not create an instance; only user defined classes can be instantiated.', Exception\RuntimeException::UNSUPPORTED, $this); } foreach ($this->getParentClasses() as $parent) { if ($parent->isInternal()) { throw new Exception\RuntimeException('Could not create an instance; only user defined classes can be instantiated.', Exception\RuntimeException::UNSUPPORTED, $this); } } if (PHP_VERSION_ID >= 50400) { return parent::newInstanceWithoutConstructor(); } return unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->getName()), $this->getName())); }
/** * Returns a reflection object of the given class (FQN expected). * * @param string $className CLass bame * * @return \TokenReflection\IReflectionClass */ public function getClass($className) { if (empty($this->declaredClasses)) { $this->declaredClasses = array_flip(array_merge(get_declared_classes(), get_declared_interfaces())); } $className = ltrim($className, '\\'); try { $ns = $this->getNamespace(($boundary = strrpos($className, '\\')) ? substr($className, 0, $boundary) : TokenReflection\ReflectionNamespace::NO_NAMESPACE_NAME); return $ns->getClass($className); } catch (Exception\BaseException $e) { if (isset($this->declaredClasses[$className])) { $reflection = new Php\ReflectionClass($className, $this->broker); if ($reflection->isInternal()) { return $reflection; } } return new Dummy\ReflectionClass($className, $this->broker); } }
/** * Tests an exception thrown when trying to create the reflection from a PHP internal reflection. * * @expectedException \TokenReflection\Exception\RuntimeException */ public function testInternalClassReflectionCreate() { Php\ReflectionClass::create(new \ReflectionFunction('create_function'), $this->getBroker()); }