Skip to content

Commit

Permalink
Factory: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 14, 2021
1 parent 2f28a34 commit 7faa6ed
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/PhpGenerator/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 : '');
Expand Down Expand Up @@ -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));
Expand All @@ -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());
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 7faa6ed

Please sign in to comment.