Debian Codegraph @

lib/3rdParty/composer/phpdocumentor/reflection-docblock/src/DocBlock/Serializer.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 /**
6 * This file is part of phpDocumentor.
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 *
11 * @link http://phpdoc.org
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 * Converts a DocBlock back from an object to a complete DocComment including Asterisks.
28 */
29 class Serializer
30 {
31 /** @var string The string to indent the comment with. */
32 protected string $indentString = ' ';
33
34 /** @var int The number of times the indent string is repeated. */
35 protected int $indent = 0;
36
37 /** @var bool Whether to indent the first line with the given indent amount and string. */
38 protected bool $isFirstLineIndented = true;
39
40 /** @var int|null The max length of a line. */
41 protected ?int $lineLength = null;
42
43 /** @var Formatter A custom tag formatter. */
44 protected Formatter $tagFormatter;
45 private string $lineEnding;
46
47 /**
48 * Create a Serializer instance.
49 *
50 * @param int $indent The number of times the indent string is repeated.
51 * @param string $indentString The string to indent the comment with.
52 * @param bool $indentFirstLine Whether to indent the first line.
53 * @param int|null $lineLength The max length of a line or NULL to disable line wrapping.
54 * @param Formatter $tagFormatter A custom tag formatter, defaults to PassthroughFormatter.
55 * @param string $lineEnding Line ending used in the output, by default \n is used.
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 * Generate a DocBlock comment.
75 *
76 * @param DocBlock $docblock The DocBlock to serialize.
77 *
78 * @return string The serialized doc block.
79 */
80 public function getDocComment(DocBlock $docblock): string
81 {
82 $indent = str_repeat($this->indentString, $this->indent);
83 $firstIndent = $this->isFirstLineIndented ? $indent : '';
84 // 3 === strlen(' * ')
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 }