Skip to content

Commit

Permalink
allow customizing comment formatting through protected printDocComment (
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroeny authored Oct 3, 2022
1 parent 5860941 commit c175240
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Nette\PhpGenerator;

use Nette;
use Nette\PhpGenerator\Traits\CommentAware;
use Nette\Utils\Strings;


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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()))
Expand All @@ -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";
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -295,7 +296,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 +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')
Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit c175240

Please sign in to comment.