fromClassReflection(new \ReflectionClass($class), $withBodies); if (!$instance instanceof static) { $class = is_object($class) ? $class::class : $class; throw new Nette\InvalidArgumentException("$class cannot be represented with " . static::class . '. Call ' . $instance::class . '::' . __FUNCTION__ . '() or ' . __METHOD__ . '() instead.'); } return $instance; } public static function fromCode(string $code): static { $instance = (new Factory) ->fromClassCode($code); if (!$instance instanceof static) { throw new Nette\InvalidArgumentException('Provided code cannot be represented with ' . static::class . '. Call ' . $instance::class . '::' . __FUNCTION__ . '() or ' . __METHOD__ . '() instead.'); } return $instance; } public function __construct(string $name) { if (str_contains($name, '\\')) { $this->namespace = new PhpNamespace(Helpers::extractNamespace($name)); $this->setName(Helpers::extractShortName($name)); } else { $this->namespace = func_num_args() > 1 ? func_get_arg(1) : null; // backward compatibility $this->setName($name); } } public function __toString(): string { return (new Printer)->printClass($this, $this->namespace); } /** @internal */ public function setNamespace(?PhpNamespace $namespace): static { $this->namespace = $namespace; return $this; } public function getNamespace(): ?PhpNamespace { return $this->namespace; } public function setName(?string $name): static { if ($name !== null && (!Helpers::isIdentifier($name) || isset(Helpers::Keywords[strtolower($name)]))) { throw new Nette\InvalidArgumentException("Value '$name' is not valid class name."); } $this->name = $name; return $this; } public function getName(): ?string { return $this->name; } public function getFullName(): ?string { return $this->name && ($namespace = $this->namespace?->getName()) ? $namespace . '\\' . $this->name : $this->name; } public function isClass(): bool { return $this instanceof ClassType; } public function isInterface(): bool { return $this instanceof InterfaceType; } public function isTrait(): bool { return $this instanceof TraitType; } public function isEnum(): bool { return $this instanceof EnumType; } /** @param list $names */ protected function validateNames(array $names): void { foreach ($names as $name) { if (!Helpers::isNamespaceIdentifier($name, allowLeadingSlash: true)) { throw new Nette\InvalidArgumentException("Value '$name' is not valid class name."); } } } public function validate(): void { } public function __clone(): void { $this->attributes = array_map(fn($attr) => clone $attr, $this->attributes); } }