From 690b00d81d42d5633e4457c43ef9754573b6f9d6 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sun, 12 May 2024 19:31:02 +0200 Subject: [PATCH] Printer: refactoring --- src/PhpGenerator/Printer.php | 64 +++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/src/PhpGenerator/Printer.php b/src/PhpGenerator/Printer.php index 4a1cf447..42e5cf73 100644 --- a/src/PhpGenerator/Printer.php +++ b/src/PhpGenerator/Printer.php @@ -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) { @@ -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); } } @@ -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) {