Skip to content

Commit ad44c0e

Browse files
committed
Factory::getAttributes() refactoring to formatAttributes()
1 parent 054cf34 commit ad44c0e

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

src/PhpGenerator/Factory.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private function setupInheritance(ClassLike $class, \ReflectionClass $from): voi
8484
}
8585

8686
$class->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
87-
$class->setAttributes($this->getAttributes($from));
87+
$class->setAttributes($this->formatAttributes($from->getAttributes()));
8888
if ($from->getParentClass()) {
8989
$class->setExtends($from->getParentClass()->name);
9090
$class->setImplements(array_values(array_diff($class->getImplements(), $from->getParentClass()->getInterfaceNames())));
@@ -190,7 +190,7 @@ public function fromMethodReflection(\ReflectionMethod $from): Method
190190
$method->setReturnReference($from->returnsReference());
191191
$method->setVariadic($from->isVariadic());
192192
$method->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
193-
$method->setAttributes($this->getAttributes($from));
193+
$method->setAttributes($this->formatAttributes($from->getAttributes()));
194194
$method->setReturnType((string) $from->getReturnType());
195195

196196
return $method;
@@ -207,7 +207,7 @@ public function fromFunctionReflection(\ReflectionFunction $from, bool $withBody
207207
$function->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
208208
}
209209

210-
$function->setAttributes($this->getAttributes($from));
210+
$function->setAttributes($this->formatAttributes($from->getAttributes()));
211211
$function->setReturnType((string) $from->getReturnType());
212212

213213
if ($withBody) {
@@ -262,7 +262,7 @@ public function fromParameterReflection(\ReflectionParameter $from): Parameter
262262
}
263263
}
264264

265-
$param->setAttributes($this->getAttributes($from));
265+
$param->setAttributes($this->formatAttributes($from->getAttributes()));
266266
return $param;
267267
}
268268

@@ -274,7 +274,7 @@ public function fromConstantReflection(\ReflectionClassConstant $from): Constant
274274
$const->setVisibility($this->getVisibility($from));
275275
$const->setFinal($from->isFinal());
276276
$const->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
277-
$const->setAttributes($this->getAttributes($from));
277+
$const->setAttributes($this->formatAttributes($from->getAttributes()));
278278
return $const;
279279
}
280280

@@ -284,7 +284,7 @@ public function fromCaseReflection(\ReflectionClassConstant $from): EnumCase
284284
$const = new EnumCase($from->name);
285285
$const->setValue($from->getValue()->value ?? null);
286286
$const->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
287-
$const->setAttributes($this->getAttributes($from));
287+
$const->setAttributes($this->formatAttributes($from->getAttributes()));
288288
return $const;
289289
}
290290

@@ -300,7 +300,7 @@ public function fromPropertyReflection(\ReflectionProperty $from): Property
300300
$prop->setInitialized($from->hasType() && array_key_exists($prop->getName(), $defaults));
301301
$prop->setReadOnly($from->isReadOnly());
302302
$prop->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
303-
$prop->setAttributes($this->getAttributes($from));
303+
$prop->setAttributes($this->formatAttributes($from->getAttributes()));
304304

305305
if (PHP_VERSION_ID >= 80400) {
306306
$this->addHooks($from, $prop);
@@ -344,7 +344,7 @@ private function addHooks(\ReflectionProperty $from, Property|PromotedParameter
344344
->setFinal($hook->isFinal())
345345
->setReturnReference($hook->returnsReference())
346346
->setComment(Helpers::unformatDocComment((string) $hook->getDocComment()))
347-
->setAttributes($this->getAttributes($hook));
347+
->setAttributes($this->formatAttributes($hook->getAttributes()));
348348
}
349349
}
350350

@@ -369,19 +369,23 @@ public function fromCode(string $code): PhpFile
369369
}
370370

371371

372-
/** @return list<Attribute> */
373-
private function getAttributes($from): array
372+
/**
373+
* @param list<\ReflectionAttribute<object>> $attrs
374+
* @return list<Attribute>
375+
*/
376+
private function formatAttributes(array $attrs): array
374377
{
375-
return array_map(function ($attr) {
378+
$res = [];
379+
foreach ($attrs as $attr) {
376380
$args = $attr->getArguments();
377381
foreach ($args as &$arg) {
378382
if (is_object($arg)) {
379383
$arg = $this->fromObject($arg);
380384
}
381385
}
382-
383-
return new Attribute($attr->getName(), $args);
384-
}, $from->getAttributes());
386+
$res[] = new Attribute($attr->getName(), $args);
387+
}
388+
return $res;
385389
}
386390

387391

0 commit comments

Comments
 (0)