Line.php 2.8 KB

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