Skip to content

Commit 2d9e6a4

Browse files
committed
Extractor: refactoring
1 parent e9f713c commit 2d9e6a4

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/PhpGenerator/Extractor.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ private function addPropertyToClass(ClassLike $class, Node\Stmt\Property $node):
314314
$prop->setVisibility($this->toVisibility($node->flags));
315315
$prop->setType($node->type ? $this->toPhp($node->type) : null);
316316
if ($item->default) {
317-
$prop->setValue(new Literal($this->getReformattedContents([$item->default], 1)));
317+
$prop->setValue($this->formatValue($item->default, 1));
318318
}
319319

320320
$prop->setReadOnly(method_exists($node, 'isReadonly') && $node->isReadonly());
@@ -337,8 +337,7 @@ private function addMethodToClass(ClassLike $class, Node\Stmt\ClassMethod $node)
337337
private function addConstantToClass(ClassLike $class, Node\Stmt\ClassConst $node): void
338338
{
339339
foreach ($node->consts as $item) {
340-
$value = $this->getReformattedContents([$item->value], 1);
341-
$const = $class->addConstant($item->name->toString(), new Literal($value));
340+
$const = $class->addConstant($item->name->toString(), $this->formatValue($item->value, 1));
342341
$const->setVisibility($this->toVisibility($node->flags));
343342
$const->setFinal(method_exists($node, 'isFinal') && $node->isFinal());
344343
$this->addCommentAndAttributes($const, $node);
@@ -351,7 +350,7 @@ private function addEnumCaseToClass(EnumType $class, Node\Stmt\EnumCase $node):
351350
$value = match (true) {
352351
$node->expr === null => null,
353352
$node->expr instanceof Node\Scalar\LNumber, $node->expr instanceof Node\Scalar\String_ => $node->expr->value,
354-
default => new Literal($this->getReformattedContents([$node->expr], 1)),
353+
default => $this->formatValue($node->expr, 1),
355354
};
356355
$case = $class->addCase($node->name->toString(), $value);
357356
$this->addCommentAndAttributes($case, $node);
@@ -371,7 +370,7 @@ private function addCommentAndAttributes($element, Node $node): void
371370
foreach ($group->attrs as $attribute) {
372371
$args = [];
373372
foreach ($attribute->args as $arg) {
374-
$value = new Literal($this->getReformattedContents([$arg->value], 0));
373+
$value = $this->formatValue($arg->value, 0);
375374
if ($arg->name) {
376375
$args[$arg->name->toString()] = $value;
377376
} else {
@@ -399,7 +398,7 @@ private function setupFunction(GlobalFunction|Method $function, Node\FunctionLik
399398
$param->setReference($item->byRef);
400399
$function->setVariadic($item->variadic);
401400
if ($item->default) {
402-
$param->setDefaultValue(new Literal($this->getReformattedContents([$item->default], 2)));
401+
$param->setDefaultValue($this->formatValue($item->default, 2));
403402
}
404403

405404
$this->addCommentAndAttributes($param, $item);
@@ -412,6 +411,13 @@ private function setupFunction(GlobalFunction|Method $function, Node\FunctionLik
412411
}
413412

414413

414+
private function formatValue(Node\Expr $value, int $level): Literal
415+
{
416+
$value = $this->getReformattedContents([$value], $level);
417+
return new Literal($value);
418+
}
419+
420+
415421
private function toVisibility(int $flags): ?string
416422
{
417423
return match (true) {

0 commit comments

Comments
 (0)