From dc923d84dc27a314321039708ce9b5f02ec4aaa5 Mon Sep 17 00:00:00 2001 From: Jeroen <1517978+Jeroeny@users.noreply.github.com> Date: Tue, 4 Oct 2022 01:00:56 +0200 Subject: [PATCH] Printer: allow customizing comment formatting through protected printDocComment (#118) --- src/PhpGenerator/Printer.php | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/PhpGenerator/Printer.php b/src/PhpGenerator/Printer.php index 3a015a98..7b2852b1 100644 --- a/src/PhpGenerator/Printer.php +++ b/src/PhpGenerator/Printer.php @@ -48,7 +48,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 +119,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 +144,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 +158,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 +173,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 +204,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 +242,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 +295,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 +332,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) : '') . ($attrs = self::printAttributes($param->getAttributes(), inline: true)) . ($promoted ? ($promoted->getVisibility() ?: 'public') @@ -375,6 +375,17 @@ protected function printType(?string $type, bool $nullable): string } + protected function printDocComment( + GlobalFunction|Method|ClassLike|Constant|EnumCase|PhpFile|PromotedParameter|Property|TraitUse $commentable, + ): string { + $forceMultiLine = $commentable instanceof GlobalFunction + || $commentable instanceof Method + || $commentable instanceof ClassLike + || $commentable instanceof PhpFile; + return Helpers::formatDocComment((string) $commentable->getComment(), $forceMultiLine); + } + + private function printReturnType(Closure|GlobalFunction|Method $function): string { return ($tmp = $this->printType($function->getReturnType(), $function->isReturnNullable()))