From 1be90092b20b6eaad7aeea55d36b21d8bdb8bc73 Mon Sep 17 00:00:00 2001 From: Jeroeny Date: Mon, 3 Oct 2022 14:15:09 +0200 Subject: [PATCH] allow customizing comment formatting through protected printDocComment --- src/PhpGenerator/Printer.php | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/PhpGenerator/Printer.php b/src/PhpGenerator/Printer.php index 3a015a98..3daf4564 100644 --- a/src/PhpGenerator/Printer.php +++ b/src/PhpGenerator/Printer.php @@ -10,6 +10,7 @@ namespace Nette\PhpGenerator; use Nette; +use Nette\PhpGenerator\Traits\CommentAware; use Nette\Utils\Strings; @@ -48,7 +49,7 @@ public function printFunction(GlobalFunction $function, ?PhpNamespace $namespace $body = Helpers::simplifyTaggedNames($function->getBody(), $this->namespace); $body = ltrim(rtrim(Strings::normalize($body)) . "\n"); - return Helpers::formatDocComment($function->getComment() . "\n") + return $this->printDocComment($function) . self::printAttributes($function->getAttributes()) . $line . $this->printParameters($function, strlen($line) + strlen($returnType) + 2) // 2 = parentheses @@ -119,7 +120,7 @@ public function printMethod(Method $method, ?PhpNamespace $namespace = null, boo $body = ltrim(rtrim(Strings::normalize($body)) . "\n"); $braceOnNextLine = $this->bracesOnNextLine && !str_contains($params, "\n"); - return Helpers::formatDocComment($method->getComment() . "\n") + return $this->printDocComment($method) . self::printAttributes($method->getAttributes()) . $line . $params @@ -144,7 +145,7 @@ public function printClass( if ($class instanceof ClassType || $class instanceof TraitType || $class instanceof EnumType) { foreach ($class->getTraits() as $trait) { $resolutions = $trait->getResolutions(); - $traits[] = Helpers::formatDocComment((string) $trait->getComment()) + $traits[] = $this->printDocComment($trait) . 'use ' . $resolver($trait->getName()) . ($resolutions ? " {\n" . $this->indentation . implode(";\n" . $this->indentation, $resolutions) . ";\n}\n" @@ -158,7 +159,7 @@ public function printClass( $enumType = $class->getType(); foreach ($class->getCases() as $case) { $enumType ??= is_scalar($case->getValue()) ? get_debug_type($case->getValue()) : null; - $cases[] = Helpers::formatDocComment((string) $case->getComment()) + $cases[] = $this->printDocComment($case) . self::printAttributes($case->getAttributes()) . 'case ' . $case->getName() . ($case->getValue() === null ? '' : ' = ' . $this->dump($case->getValue())) @@ -173,7 +174,7 @@ public function printClass( . ($const->getVisibility() ? $const->getVisibility() . ' ' : '') . 'const ' . $const->getName() . ' = '; - $consts[] = Helpers::formatDocComment((string) $const->getComment()) + $consts[] = $this->printDocComment($const) . self::printAttributes($const->getAttributes()) . $def . $this->dump($const->getValue(), strlen($def)) . ";\n"; @@ -204,7 +205,7 @@ public function printClass( . ltrim($this->printType($type, $property->isNullable()) . ' ') . '$' . $property->getName()); - $properties[] = Helpers::formatDocComment((string) $property->getComment()) + $properties[] = $this->printDocComment($property) . self::printAttributes($property->getAttributes()) . $def . ($property->getValue() === null && !$property->isInitialized() @@ -242,7 +243,7 @@ public function printClass( : null; $line[] = $class->getName() ? null : '{'; - return Helpers::formatDocComment($class->getComment() . "\n") + return $this->printDocComment($class) . self::printAttributes($class->getAttributes()) . implode(' ', array_filter($line)) . ($class->getName() ? "\n{\n" : "\n") @@ -295,7 +296,7 @@ public function printFile(PhpFile $file): string } return "getComment() ? "\n" . Helpers::formatDocComment($file->getComment() . "\n") : '') + . ($file->getComment() ? "\n" . $this->printDocComment($file) : '') . "\n" . ($file->hasStrictTypes() ? "declare(strict_types=1);\n\n" : '') . implode("\n\n", $namespaces); @@ -332,7 +333,7 @@ protected function printParameters(Closure|GlobalFunction|Method $function, int $type = $param->getType(); $promoted = $param instanceof PromotedParameter ? $param : null; $params[] = - ($promoted ? Helpers::formatDocComment((string) $promoted->getComment()) : '') + ($promoted ? $this->printDocComment($promoted, false) : '') . ($attrs = self::printAttributes($param->getAttributes(), inline: true)) . ($promoted ? ($promoted->getVisibility() ?: 'public') @@ -374,6 +375,11 @@ protected function printType(?string $type, bool $nullable): string return $type; } + /** @param CommentAware $commentable */ + protected function printDocComment($commentable, bool $newline = true): string + { + return ($commentable instanceof PhpFile && $commentable->getComment() ? "\n" : '') . Helpers::formatDocComment($commentable->getComment() . ($commentable->getComment() && $newline ? "\n" : '')); + } private function printReturnType(Closure|GlobalFunction|Method $function): string {