暂无描述

Serializer.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of phpDocumentor.
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. *
  9. * @link http://phpdoc.org
  10. */
  11. namespace phpDocumentor\Reflection\DocBlock;
  12. use phpDocumentor\Reflection\DocBlock;
  13. use phpDocumentor\Reflection\DocBlock\Tags\Formatter;
  14. use phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter;
  15. use function sprintf;
  16. use function str_repeat;
  17. use function str_replace;
  18. use function strlen;
  19. use function wordwrap;
  20. /**
  21. * Converts a DocBlock back from an object to a complete DocComment including Asterisks.
  22. */
  23. class Serializer
  24. {
  25. /** @var string The string to indent the comment with. */
  26. protected $indentString = ' ';
  27. /** @var int The number of times the indent string is repeated. */
  28. protected $indent = 0;
  29. /** @var bool Whether to indent the first line with the given indent amount and string. */
  30. protected $isFirstLineIndented = true;
  31. /** @var int|null The max length of a line. */
  32. protected $lineLength;
  33. /** @var Formatter A custom tag formatter. */
  34. protected $tagFormatter;
  35. /** @var string */
  36. private $lineEnding;
  37. /**
  38. * Create a Serializer instance.
  39. *
  40. * @param int $indent The number of times the indent string is repeated.
  41. * @param string $indentString The string to indent the comment with.
  42. * @param bool $indentFirstLine Whether to indent the first line.
  43. * @param int|null $lineLength The max length of a line or NULL to disable line wrapping.
  44. * @param Formatter $tagFormatter A custom tag formatter, defaults to PassthroughFormatter.
  45. * @param string $lineEnding Line ending used in the output, by default \n is used.
  46. */
  47. public function __construct(
  48. int $indent = 0,
  49. string $indentString = ' ',
  50. bool $indentFirstLine = true,
  51. ?int $lineLength = null,
  52. ?Formatter $tagFormatter = null,
  53. string $lineEnding = "\n"
  54. ) {
  55. $this->indent = $indent;
  56. $this->indentString = $indentString;
  57. $this->isFirstLineIndented = $indentFirstLine;
  58. $this->lineLength = $lineLength;
  59. $this->tagFormatter = $tagFormatter ?: new PassthroughFormatter();
  60. $this->lineEnding = $lineEnding;
  61. }
  62. /**
  63. * Generate a DocBlock comment.
  64. *
  65. * @param DocBlock $docblock The DocBlock to serialize.
  66. *
  67. * @return string The serialized doc block.
  68. */
  69. public function getDocComment(DocBlock $docblock): string
  70. {
  71. $indent = str_repeat($this->indentString, $this->indent);
  72. $firstIndent = $this->isFirstLineIndented ? $indent : '';
  73. // 3 === strlen(' * ')
  74. $wrapLength = $this->lineLength ? $this->lineLength - strlen($indent) - 3 : null;
  75. $text = $this->removeTrailingSpaces(
  76. $indent,
  77. $this->addAsterisksForEachLine(
  78. $indent,
  79. $this->getSummaryAndDescriptionTextBlock($docblock, $wrapLength)
  80. )
  81. );
  82. $comment = $firstIndent . "/**\n";
  83. if ($text) {
  84. $comment .= $indent . ' * ' . $text . "\n";
  85. $comment .= $indent . " *\n";
  86. }
  87. $comment = $this->addTagBlock($docblock, $wrapLength, $indent, $comment);
  88. return str_replace("\n", $this->lineEnding, $comment . $indent . ' */');
  89. }
  90. private function removeTrailingSpaces(string $indent, string $text): string
  91. {
  92. return str_replace(
  93. sprintf("\n%s * \n", $indent),
  94. sprintf("\n%s *\n", $indent),
  95. $text
  96. );
  97. }
  98. private function addAsterisksForEachLine(string $indent, string $text): string
  99. {
  100. return str_replace(
  101. "\n",
  102. sprintf("\n%s * ", $indent),
  103. $text
  104. );
  105. }
  106. private function getSummaryAndDescriptionTextBlock(DocBlock $docblock, ?int $wrapLength): string
  107. {
  108. $text = $docblock->getSummary() . ((string) $docblock->getDescription() ? "\n\n" . $docblock->getDescription()
  109. : '');
  110. if ($wrapLength !== null) {
  111. $text = wordwrap($text, $wrapLength);
  112. return $text;
  113. }
  114. return $text;
  115. }
  116. private function addTagBlock(DocBlock $docblock, ?int $wrapLength, string $indent, string $comment): string
  117. {
  118. foreach ($docblock->getTags() as $tag) {
  119. $tagText = $this->tagFormatter->format($tag);
  120. if ($wrapLength !== null) {
  121. $tagText = wordwrap($tagText, $wrapLength);
  122. }
  123. $tagText = str_replace(
  124. "\n",
  125. sprintf("\n%s * ", $indent),
  126. $tagText
  127. );
  128. $comment .= sprintf("%s * %s\n", $indent, $tagText);
  129. }
  130. return $comment;
  131. }
  132. }