Annotation.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 an entire annotation from a docblock.
  13. *
  14. * @author Graham Campbell <graham@mineuk.com>
  15. */
  16. class Annotation
  17. {
  18. /**
  19. * The lines that make up the annotation.
  20. *
  21. * Note that the array indexes represent the position in the docblock.
  22. *
  23. * @var Lines[]
  24. */
  25. private $lines;
  26. /**
  27. * The associated tag.
  28. *
  29. * @var Tag|null
  30. */
  31. private $tag;
  32. /**
  33. * Create a new line instance.
  34. *
  35. * @param Lines[] $lines
  36. */
  37. public function __construct(array $lines)
  38. {
  39. $this->lines = $lines;
  40. }
  41. /**
  42. * Get the start position of this annotation.
  43. *
  44. * @return int
  45. */
  46. public function getStart()
  47. {
  48. $keys = array_keys($this->lines);
  49. return $keys[0];
  50. }
  51. /**
  52. * Get the end position of this annotation.
  53. *
  54. * @return int
  55. */
  56. public function getEnd()
  57. {
  58. $keys = array_keys($this->lines);
  59. return end($keys);
  60. }
  61. /**
  62. * Get the associated tag.
  63. *
  64. * @return Tag
  65. */
  66. public function getTag()
  67. {
  68. if (null === $this->tag) {
  69. $values = array_values($this->lines);
  70. $this->tag = new Tag($values[0]->getContent());
  71. }
  72. return $this->tag;
  73. }
  74. /**
  75. * Remove this annotation by removing all its lines.
  76. */
  77. public function remove()
  78. {
  79. foreach ($this->lines as $line) {
  80. $line->remove();
  81. }
  82. }
  83. /**
  84. * Get the annotation content.
  85. *
  86. * @return string
  87. */
  88. public function getContent()
  89. {
  90. return implode($this->lines);
  91. }
  92. /**
  93. * Get the string representation of object.
  94. *
  95. * @return string
  96. */
  97. public function __toString()
  98. {
  99. return $this->getContent();
  100. }
  101. }