Skip to content

Commit a73c124

Browse files
Jeroenydg
authored andcommitted
Printer: allow customizing comment formatting through protected printDocComment (#118)
1 parent 22c9ed7 commit a73c124

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/PhpGenerator/Printer.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function printFunction(GlobalFunction $function, ?PhpNamespace $namespace
4848
$body = Helpers::simplifyTaggedNames($function->getBody(), $this->namespace);
4949
$body = ltrim(rtrim(Strings::normalize($body)) . "\n");
5050

51-
return Helpers::formatDocComment($function->getComment() . "\n")
51+
return $this->printDocComment($function)
5252
. self::printAttributes($function->getAttributes())
5353
. $line
5454
. $this->printParameters($function, strlen($line) + strlen($returnType) + 2) // 2 = parentheses
@@ -119,7 +119,7 @@ public function printMethod(Method $method, ?PhpNamespace $namespace = null, boo
119119
$body = ltrim(rtrim(Strings::normalize($body)) . "\n");
120120
$braceOnNextLine = $this->bracesOnNextLine && !str_contains($params, "\n");
121121

122-
return Helpers::formatDocComment($method->getComment() . "\n")
122+
return $this->printDocComment($method)
123123
. self::printAttributes($method->getAttributes())
124124
. $line
125125
. $params
@@ -144,7 +144,7 @@ public function printClass(
144144
if ($class instanceof ClassType || $class instanceof TraitType || $class instanceof EnumType) {
145145
foreach ($class->getTraits() as $trait) {
146146
$resolutions = $trait->getResolutions();
147-
$traits[] = Helpers::formatDocComment((string) $trait->getComment())
147+
$traits[] = $this->printDocComment($trait)
148148
. 'use ' . $resolver($trait->getName())
149149
. ($resolutions
150150
? " {\n" . $this->indentation . implode(";\n" . $this->indentation, $resolutions) . ";\n}\n"
@@ -158,7 +158,7 @@ public function printClass(
158158
$enumType = $class->getType();
159159
foreach ($class->getCases() as $case) {
160160
$enumType ??= is_scalar($case->getValue()) ? get_debug_type($case->getValue()) : null;
161-
$cases[] = Helpers::formatDocComment((string) $case->getComment())
161+
$cases[] = $this->printDocComment($case)
162162
. self::printAttributes($case->getAttributes())
163163
. 'case ' . $case->getName()
164164
. ($case->getValue() === null ? '' : ' = ' . $this->dump($case->getValue()))
@@ -173,7 +173,7 @@ public function printClass(
173173
. ($const->getVisibility() ? $const->getVisibility() . ' ' : '')
174174
. 'const ' . $const->getName() . ' = ';
175175

176-
$consts[] = Helpers::formatDocComment((string) $const->getComment())
176+
$consts[] = $this->printDocComment($const)
177177
. self::printAttributes($const->getAttributes())
178178
. $def
179179
. $this->dump($const->getValue(), strlen($def)) . ";\n";
@@ -204,7 +204,7 @@ public function printClass(
204204
. ltrim($this->printType($type, $property->isNullable()) . ' ')
205205
. '$' . $property->getName());
206206

207-
$properties[] = Helpers::formatDocComment((string) $property->getComment())
207+
$properties[] = $this->printDocComment($property)
208208
. self::printAttributes($property->getAttributes())
209209
. $def
210210
. ($property->getValue() === null && !$property->isInitialized()
@@ -242,7 +242,7 @@ public function printClass(
242242
: null;
243243
$line[] = $class->getName() ? null : '{';
244244

245-
return Helpers::formatDocComment($class->getComment() . "\n")
245+
return $this->printDocComment($class)
246246
. self::printAttributes($class->getAttributes())
247247
. implode(' ', array_filter($line))
248248
. ($class->getName() ? "\n{\n" : "\n")
@@ -295,7 +295,7 @@ public function printFile(PhpFile $file): string
295295
}
296296

297297
return "<?php\n"
298-
. ($file->getComment() ? "\n" . Helpers::formatDocComment($file->getComment() . "\n") : '')
298+
. ($file->getComment() ? "\n" . $this->printDocComment($file) : '')
299299
. "\n"
300300
. ($file->hasStrictTypes() ? "declare(strict_types=1);\n\n" : '')
301301
. implode("\n\n", $namespaces);
@@ -332,7 +332,7 @@ protected function printParameters(Closure|GlobalFunction|Method $function, int
332332
$type = $param->getType();
333333
$promoted = $param instanceof PromotedParameter ? $param : null;
334334
$params[] =
335-
($promoted ? Helpers::formatDocComment((string) $promoted->getComment()) : '')
335+
($promoted ? $this->printDocComment($promoted) : '')
336336
. ($attrs = self::printAttributes($param->getAttributes(), inline: true))
337337
. ($promoted ?
338338
($promoted->getVisibility() ?: 'public')
@@ -375,6 +375,16 @@ protected function printType(?string $type, bool $nullable): string
375375
}
376376

377377

378+
protected function printDocComment(/*Traits\CommentAware*/ $commentable): string
379+
{
380+
$multiLine = $commentable instanceof GlobalFunction
381+
|| $commentable instanceof Method
382+
|| $commentable instanceof ClassLike
383+
|| $commentable instanceof PhpFile;
384+
return Helpers::formatDocComment((string) $commentable->getComment(), $multiLine);
385+
}
386+
387+
378388
private function printReturnType(Closure|GlobalFunction|Method $function): string
379389
{
380390
return ($tmp = $this->printType($function->getReturnType(), $function->isReturnNullable()))

0 commit comments

Comments
 (0)