public function testFromTrait() { $sourceLocator = new SingleFileSourceLocator(__DIR__ . '/../../Fixture/ExampleClass.php'); $reflector = new ClassReflector($sourceLocator); $exception = NotAClassReflection::fromReflectionClass($reflector->reflect(Fixture\ExampleTrait::class)); $this->assertInstanceOf(NotAClassReflection::class, $exception); $this->assertSame('Provided node "' . Fixture\ExampleTrait::class . '" is not class, but "trait"', $exception->getMessage()); }
/** * Set whether this class is final or not * * @param bool $isFinal */ public function setFinal($isFinal) { if (!$this->node instanceof ClassNode) { throw Exception\NotAClassReflection::fromReflectionClass($this); } if ($isFinal === true) { $this->node->type |= ClassNode::MODIFIER_FINAL; return; } $this->node->type &= ~ClassNode::MODIFIER_FINAL; }
/** * Get the parent class, if it is defined. If this class does not have a * specified parent class, this will throw an exception. * * You may optionally specify a source locator that will be used to locate * the parent class. If no source locator is given, a default will be used. * * @return ReflectionClass */ public function getParentClass() { if (!$this->node instanceof ClassNode || null === $this->node->extends) { return null; } $objectType = (new FindTypeFromAst())->__invoke($this->node->extends, $this->locatedSource, $this->getNamespaceName()); if (null === $objectType || !$objectType instanceof Object_) { return null; } // @TODO use actual `ClassReflector` or `FunctionReflector`? /* @var $parent self */ $parent = $this->reflector->reflect((string) $objectType->getFqsen()); if ($parent->isInterface() || $parent->isTrait()) { throw NotAClassReflection::fromReflectionClass($parent); } return $parent; }
/** * Get the parent class, if it is defined. If this class does not have a * specified parent class, this will throw an exception. * * You may optionally specify a source locator that will be used to locate * the parent class. If no source locator is given, a default will be used. * * @return ReflectionClass */ public function getParentClass() { if (null === $this->extendsClassType) { return null; } // @TODO use actual `ClassReflector` or `FunctionReflector`? /* @var $parent self */ $parent = $this->reflector->reflect((string) $this->extendsClassType); if ($parent->isInterface() || $parent->isTrait()) { throw NotAClassReflection::fromReflectionClass($parent); } return $parent; }