Skip to content

Commit

Permalink
Extractor: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 13, 2023
1 parent e9f713c commit 2d9e6a4
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/PhpGenerator/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ private function addPropertyToClass(ClassLike $class, Node\Stmt\Property $node):
$prop->setVisibility($this->toVisibility($node->flags));
$prop->setType($node->type ? $this->toPhp($node->type) : null);
if ($item->default) {
$prop->setValue(new Literal($this->getReformattedContents([$item->default], 1)));
$prop->setValue($this->formatValue($item->default, 1));
}

$prop->setReadOnly(method_exists($node, 'isReadonly') && $node->isReadonly());
Expand All @@ -337,8 +337,7 @@ private function addMethodToClass(ClassLike $class, Node\Stmt\ClassMethod $node)
private function addConstantToClass(ClassLike $class, Node\Stmt\ClassConst $node): void
{
foreach ($node->consts as $item) {
$value = $this->getReformattedContents([$item->value], 1);
$const = $class->addConstant($item->name->toString(), new Literal($value));
$const = $class->addConstant($item->name->toString(), $this->formatValue($item->value, 1));
$const->setVisibility($this->toVisibility($node->flags));
$const->setFinal(method_exists($node, 'isFinal') && $node->isFinal());
$this->addCommentAndAttributes($const, $node);
Expand All @@ -351,7 +350,7 @@ private function addEnumCaseToClass(EnumType $class, Node\Stmt\EnumCase $node):
$value = match (true) {
$node->expr === null => null,
$node->expr instanceof Node\Scalar\LNumber, $node->expr instanceof Node\Scalar\String_ => $node->expr->value,
default => new Literal($this->getReformattedContents([$node->expr], 1)),
default => $this->formatValue($node->expr, 1),
};
$case = $class->addCase($node->name->toString(), $value);
$this->addCommentAndAttributes($case, $node);
Expand All @@ -371,7 +370,7 @@ private function addCommentAndAttributes($element, Node $node): void
foreach ($group->attrs as $attribute) {
$args = [];
foreach ($attribute->args as $arg) {
$value = new Literal($this->getReformattedContents([$arg->value], 0));
$value = $this->formatValue($arg->value, 0);
if ($arg->name) {
$args[$arg->name->toString()] = $value;
} else {
Expand Down Expand Up @@ -399,7 +398,7 @@ private function setupFunction(GlobalFunction|Method $function, Node\FunctionLik
$param->setReference($item->byRef);
$function->setVariadic($item->variadic);
if ($item->default) {
$param->setDefaultValue(new Literal($this->getReformattedContents([$item->default], 2)));
$param->setDefaultValue($this->formatValue($item->default, 2));
}

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


private function formatValue(Node\Expr $value, int $level): Literal
{
$value = $this->getReformattedContents([$value], $level);
return new Literal($value);
}


private function toVisibility(int $flags): ?string
{
return match (true) {
Expand Down

0 comments on commit 2d9e6a4

Please sign in to comment.