Skip to content

Commit

Permalink
Printer: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed May 12, 2024
1 parent ea9deed commit 690b00d
Showing 1 changed file with 38 additions and 26 deletions.
64 changes: 38 additions & 26 deletions src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,7 @@ public function printClass(
|| $class instanceof EnumType
) {
foreach ($class->getConstants() as $const) {
$def = ($const->isFinal() ? 'final ' : '')
. ($const->getVisibility() ? $const->getVisibility() . ' ' : '')
. 'const '
. ltrim($this->printType($const->getType(), nullable: false) . ' ')
. $const->getName() . ' = ';

$consts[] = $this->printDocComment($const)
. $this->printAttributes($const->getAttributes())
. $def
. $this->dump($const->getValue(), strlen($def)) . ";\n";
$consts[] = $this->printConstant($const);
}

foreach ($class->getMethods() as $method) {
Expand All @@ -204,22 +195,7 @@ public function printClass(
$properties = [];
if ($class instanceof ClassType || $class instanceof TraitType) {
foreach ($class->getProperties() as $property) {
$property->validate();
$type = $property->getType();
$def = (($property->getVisibility() ?: 'public')
. ($property->isStatic() ? ' static' : '')
. (!$readOnlyClass && $property->isReadOnly() && $type ? ' readonly' : '')
. ' '
. ltrim($this->printType($type, $property->isNullable()) . ' ')
. '$' . $property->getName());

$properties[] = $this->printDocComment($property)
. $this->printAttributes($property->getAttributes())
. $def
. ($property->getValue() === null && !$property->isInitialized()
? ''
: ' = ' . $this->dump($property->getValue(), strlen($def) + 3)) // 3 = ' = '
. ";\n";
$properties[] = $this->printProperty($property, $readOnlyClass);
}
}

Expand Down Expand Up @@ -380,6 +356,42 @@ private function formatParameters(Closure|GlobalFunction|Method $function, bool
}


private function printConstant(Constant $const): string
{
$def = ($const->isFinal() ? 'final ' : '')
. ($const->getVisibility() ? $const->getVisibility() . ' ' : '')
. 'const '
. ltrim($this->printType($const->getType(), nullable: false) . ' ')
. $const->getName() . ' = ';

return $this->printDocComment($const)
. $this->printAttributes($const->getAttributes())
. $def
. $this->dump($const->getValue(), strlen($def)) . ";\n";
}


private function printProperty(Property $property, bool $readOnlyClass = false): string
{
$property->validate();
$type = $property->getType();
$def = (($property->getVisibility() ?: 'public')
. ($property->isStatic() ? ' static' : '')
. (!$readOnlyClass && $property->isReadOnly() && $type ? ' readonly' : '')
. ' '
. ltrim($this->printType($type, $property->isNullable()) . ' ')
. '$' . $property->getName());

return $this->printDocComment($property)
. $this->printAttributes($property->getAttributes())
. $def
. ($property->getValue() === null && !$property->isInitialized()
? ''
: ' = ' . $this->dump($property->getValue(), strlen($def) + 3)) // 3 = ' = '
. ";\n";
}


protected function printType(?string $type, bool $nullable): string
{
if ($type === null) {
Expand Down

0 comments on commit 690b00d

Please sign in to comment.