Line.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\DocBlock;
  12. use PhpCsFixer\Preg;
  13. /**
  14. * This represents a line of a docblock.
  15. *
  16. * @author Graham Campbell <graham@alt-three.com>
  17. *
  18. * @final
  19. */
  20. class Line
  21. {
  22. /**
  23. * The content of this line.
  24. *
  25. * @var string
  26. */
  27. private $content;
  28. /**
  29. * Create a new line instance.
  30. *
  31. * @param string $content
  32. */
  33. public function __construct($content)
  34. {
  35. $this->content = $content;
  36. }
  37. /**
  38. * Get the string representation of object.
  39. *
  40. * @return string
  41. */
  42. public function __toString()
  43. {
  44. return $this->content;
  45. }
  46. /**
  47. * Get the content of this line.
  48. *
  49. * @return string
  50. */
  51. public function getContent()
  52. {
  53. return $this->content;
  54. }
  55. /**
  56. * Does this line contain useful content?
  57. *
  58. * If the line contains text or tags, then this is true.
  59. *
  60. * @return bool
  61. */
  62. public function containsUsefulContent()
  63. {
  64. return 0 !== Preg::match('/\\*\s*\S+/', $this->content) && '' !== trim(str_replace(['/', '*'], ' ', $this->content));
  65. }
  66. /**
  67. * Does the line contain a tag?
  68. *
  69. * If this is true, then it must be the first line of an annotation.
  70. *
  71. * @return bool
  72. */
  73. public function containsATag()
  74. {
  75. return 0 !== Preg::match('/\\*\s*@/', $this->content);
  76. }
  77. /**
  78. * Is the line the start of a docblock?
  79. *
  80. * @return bool
  81. */
  82. public function isTheStart()
  83. {
  84. return false !== strpos($this->content, '/**');
  85. }
  86. /**
  87. * Is the line the end of a docblock?
  88. *
  89. * @return bool
  90. */
  91. public function isTheEnd()
  92. {
  93. return false !== strpos($this->content, '*/');
  94. }
  95. /**
  96. * Set the content of this line.
  97. *
  98. * @param string $content
  99. */
  100. public function setContent($content)
  101. {
  102. $this->content = $content;
  103. }
  104. /**
  105. * Remove this line by clearing its contents.
  106. *
  107. * Note that this method technically brakes the internal state of the
  108. * docblock, but is useful when we need to retain the indexes of lines
  109. * during the execution of an algorithm.
  110. */
  111. public function remove()
  112. {
  113. $this->content = '';
  114. }
  115. /**
  116. * Append a blank docblock line to this line's contents.
  117. *
  118. * Note that this method technically brakes the internal state of the
  119. * docblock, but is useful when we need to retain the indexes of lines
  120. * during the execution of an algorithm.
  121. */
  122. public function addBlank()
  123. {
  124. $matched = Preg::match('/^(\h*\*)[^\r\n]*(\r?\n)$/', $this->content, $matches);
  125. if (1 !== $matched) {
  126. return;
  127. }
  128. $this->content .= $matches[1].$matches[2];
  129. }
  130. }