Line.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. /**
  15. * This represents a line of a docblock.
  16. *
  17. * @author Graham Campbell <hello@gjcampbell.co.uk>
  18. */
  19. final class Line
  20. {
  21. /**
  22. * The content of this line.
  23. */
  24. private string $content;
  25. /**
  26. * Create a new line instance.
  27. */
  28. public function __construct(string $content)
  29. {
  30. $this->content = $content;
  31. }
  32. /**
  33. * Get the string representation of object.
  34. */
  35. public function __toString(): string
  36. {
  37. return $this->content;
  38. }
  39. /**
  40. * Get the content of this line.
  41. */
  42. public function getContent(): string
  43. {
  44. return $this->content;
  45. }
  46. /**
  47. * Does this line contain useful content?
  48. *
  49. * If the line contains text or tags, then this is true.
  50. */
  51. public function containsUsefulContent(): bool
  52. {
  53. return Preg::match('/\*\s*\S+/', $this->content) && '' !== trim(str_replace(['/', '*'], ' ', $this->content));
  54. }
  55. /**
  56. * Does the line contain a tag?
  57. *
  58. * If this is true, then it must be the first line of an annotation.
  59. */
  60. public function containsATag(): bool
  61. {
  62. return Preg::match('/\*\s*@/', $this->content);
  63. }
  64. /**
  65. * Is the line the start of a docblock?
  66. */
  67. public function isTheStart(): bool
  68. {
  69. return str_contains($this->content, '/**');
  70. }
  71. /**
  72. * Is the line the end of a docblock?
  73. */
  74. public function isTheEnd(): bool
  75. {
  76. return str_contains($this->content, '*/');
  77. }
  78. /**
  79. * Set the content of this line.
  80. */
  81. public function setContent(string $content): void
  82. {
  83. $this->content = $content;
  84. }
  85. /**
  86. * Remove this line by clearing its contents.
  87. *
  88. * Note that this method technically brakes the internal state of the
  89. * docblock, but is useful when we need to retain the indices of lines
  90. * during the execution of an algorithm.
  91. */
  92. public function remove(): void
  93. {
  94. $this->content = '';
  95. }
  96. /**
  97. * Append a blank docblock line to this line's contents.
  98. *
  99. * Note that this method technically brakes the internal state of the
  100. * docblock, but is useful when we need to retain the indices of lines
  101. * during the execution of an algorithm.
  102. */
  103. public function addBlank(): void
  104. {
  105. $matched = Preg::match('/^(\h*\*)[^\r\n]*(\r?\n)$/', $this->content, $matches);
  106. if (!$matched) {
  107. return;
  108. }
  109. $this->content .= $matches[1].$matches[2];
  110. }
  111. }