Skip to content

Commit 690b00d

Browse files
committed
Printer: refactoring
1 parent ea9deed commit 690b00d

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed

src/PhpGenerator/Printer.php

+38-26
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,7 @@ public function printClass(
180180
|| $class instanceof EnumType
181181
) {
182182
foreach ($class->getConstants() as $const) {
183-
$def = ($const->isFinal() ? 'final ' : '')
184-
. ($const->getVisibility() ? $const->getVisibility() . ' ' : '')
185-
. 'const '
186-
. ltrim($this->printType($const->getType(), nullable: false) . ' ')
187-
. $const->getName() . ' = ';
188-
189-
$consts[] = $this->printDocComment($const)
190-
. $this->printAttributes($const->getAttributes())
191-
. $def
192-
. $this->dump($const->getValue(), strlen($def)) . ";\n";
183+
$consts[] = $this->printConstant($const);
193184
}
194185

195186
foreach ($class->getMethods() as $method) {
@@ -204,22 +195,7 @@ public function printClass(
204195
$properties = [];
205196
if ($class instanceof ClassType || $class instanceof TraitType) {
206197
foreach ($class->getProperties() as $property) {
207-
$property->validate();
208-
$type = $property->getType();
209-
$def = (($property->getVisibility() ?: 'public')
210-
. ($property->isStatic() ? ' static' : '')
211-
. (!$readOnlyClass && $property->isReadOnly() && $type ? ' readonly' : '')
212-
. ' '
213-
. ltrim($this->printType($type, $property->isNullable()) . ' ')
214-
. '$' . $property->getName());
215-
216-
$properties[] = $this->printDocComment($property)
217-
. $this->printAttributes($property->getAttributes())
218-
. $def
219-
. ($property->getValue() === null && !$property->isInitialized()
220-
? ''
221-
: ' = ' . $this->dump($property->getValue(), strlen($def) + 3)) // 3 = ' = '
222-
. ";\n";
198+
$properties[] = $this->printProperty($property, $readOnlyClass);
223199
}
224200
}
225201

@@ -380,6 +356,42 @@ private function formatParameters(Closure|GlobalFunction|Method $function, bool
380356
}
381357

382358

359+
private function printConstant(Constant $const): string
360+
{
361+
$def = ($const->isFinal() ? 'final ' : '')
362+
. ($const->getVisibility() ? $const->getVisibility() . ' ' : '')
363+
. 'const '
364+
. ltrim($this->printType($const->getType(), nullable: false) . ' ')
365+
. $const->getName() . ' = ';
366+
367+
return $this->printDocComment($const)
368+
. $this->printAttributes($const->getAttributes())
369+
. $def
370+
. $this->dump($const->getValue(), strlen($def)) . ";\n";
371+
}
372+
373+
374+
private function printProperty(Property $property, bool $readOnlyClass = false): string
375+
{
376+
$property->validate();
377+
$type = $property->getType();
378+
$def = (($property->getVisibility() ?: 'public')
379+
. ($property->isStatic() ? ' static' : '')
380+
. (!$readOnlyClass && $property->isReadOnly() && $type ? ' readonly' : '')
381+
. ' '
382+
. ltrim($this->printType($type, $property->isNullable()) . ' ')
383+
. '$' . $property->getName());
384+
385+
return $this->printDocComment($property)
386+
. $this->printAttributes($property->getAttributes())
387+
. $def
388+
. ($property->getValue() === null && !$property->isInitialized()
389+
? ''
390+
: ' = ' . $this->dump($property->getValue(), strlen($def) + 3)) // 3 = ' = '
391+
. ";\n";
392+
}
393+
394+
383395
protected function printType(?string $type, bool $nullable): string
384396
{
385397
if ($type === null) {

0 commit comments

Comments
 (0)