Skip to content

Commit 7fe136b

Browse files
committed
Printer: refactoring
1 parent 5691cd2 commit 7fe136b

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

src/PhpGenerator/Printer.php

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,10 @@ public function printMethod(Method $method, ?PhpNamespace $namespace = null, boo
126126
}
127127

128128

129-
public function printClass(ClassLike $class, ?PhpNamespace $namespace = null): string
130-
{
129+
public function printClass(
130+
ClassType|InterfaceType|TraitType|EnumType $class,
131+
?PhpNamespace $namespace = null,
132+
): string {
131133
$this->namespace = $this->resolveTypes ? $namespace : null;
132134
$class->validate();
133135
$resolver = $this->namespace
@@ -161,11 +163,7 @@ public function printClass(ClassLike $class, ?PhpNamespace $namespace = null): s
161163
}
162164

163165
$consts = [];
164-
if (
165-
$class instanceof ClassType
166-
|| $class instanceof InterfaceType
167-
|| $class instanceof EnumType
168-
) {
166+
if ($class instanceof ClassType || $class instanceof InterfaceType || $class instanceof EnumType) {
169167
foreach ($class->getConstants() as $const) {
170168
$def = ($const->isFinal() ? 'final ' : '')
171169
. ($const->getVisibility() ? $const->getVisibility() . ' ' : '')
@@ -221,29 +219,32 @@ public function printClass(ClassLike $class, ?PhpNamespace $namespace = null): s
221219
. implode(str_repeat("\n", $this->linesBetweenMethods), $methods),
222220
]);
223221

224-
$type = match (true) {
225-
$class instanceof ClassType => $class->getType(),
226-
$class instanceof InterfaceType => 'interface',
227-
$class instanceof TraitType => 'trait',
228-
$class instanceof EnumType => 'enum',
229-
};
222+
if ($class instanceof ClassType) {
223+
$line[] = $class->isAbstract() ? 'abstract' : null;
224+
$line[] = $class->isFinal() ? 'final' : null;
225+
}
230226

231-
return Strings::normalize(
232-
Helpers::formatDocComment($class->getComment() . "\n")
227+
$line[] = match (true) {
228+
$class instanceof ClassType => $class->getName() ? $class->getType() . ' ' . $class->getName() : null,
229+
$class instanceof InterfaceType => 'interface ' . $class->getName(),
230+
$class instanceof TraitType => 'trait ' . $class->getName(),
231+
$class instanceof EnumType => 'enum ' . $class->getName() . ($enumType ? $this->returnTypeColon . $enumType : ''),
232+
};
233+
$line[] = ($class instanceof ClassType || $class instanceof InterfaceType) && $class->getExtends()
234+
? 'extends ' . implode(', ', array_map($resolver, (array) $class->getExtends()))
235+
: null;
236+
$line[] = ($class instanceof ClassType || $class instanceof EnumType) && $class->getImplements()
237+
? 'implements ' . implode(', ', array_map($resolver, $class->getImplements()))
238+
: null;
239+
$line[] = $class->getName() ? null : '{';
240+
241+
return Helpers::formatDocComment($class->getComment() . "\n")
233242
. self::printAttributes($class->getAttributes())
234-
. ($class instanceof ClassType && $class->isAbstract() ? 'abstract ' : '')
235-
. ($class instanceof ClassType && $class->isFinal() ? 'final ' : '')
236-
. ($class->getName() ? $type . ' ' . $class->getName() . ($enumType ? $this->returnTypeColon . $enumType : '') . ' ' : '')
237-
. (($class instanceof ClassType || $class instanceof InterfaceType) && $class->getExtends()
238-
? 'extends ' . implode(', ', array_map($resolver, (array) $class->getExtends())) . ' '
239-
: '')
240-
. (($class instanceof ClassType || $class instanceof EnumType) && $class->getImplements()
241-
? 'implements ' . implode(', ', array_map($resolver, $class->getImplements())) . ' '
242-
: '')
243-
. ($class->getName() ? "\n" : '') . "{\n"
243+
. implode(' ', array_filter($line))
244+
. ($class->getName() ? "\n{\n" : "\n")
244245
. ($members ? $this->indent(implode("\n", $members)) : '')
245-
. '}',
246-
) . ($class->getName() ? "\n" : '');
246+
. '}'
247+
. ($class->getName() ? "\n" : '');
247248
}
248249

249250

@@ -286,13 +287,11 @@ public function printFile(PhpFile $file): string
286287
$namespaces[] = $this->printNamespace($namespace);
287288
}
288289

289-
return Strings::normalize(
290-
"<?php\n"
290+
return "<?php\n"
291291
. ($file->getComment() ? "\n" . Helpers::formatDocComment($file->getComment() . "\n") : '')
292292
. "\n"
293293
. ($file->hasStrictTypes() ? "declare(strict_types=1);\n\n" : '')
294-
. implode("\n\n", $namespaces),
295-
) . "\n";
294+
. implode("\n\n", $namespaces);
296295
}
297296

298297

0 commit comments

Comments
 (0)