DocBlock.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\DocBlock;
  13. use PhpCsFixer\Preg;
  14. use PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceAnalysis;
  15. use PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceUseAnalysis;
  16. /**
  17. * This class represents a docblock.
  18. *
  19. * It internally splits it up into "lines" that we can manipulate.
  20. *
  21. * @author Graham Campbell <graham@alt-three.com>
  22. *
  23. * @final
  24. */
  25. final class DocBlock
  26. {
  27. /**
  28. * The array of lines.
  29. *
  30. * @var Line[]
  31. */
  32. private $lines = [];
  33. /**
  34. * The array of annotations.
  35. *
  36. * @var null|Annotation[]
  37. */
  38. private $annotations;
  39. /**
  40. * @var null|NamespaceAnalysis
  41. */
  42. private $namespace;
  43. /**
  44. * @var NamespaceUseAnalysis[]
  45. */
  46. private $namespaceUses;
  47. public function __construct(string $content, ?NamespaceAnalysis $namespace = null, array $namespaceUses = [])
  48. {
  49. foreach (Preg::split('/([^\n\r]+\R*)/', $content, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $line) {
  50. $this->lines[] = new Line($line);
  51. }
  52. $this->namespace = $namespace;
  53. $this->namespaceUses = $namespaceUses;
  54. }
  55. /**
  56. * Get the string representation of object.
  57. */
  58. public function __toString(): string
  59. {
  60. return $this->getContent();
  61. }
  62. /**
  63. * Get this docblock's lines.
  64. *
  65. * @return Line[]
  66. */
  67. public function getLines(): array
  68. {
  69. return $this->lines;
  70. }
  71. /**
  72. * Get a single line.
  73. */
  74. public function getLine(int $pos): ?Line
  75. {
  76. return $this->lines[$pos] ?? null;
  77. }
  78. /**
  79. * Get this docblock's annotations.
  80. *
  81. * @return Annotation[]
  82. */
  83. public function getAnnotations(): array
  84. {
  85. if (null !== $this->annotations) {
  86. return $this->annotations;
  87. }
  88. $this->annotations = [];
  89. $total = \count($this->lines);
  90. for ($index = 0; $index < $total; ++$index) {
  91. if ($this->lines[$index]->containsATag()) {
  92. // get all the lines that make up the annotation
  93. $lines = \array_slice($this->lines, $index, $this->findAnnotationLength($index), true);
  94. $annotation = new Annotation($lines, $this->namespace, $this->namespaceUses);
  95. // move the index to the end of the annotation to avoid
  96. // checking it again because we know the lines inside the
  97. // current annotation cannot be part of another annotation
  98. $index = $annotation->getEnd();
  99. // add the current annotation to the list of annotations
  100. $this->annotations[] = $annotation;
  101. }
  102. }
  103. return $this->annotations;
  104. }
  105. public function isMultiLine(): bool
  106. {
  107. return 1 !== \count($this->lines);
  108. }
  109. /**
  110. * Take a one line doc block, and turn it into a multi line doc block.
  111. */
  112. public function makeMultiLine(string $indent, string $lineEnd): void
  113. {
  114. if ($this->isMultiLine()) {
  115. return;
  116. }
  117. $lineContent = $this->getSingleLineDocBlockEntry($this->lines[0]);
  118. if ('' === $lineContent) {
  119. $this->lines = [
  120. new Line('/**'.$lineEnd),
  121. new Line($indent.' *'.$lineEnd),
  122. new Line($indent.' */'),
  123. ];
  124. return;
  125. }
  126. $this->lines = [
  127. new Line('/**'.$lineEnd),
  128. new Line($indent.' * '.$lineContent.$lineEnd),
  129. new Line($indent.' */'),
  130. ];
  131. }
  132. public function makeSingleLine(): void
  133. {
  134. if (!$this->isMultiLine()) {
  135. return;
  136. }
  137. $usefulLines = array_filter(
  138. $this->lines,
  139. static function (Line $line) {
  140. return $line->containsUsefulContent();
  141. }
  142. );
  143. if (1 < \count($usefulLines)) {
  144. return;
  145. }
  146. $lineContent = '';
  147. if (\count($usefulLines)) {
  148. $lineContent = $this->getSingleLineDocBlockEntry(array_shift($usefulLines));
  149. }
  150. $this->lines = [new Line('/** '.$lineContent.' */')];
  151. }
  152. public function getAnnotation(int $pos): ?Annotation
  153. {
  154. $annotations = $this->getAnnotations();
  155. return $annotations[$pos] ?? null;
  156. }
  157. /**
  158. * Get specific types of annotations only.
  159. *
  160. * If none exist, we're returning an empty array.
  161. *
  162. * @param string|string[] $types
  163. *
  164. * @return Annotation[]
  165. */
  166. public function getAnnotationsOfType($types): array
  167. {
  168. $annotations = [];
  169. $types = (array) $types;
  170. foreach ($this->getAnnotations() as $annotation) {
  171. $tag = $annotation->getTag()->getName();
  172. foreach ($types as $type) {
  173. if ($type === $tag) {
  174. $annotations[] = $annotation;
  175. }
  176. }
  177. }
  178. return $annotations;
  179. }
  180. /**
  181. * Get the actual content of this docblock.
  182. */
  183. public function getContent(): string
  184. {
  185. return implode('', $this->lines);
  186. }
  187. private function findAnnotationLength(int $start): int
  188. {
  189. $index = $start;
  190. while ($line = $this->getLine(++$index)) {
  191. if ($line->containsATag()) {
  192. // we've 100% reached the end of the description if we get here
  193. break;
  194. }
  195. if (!$line->containsUsefulContent()) {
  196. // if next line is also non-useful, or contains a tag, then we're done here
  197. $next = $this->getLine($index + 1);
  198. if (null === $next || !$next->containsUsefulContent() || $next->containsATag()) {
  199. break;
  200. }
  201. // otherwise, continue, the annotation must have contained a blank line in its description
  202. }
  203. }
  204. return $index - $start;
  205. }
  206. private function getSingleLineDocBlockEntry(Line $line): string
  207. {
  208. $lineString = $line->getContent();
  209. if (0 === \strlen($lineString)) {
  210. return $lineString;
  211. }
  212. $lineString = str_replace('*/', '', $lineString);
  213. $lineString = trim($lineString);
  214. if ('/**' === substr($lineString, 0, 3)) {
  215. $lineString = substr($lineString, 3);
  216. } elseif ('*' === substr($lineString, 0, 1)) {
  217. $lineString = substr($lineString, 1);
  218. }
  219. return trim($lineString);
  220. }
  221. }