From 0467b7521ce9bfc2086b40195801a5c54df3bc35 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 13 Oct 2023 20:59:11 +0200 Subject: [PATCH] added Modifier --- readme.md | 2 +- src/PhpGenerator/ClassLike.php | 24 +++++++++++--------- src/PhpGenerator/Extractor.php | 6 ++--- src/PhpGenerator/Factory.php | 4 ++-- src/PhpGenerator/Method.php | 2 +- src/PhpGenerator/Modifier.php | 25 +++++++++++++++++++++ src/PhpGenerator/Traits/VisibilityAware.php | 16 ++++++------- 7 files changed, 54 insertions(+), 25 deletions(-) create mode 100644 src/PhpGenerator/Modifier.php diff --git a/readme.md b/readme.md index 456fe406..9685d377 100644 --- a/readme.md +++ b/readme.md @@ -77,7 +77,7 @@ $class->addConstant('ID', 123) ->setFinal(); $class->addProperty('items', [1, 2, 3]) - ->setPrivate() // or setVisibility('private') + ->setPrivate() // or setVisibility(Nette\PhpGenerator\Modifier::Private) ->setStatic() ->addComment('@var int[]'); diff --git a/src/PhpGenerator/ClassLike.php b/src/PhpGenerator/ClassLike.php index 9e308e9d..30cb4656 100644 --- a/src/PhpGenerator/ClassLike.php +++ b/src/PhpGenerator/ClassLike.php @@ -21,19 +21,23 @@ abstract class ClassLike use Traits\CommentAware; use Traits\AttributeAware; - public const - VisibilityPublic = 'public', - VisibilityProtected = 'protected', - VisibilityPrivate = 'private'; + /** @deprecated use Modifier::Public */ + public const VisibilityPublic = Modifier::Public; - /** @deprecated use ClassLike::VisibilityPublic */ - public const VISIBILITY_PUBLIC = self::VisibilityPublic; + /** @deprecated use Modifier::Protected */ + public const VisibilityProtected = Modifier::Protected; - /** @deprecated use ClassLike::VisibilityProtected */ - public const VISIBILITY_PROTECTED = self::VisibilityProtected; + /** @deprecated use Modifier::Private */ + public const VisibilityPrivate = Modifier::Private; - /** @deprecated use ClassLike::VisibilityPrivate */ - public const VISIBILITY_PRIVATE = self::VisibilityPrivate; + /** @deprecated use Modifier::Public */ + public const VISIBILITY_PUBLIC = Modifier::Public; + + /** @deprecated use Modifier::Protected */ + public const VISIBILITY_PROTECTED = Modifier::Protected; + + /** @deprecated use Modifier::Private */ + public const VISIBILITY_PRIVATE = Modifier::Private; private ?PhpNamespace $namespace; private ?string $name; diff --git a/src/PhpGenerator/Extractor.php b/src/PhpGenerator/Extractor.php index 6b00ad49..c3b2577f 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) => Modifier::Public, + (bool) ($flags & Node\Stmt\Class_::MODIFIER_PROTECTED) => Modifier::Protected, + (bool) ($flags & Node\Stmt\Class_::MODIFIER_PRIVATE) => Modifier::Private, default => null, }; } diff --git a/src/PhpGenerator/Factory.php b/src/PhpGenerator/Factory.php index 589c4b64..edb73cfe 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); + ? Modifier::Private + : ($from->isProtected() ? Modifier::Protected : Modifier::Public); } diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index 55b020c5..188ce085 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 === Modifier::Private)) { throw new Nette\InvalidStateException("Method $this->name() cannot be abstract and final or private at the same time."); } } diff --git a/src/PhpGenerator/Modifier.php b/src/PhpGenerator/Modifier.php new file mode 100644 index 00000000..f27f789c --- /dev/null +++ b/src/PhpGenerator/Modifier.php @@ -0,0 +1,25 @@ +visibility = ClassLike::VisibilityPublic; + $this->visibility = Modifier::Public; return $this; } public function isPublic(): bool { - return $this->visibility === ClassLike::VisibilityPublic || $this->visibility === null; + return $this->visibility === Modifier::Public || $this->visibility === null; } public function setProtected(): static { - $this->visibility = ClassLike::VisibilityProtected; + $this->visibility = Modifier::Protected; return $this; } public function isProtected(): bool { - return $this->visibility === ClassLike::VisibilityProtected; + return $this->visibility === Modifier::Protected; } public function setPrivate(): static { - $this->visibility = ClassLike::VisibilityPrivate; + $this->visibility = Modifier::Private; return $this; } public function isPrivate(): bool { - return $this->visibility === ClassLike::VisibilityPrivate; + return $this->visibility === Modifier::Private; } }