Skip to content

Commit

Permalink
Printer: allow customizing comment formatting through protected print…
Browse files Browse the repository at this point in the history
…DocComment (#118)
  • Loading branch information
Jeroeny authored and dg committed Oct 4, 2022
1 parent 22c9ed7 commit dc923d8
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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()))
Expand All @@ -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";
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -295,7 +295,7 @@ public function printFile(PhpFile $file): string
}

return "<?php\n"
. ($file->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);
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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()))
Expand Down

0 comments on commit dc923d8

Please sign in to comment.