Skip to content

Commit b35f406

Browse files
committed
Factory: refactoring
1 parent 2f28a34 commit b35f406

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

src/PhpGenerator/Factory.php

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,7 @@ public function fromMethodReflection(\ReflectionMethod $from): Method
111111
$method->setParameters(array_map([$this, 'fromParameterReflection'], $from->getParameters()));
112112
$method->setStatic($from->isStatic());
113113
$isInterface = $from->getDeclaringClass()->isInterface();
114-
$method->setVisibility(
115-
$from->isPrivate()
116-
? ClassType::VISIBILITY_PRIVATE
117-
: ($from->isProtected() ? ClassType::VISIBILITY_PROTECTED : ($isInterface ? null : ClassType::VISIBILITY_PUBLIC))
118-
);
114+
$method->setVisibility($isInterface ? null : $this->getVisibility($from));
119115
$method->setFinal($from->isFinal());
120116
$method->setAbstract($from->isAbstract() && !$isInterface);
121117
$method->setBody($from->isAbstract() ? null : '');
@@ -201,11 +197,7 @@ public function fromConstantReflection(\ReflectionClassConstant $from): Constant
201197
{
202198
$const = new Constant($from->name);
203199
$const->setValue($from->getValue());
204-
$const->setVisibility(
205-
$from->isPrivate()
206-
? ClassType::VISIBILITY_PRIVATE
207-
: ($from->isProtected() ? ClassType::VISIBILITY_PROTECTED : ClassType::VISIBILITY_PUBLIC)
208-
);
200+
$const->setVisibility($this->getVisibility($from));
209201
$const->setFinal(PHP_VERSION_ID >= 80100 ? $from->isFinal() : false);
210202
$const->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
211203
$const->setAttributes(self::getAttributes($from));
@@ -229,11 +221,7 @@ public function fromPropertyReflection(\ReflectionProperty $from): Property
229221
$prop = new Property($from->name);
230222
$prop->setValue($defaults[$prop->getName()] ?? null);
231223
$prop->setStatic($from->isStatic());
232-
$prop->setVisibility(
233-
$from->isPrivate()
234-
? ClassType::VISIBILITY_PRIVATE
235-
: ($from->isProtected() ? ClassType::VISIBILITY_PROTECTED : ClassType::VISIBILITY_PUBLIC)
236-
);
224+
$prop->setVisibility($this->getVisibility($from));
237225
if (PHP_VERSION_ID >= 70400) {
238226
if ($from->getType() instanceof \ReflectionNamedType) {
239227
$prop->setType($from->getType()->getName());
@@ -255,6 +243,25 @@ public function fromPropertyReflection(\ReflectionProperty $from): Property
255243
}
256244

257245

246+
private function getAttributes($from): array
247+
{
248+
if (PHP_VERSION_ID < 80000) {
249+
return [];
250+
}
251+
return array_map(function ($attr) {
252+
return new Attribute($attr->getName(), $attr->getArguments());
253+
}, $from->getAttributes());
254+
}
255+
256+
257+
private function getVisibility($from): string
258+
{
259+
return $from->isPrivate()
260+
? ClassType::VISIBILITY_PRIVATE
261+
: ($from->isProtected() ? ClassType::VISIBILITY_PROTECTED : ClassType::VISIBILITY_PUBLIC);
262+
}
263+
264+
258265
private function loadMethodBodies(\ReflectionClass $from): array
259266
{
260267
if ($from->isAnonymous()) {
@@ -395,17 +402,4 @@ private function parse($from): array
395402

396403
return [$code, $stmts];
397404
}
398-
399-
400-
private function getAttributes($from): array
401-
{
402-
if (PHP_VERSION_ID < 80000) {
403-
return [];
404-
}
405-
$res = [];
406-
foreach ($from->getAttributes() as $attr) {
407-
$res[] = new Attribute($attr->getName(), $attr->getArguments());
408-
}
409-
return $res;
410-
}
411405
}

0 commit comments

Comments
 (0)