From 132099e54b2d37289345919a25b9592a92773113 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 13 Oct 2023 20:59:11 +0200 Subject: [PATCH] added Visibility --- src/PhpGenerator/ClassLike.php | 20 ++++++------ src/PhpGenerator/Extractor.php | 6 ++-- src/PhpGenerator/Factory.php | 4 +-- src/PhpGenerator/Method.php | 2 +- src/PhpGenerator/Traits/VisibilityAware.php | 27 ++++++---------- src/PhpGenerator/Visibility.php | 34 +++++++++++++++++++++ tests/PhpGenerator/ClassType.phpt | 3 +- 7 files changed, 60 insertions(+), 36 deletions(-) create mode 100644 src/PhpGenerator/Visibility.php diff --git a/src/PhpGenerator/ClassLike.php b/src/PhpGenerator/ClassLike.php index 9e308e9d..9e5469d4 100644 --- a/src/PhpGenerator/ClassLike.php +++ b/src/PhpGenerator/ClassLike.php @@ -21,19 +21,17 @@ abstract class ClassLike use Traits\CommentAware; use Traits\AttributeAware; - public const - VisibilityPublic = 'public', - VisibilityProtected = 'protected', - VisibilityPrivate = 'private'; + /** @deprecated use Visibility::Public */ + public const VisibilityPublic = Visibility::Public, + VISIBILITY_PUBLIC = Visibility::Public; - /** @deprecated use ClassLike::VisibilityPublic */ - public const VISIBILITY_PUBLIC = self::VisibilityPublic; + /** @deprecated use Visibility::Protected */ + public const VisibilityProtected = Visibility::Protected, + VISIBILITY_PROTECTED = Visibility::Protected; - /** @deprecated use ClassLike::VisibilityProtected */ - public const VISIBILITY_PROTECTED = self::VisibilityProtected; - - /** @deprecated use ClassLike::VisibilityPrivate */ - public const VISIBILITY_PRIVATE = self::VisibilityPrivate; + /** @deprecated use Visibility::Private */ + public const VisibilityPrivate = Visibility::Private, + VISIBILITY_PRIVATE = Visibility::Private; private ?PhpNamespace $namespace; private ?string $name; diff --git a/src/PhpGenerator/Extractor.php b/src/PhpGenerator/Extractor.php index 6b00ad49..2bb32624 100644 --- a/src/PhpGenerator/Extractor.php +++ b/src/PhpGenerator/Extractor.php @@ -428,9 +428,9 @@ private function formatValue(Node\Expr $value, int $level): Literal private function toVisibility(int $flags): ?string { return match (true) { - (bool) ($flags & Node\Stmt\Class_::MODIFIER_PUBLIC) => ClassType::VisibilityPublic, - (bool) ($flags & Node\Stmt\Class_::MODIFIER_PROTECTED) => ClassType::VisibilityProtected, - (bool) ($flags & Node\Stmt\Class_::MODIFIER_PRIVATE) => ClassType::VisibilityPrivate, + (bool) ($flags & Node\Stmt\Class_::MODIFIER_PUBLIC) => Visibility::Public, + (bool) ($flags & Node\Stmt\Class_::MODIFIER_PROTECTED) => Visibility::Protected, + (bool) ($flags & Node\Stmt\Class_::MODIFIER_PRIVATE) => Visibility::Private, default => null, }; } diff --git a/src/PhpGenerator/Factory.php b/src/PhpGenerator/Factory.php index 589c4b64..7a187767 100644 --- a/src/PhpGenerator/Factory.php +++ b/src/PhpGenerator/Factory.php @@ -310,8 +310,8 @@ private function getAttributes($from): array private function getVisibility($from): string { return $from->isPrivate() - ? ClassLike::VisibilityPrivate - : ($from->isProtected() ? ClassLike::VisibilityProtected : ClassLike::VisibilityPublic); + ? Visibility::Private + : ($from->isProtected() ? Visibility::Protected : Visibility::Public); } diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index 55b020c5..5dbd4c3e 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -99,7 +99,7 @@ public function addPromotedParameter(string $name, mixed $defaultValue = null): /** @throws Nette\InvalidStateException */ public function validate(): void { - if ($this->abstract && ($this->final || $this->visibility === ClassLike::VisibilityPrivate)) { + if ($this->abstract && ($this->final || $this->visibility === Visibility::Private)) { throw new Nette\InvalidStateException("Method $this->name() cannot be abstract and final or private at the same time."); } } diff --git a/src/PhpGenerator/Traits/VisibilityAware.php b/src/PhpGenerator/Traits/VisibilityAware.php index 0b3230ed..0a3a383f 100644 --- a/src/PhpGenerator/Traits/VisibilityAware.php +++ b/src/PhpGenerator/Traits/VisibilityAware.php @@ -9,8 +9,7 @@ namespace Nette\PhpGenerator\Traits; -use Nette; -use Nette\PhpGenerator\ClassLike; +use Nette\PhpGenerator\Visibility; /** @@ -22,16 +21,10 @@ trait VisibilityAware private ?string $visibility = null; - /** - * @param string|null $val public|protected|private - */ - public function setVisibility(?string $val): static + /** @param 'public'|'protected'|'private'|null $value */ + public function setVisibility(?string $value): static { - if (!in_array($val, [ClassLike::VisibilityPublic, ClassLike::VisibilityProtected, ClassLike::VisibilityPrivate, null], true)) { - throw new Nette\InvalidArgumentException('Argument must be public|protected|private.'); - } - - $this->visibility = $val; + $this->visibility = $value === null ? $value : Visibility::from($value); return $this; } @@ -44,39 +37,39 @@ public function getVisibility(): ?string public function setPublic(): static { - $this->visibility = ClassLike::VisibilityPublic; + $this->visibility = Visibility::Public; return $this; } public function isPublic(): bool { - return $this->visibility === ClassLike::VisibilityPublic || $this->visibility === null; + return $this->visibility === Visibility::Public || $this->visibility === null; } public function setProtected(): static { - $this->visibility = ClassLike::VisibilityProtected; + $this->visibility = Visibility::Protected; return $this; } public function isProtected(): bool { - return $this->visibility === ClassLike::VisibilityProtected; + return $this->visibility === Visibility::Protected; } public function setPrivate(): static { - $this->visibility = ClassLike::VisibilityPrivate; + $this->visibility = Visibility::Private; return $this; } public function isPrivate(): bool { - return $this->visibility === ClassLike::VisibilityPrivate; + return $this->visibility === Visibility::Private; } } diff --git a/src/PhpGenerator/Visibility.php b/src/PhpGenerator/Visibility.php new file mode 100644 index 00000000..a31c75c8 --- /dev/null +++ b/src/PhpGenerator/Visibility.php @@ -0,0 +1,34 @@ +getParameters()); Assert::exception( fn() => (new ClassType)->addMethod('method')->setVisibility('unknown'), - Nette\InvalidArgumentException::class, - 'Argument must be public|protected|private.', + ValueError::class, );