| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
namespace phpDocumentor\Reflection\DocBlock; |
| 15 |
|
| 16 |
use phpDocumentor\Reflection\DocBlock; |
| 17 |
use phpDocumentor\Reflection\DocBlock\Tags\Formatter; |
| 18 |
use phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter; |
| 19 |
|
| 20 |
use function sprintf; |
| 21 |
use function str_repeat; |
| 22 |
use function str_replace; |
| 23 |
use function strlen; |
| 24 |
use function wordwrap; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class Serializer |
| 30 |
{ |
| 31 |
|
| 32 |
protected string $indentString = ' '; |
| 33 |
|
| 34 |
|
| 35 |
protected int $indent = 0; |
| 36 |
|
| 37 |
|
| 38 |
protected bool $isFirstLineIndented = true; |
| 39 |
|
| 40 |
|
| 41 |
protected ?int $lineLength = null; |
| 42 |
|
| 43 |
|
| 44 |
protected Formatter $tagFormatter; |
| 45 |
private string $lineEnding; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function __construct( |
| 58 |
int $indent = 0, |
| 59 |
string $indentString = ' ', |
| 60 |
bool $indentFirstLine = true, |
| 61 |
?int $lineLength = null, |
| 62 |
?Formatter $tagFormatter = null, |
| 63 |
string $lineEnding = "\n" |
| 64 |
) { |
| 65 |
$this->indent = $indent; |
| 66 |
$this->indentString = $indentString; |
| 67 |
$this->isFirstLineIndented = $indentFirstLine; |
| 68 |
$this->lineLength = $lineLength; |
| 69 |
$this->tagFormatter = $tagFormatter ?: new PassthroughFormatter(); |
| 70 |
$this->lineEnding = $lineEnding; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function getDocComment(DocBlock $docblock): string |
| 81 |
{ |
| 82 |
$indent = str_repeat($this->indentString, $this->indent); |
| 83 |
$firstIndent = $this->isFirstLineIndented ? $indent : ''; |
| 84 |
|
| 85 |
$wrapLength = $this->lineLength !== null ? $this->lineLength - strlen($indent) - 3 : null; |
| 86 |
|
| 87 |
$text = $this->removeTrailingSpaces( |
| 88 |
$indent, |
| 89 |
$this->addAsterisksForEachLine( |
| 90 |
$indent, |
| 91 |
$this->getSummaryAndDescriptionTextBlock($docblock, $wrapLength) |
| 92 |
) |
| 93 |
); |
| 94 |
|
| 95 |
$comment = $firstIndent . "/**\n"; |
| 96 |
if ($text) { |
| 97 |
$comment .= $indent . ' * ' . $text . "\n"; |
| 98 |
$comment .= $indent . " *\n"; |
| 99 |
} |
| 100 |
|
| 101 |
$comment = $this->addTagBlock($docblock, $wrapLength, $indent, $comment); |
| 102 |
|
| 103 |
return str_replace("\n", $this->lineEnding, $comment . $indent . ' */'); |
| 104 |
} |
| 105 |
|
| 106 |
private function removeTrailingSpaces(string $indent, string $text): string |
| 107 |
{ |
| 108 |
return str_replace( |
| 109 |
sprintf("\n%s * \n", $indent), |
| 110 |
sprintf("\n%s *\n", $indent), |
| 111 |
$text |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
private function addAsterisksForEachLine(string $indent, string $text): string |
| 116 |
{ |
| 117 |
return str_replace( |
| 118 |
"\n", |
| 119 |
sprintf("\n%s * ", $indent), |
| 120 |
$text |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
private function getSummaryAndDescriptionTextBlock(DocBlock $docblock, ?int $wrapLength): string |
| 125 |
{ |
| 126 |
$text = $docblock->getSummary() . ((string) $docblock->getDescription() ? "\n\n" . $docblock->getDescription() |
| 127 |
: ''); |
| 128 |
if ($wrapLength !== null) { |
| 129 |
$text = wordwrap($text, $wrapLength); |
| 130 |
|
| 131 |
return $text; |
| 132 |
} |
| 133 |
|
| 134 |
return $text; |
| 135 |
} |
| 136 |
|
| 137 |
private function addTagBlock(DocBlock $docblock, ?int $wrapLength, string $indent, string $comment): string |
| 138 |
{ |
| 139 |
foreach ($docblock->getTags() as $tag) { |
| 140 |
$tagText = $this->tagFormatter->format($tag); |
| 141 |
if ($wrapLength !== null) { |
| 142 |
$tagText = wordwrap($tagText, $wrapLength); |
| 143 |
} |
| 144 |
|
| 145 |
$tagText = str_replace( |
| 146 |
"\n", |
| 147 |
sprintf("\n%s * ", $indent), |
| 148 |
$tagText |
| 149 |
); |
| 150 |
|
| 151 |
$comment .= sprintf("%s * %s\n", $indent, $tagText); |
| 152 |
} |
| 153 |
|
| 154 |
return $comment; |
| 155 |
} |
| 156 |
} |