From 7faa6ed8578b21147042de459c11ce544c7e5b44 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 14 Sep 2021 14:27:58 +0200 Subject: [PATCH] Factory: refactoring --- src/PhpGenerator/Factory.php | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/PhpGenerator/Factory.php b/src/PhpGenerator/Factory.php index 4f4d04a1..3d12c02e 100644 --- a/src/PhpGenerator/Factory.php +++ b/src/PhpGenerator/Factory.php @@ -111,11 +111,7 @@ public function fromMethodReflection(\ReflectionMethod $from): Method $method->setParameters(array_map([$this, 'fromParameterReflection'], $from->getParameters())); $method->setStatic($from->isStatic()); $isInterface = $from->getDeclaringClass()->isInterface(); - $method->setVisibility( - $from->isPrivate() - ? ClassType::VISIBILITY_PRIVATE - : ($from->isProtected() ? ClassType::VISIBILITY_PROTECTED : ($isInterface ? null : ClassType::VISIBILITY_PUBLIC)) - ); + $method->setVisibility($isInterface ? null : $this->getVisibility($from)); $method->setFinal($from->isFinal()); $method->setAbstract($from->isAbstract() && !$isInterface); $method->setBody($from->isAbstract() ? null : ''); @@ -201,11 +197,7 @@ public function fromConstantReflection(\ReflectionClassConstant $from): Constant { $const = new Constant($from->name); $const->setValue($from->getValue()); - $const->setVisibility( - $from->isPrivate() - ? ClassType::VISIBILITY_PRIVATE - : ($from->isProtected() ? ClassType::VISIBILITY_PROTECTED : ClassType::VISIBILITY_PUBLIC) - ); + $const->setVisibility($this->getVisibility($from)); $const->setFinal(PHP_VERSION_ID >= 80100 ? $from->isFinal() : false); $const->setComment(Helpers::unformatDocComment((string) $from->getDocComment())); $const->setAttributes(self::getAttributes($from)); @@ -229,11 +221,7 @@ public function fromPropertyReflection(\ReflectionProperty $from): Property $prop = new Property($from->name); $prop->setValue($defaults[$prop->getName()] ?? null); $prop->setStatic($from->isStatic()); - $prop->setVisibility( - $from->isPrivate() - ? ClassType::VISIBILITY_PRIVATE - : ($from->isProtected() ? ClassType::VISIBILITY_PROTECTED : ClassType::VISIBILITY_PUBLIC) - ); + $prop->setVisibility($this->getVisibility($from)); if (PHP_VERSION_ID >= 70400) { if ($from->getType() instanceof \ReflectionNamedType) { $prop->setType($from->getType()->getName()); @@ -408,4 +396,12 @@ private function getAttributes($from): array } return $res; } + + + private function getVisibility($from): string + { + return $from->isPrivate() + ? ClassType::VISIBILITY_PRIVATE + : ($from->isProtected() ? ClassType::VISIBILITY_PROTECTED : ClassType::VISIBILITY_PUBLIC); + } }