Skip to content

Commit c175240

Browse files
authored
allow customizing comment formatting through protected printDocComment (#118)
1 parent 5860941 commit c175240

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/PhpGenerator/Printer.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Nette\PhpGenerator;
1111

1212
use Nette;
13+
use Nette\PhpGenerator\Traits\CommentAware;
1314
use Nette\Utils\Strings;
1415

1516

@@ -48,7 +49,7 @@ public function printFunction(GlobalFunction $function, ?PhpNamespace $namespace
4849
$body = Helpers::simplifyTaggedNames($function->getBody(), $this->namespace);
4950
$body = ltrim(rtrim(Strings::normalize($body)) . "\n");
5051

51-
return Helpers::formatDocComment($function->getComment() . "\n")
52+
return $this->printDocComment($function)
5253
. self::printAttributes($function->getAttributes())
5354
. $line
5455
. $this->printParameters($function, strlen($line) + strlen($returnType) + 2) // 2 = parentheses
@@ -119,7 +120,7 @@ public function printMethod(Method $method, ?PhpNamespace $namespace = null, boo
119120
$body = ltrim(rtrim(Strings::normalize($body)) . "\n");
120121
$braceOnNextLine = $this->bracesOnNextLine && !str_contains($params, "\n");
121122

122-
return Helpers::formatDocComment($method->getComment() . "\n")
123+
return $this->printDocComment($method)
123124
. self::printAttributes($method->getAttributes())
124125
. $line
125126
. $params
@@ -144,7 +145,7 @@ public function printClass(
144145
if ($class instanceof ClassType || $class instanceof TraitType || $class instanceof EnumType) {
145146
foreach ($class->getTraits() as $trait) {
146147
$resolutions = $trait->getResolutions();
147-
$traits[] = Helpers::formatDocComment((string) $trait->getComment())
148+
$traits[] = $this->printDocComment($trait)
148149
. 'use ' . $resolver($trait->getName())
149150
. ($resolutions
150151
? " {\n" . $this->indentation . implode(";\n" . $this->indentation, $resolutions) . ";\n}\n"
@@ -158,7 +159,7 @@ public function printClass(
158159
$enumType = $class->getType();
159160
foreach ($class->getCases() as $case) {
160161
$enumType ??= is_scalar($case->getValue()) ? get_debug_type($case->getValue()) : null;
161-
$cases[] = Helpers::formatDocComment((string) $case->getComment())
162+
$cases[] = $this->printDocComment($case)
162163
. self::printAttributes($case->getAttributes())
163164
. 'case ' . $case->getName()
164165
. ($case->getValue() === null ? '' : ' = ' . $this->dump($case->getValue()))
@@ -173,7 +174,7 @@ public function printClass(
173174
. ($const->getVisibility() ? $const->getVisibility() . ' ' : '')
174175
. 'const ' . $const->getName() . ' = ';
175176

176-
$consts[] = Helpers::formatDocComment((string) $const->getComment())
177+
$consts[] = $this->printDocComment($const)
177178
. self::printAttributes($const->getAttributes())
178179
. $def
179180
. $this->dump($const->getValue(), strlen($def)) . ";\n";
@@ -204,7 +205,7 @@ public function printClass(
204205
. ltrim($this->printType($type, $property->isNullable()) . ' ')
205206
. '$' . $property->getName());
206207

207-
$properties[] = Helpers::formatDocComment((string) $property->getComment())
208+
$properties[] = $this->printDocComment($property)
208209
. self::printAttributes($property->getAttributes())
209210
. $def
210211
. ($property->getValue() === null && !$property->isInitialized()
@@ -242,7 +243,7 @@ public function printClass(
242243
: null;
243244
$line[] = $class->getName() ? null : '{';
244245

245-
return Helpers::formatDocComment($class->getComment() . "\n")
246+
return $this->printDocComment($class)
246247
. self::printAttributes($class->getAttributes())
247248
. implode(' ', array_filter($line))
248249
. ($class->getName() ? "\n{\n" : "\n")
@@ -295,7 +296,7 @@ public function printFile(PhpFile $file): string
295296
}
296297

297298
return "<?php\n"
298-
. ($file->getComment() ? "\n" . Helpers::formatDocComment($file->getComment() . "\n") : '')
299+
. ($file->getComment() ? "\n" . $this->printDocComment($file) : '')
299300
. "\n"
300301
. ($file->hasStrictTypes() ? "declare(strict_types=1);\n\n" : '')
301302
. implode("\n\n", $namespaces);
@@ -332,7 +333,7 @@ protected function printParameters(Closure|GlobalFunction|Method $function, int
332333
$type = $param->getType();
333334
$promoted = $param instanceof PromotedParameter ? $param : null;
334335
$params[] =
335-
($promoted ? Helpers::formatDocComment((string) $promoted->getComment()) : '')
336+
($promoted ? $this->printDocComment($promoted, false) : '')
336337
. ($attrs = self::printAttributes($param->getAttributes(), inline: true))
337338
. ($promoted ?
338339
($promoted->getVisibility() ?: 'public')
@@ -374,6 +375,11 @@ protected function printType(?string $type, bool $nullable): string
374375
return $type;
375376
}
376377

378+
/** @param CommentAware $commentable */
379+
protected function printDocComment($commentable, bool $newline = true): string
380+
{
381+
return ($commentable instanceof PhpFile && $commentable->getComment() ? "\n" : '') . Helpers::formatDocComment($commentable->getComment() . ($commentable->getComment() && $newline ? "\n" : ''));
382+
}
377383

378384
private function printReturnType(Closure|GlobalFunction|Method $function): string
379385
{

0 commit comments

Comments
 (0)