Comment.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. class PHPParser_Comment
  3. {
  4. protected $text;
  5. protected $line;
  6. /**
  7. * Constructs a comment node.
  8. *
  9. * @param string $text Comment text (including comment delimiters like /*)
  10. * @param int $line Line number the comment started on
  11. */
  12. public function __construct($text, $line = -1) {
  13. $this->text = $text;
  14. $this->line = $line;
  15. }
  16. /**
  17. * Gets the comment text.
  18. *
  19. * @return string The comment text (including comment delimiters like /*)
  20. */
  21. public function getText() {
  22. return $this->text;
  23. }
  24. /**
  25. * Sets the comment text.
  26. *
  27. * @param string $text The comment text (including comment delimiters like /*)
  28. */
  29. public function setText($text) {
  30. $this->text = $text;
  31. }
  32. /**
  33. * Gets the line number the comment started on.
  34. *
  35. * @return int Line number
  36. */
  37. public function getLine() {
  38. return $this->line;
  39. }
  40. /**
  41. * Sets the line number the comment started on.
  42. *
  43. * @param int $line Line number
  44. */
  45. public function setLine($line) {
  46. $this->line = $line;
  47. }
  48. /**
  49. * Gets the comment text.
  50. *
  51. * @return string The comment text (including comment delimiters like /*)
  52. */
  53. public function __toString() {
  54. return $this->text;
  55. }
  56. /**
  57. * Gets the reformatted comment text.
  58. *
  59. * "Reformatted" here means that we try to clean up the whitespace at the
  60. * starts of the lines. This is necessary because we receive the comments
  61. * without trailing whitespace on the first line, but with trailing whitespace
  62. * on all subsequent lines.
  63. *
  64. * @return mixed|string
  65. */
  66. public function getReformattedText() {
  67. $text = trim($this->text);
  68. if (false === strpos($text, "\n")) {
  69. // Single line comments don't need further processing
  70. return $text;
  71. } elseif (preg_match('((*BSR_ANYCRLF)(*ANYCRLF)^.*(?:\R\s+\*.*)+$)', $text)) {
  72. // Multi line comment of the type
  73. //
  74. // /*
  75. // * Some text.
  76. // * Some more text.
  77. // */
  78. //
  79. // is handled by replacing the whitespace sequences before the * by a single space
  80. return preg_replace('(^\s+\*)m', ' *', $this->text);
  81. } elseif (preg_match('(^/\*\*?\s*[\r\n])', $text) && preg_match('(\n(\s*)\*/$)', $text, $matches)) {
  82. // Multi line comment of the type
  83. //
  84. // /*
  85. // Some text.
  86. // Some more text.
  87. // */
  88. //
  89. // is handled by removing the whitespace sequence on the line before the closing
  90. // */ on all lines. So if the last line is " */", then " " is removed at the
  91. // start of all lines.
  92. return preg_replace('(^' . preg_quote($matches[1]) . ')m', '', $text);
  93. } elseif (preg_match('(^/\*\*?\s*(?!\s))', $text, $matches)) {
  94. // Multi line comment of the type
  95. //
  96. // /* Some text.
  97. // Some more text.
  98. // Even more text. */
  99. //
  100. // is handled by taking the length of the "/* " segment and leaving only that
  101. // many space characters before the lines. Thus in the above example only three
  102. // space characters are left at the start of every line.
  103. return preg_replace('(^\s*(?= {' . strlen($matches[0]) . '}(?!\s)))m', '', $text);
  104. }
  105. // No idea how to format this comment, so simply return as is
  106. return $text;
  107. }
  108. }