菜谱项目

Comment.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace PhpParser;
  3. class Comment implements \JsonSerializable
  4. {
  5. protected $text;
  6. protected $line;
  7. protected $filePos;
  8. /**
  9. * Constructs a comment node.
  10. *
  11. * @param string $text Comment text (including comment delimiters like /*)
  12. * @param int $startLine Line number the comment started on
  13. * @param int $startFilePos File offset the comment started on
  14. */
  15. public function __construct($text, $startLine = -1, $startFilePos = -1) {
  16. $this->text = $text;
  17. $this->line = $startLine;
  18. $this->filePos = $startFilePos;
  19. }
  20. /**
  21. * Gets the comment text.
  22. *
  23. * @return string The comment text (including comment delimiters like /*)
  24. */
  25. public function getText() {
  26. return $this->text;
  27. }
  28. /**
  29. * Gets the line number the comment started on.
  30. *
  31. * @return int Line number
  32. */
  33. public function getLine() {
  34. return $this->line;
  35. }
  36. /**
  37. * Gets the file offset the comment started on.
  38. *
  39. * @return int File offset
  40. */
  41. public function getFilePos() {
  42. return $this->filePos;
  43. }
  44. /**
  45. * Gets the comment text.
  46. *
  47. * @return string The comment text (including comment delimiters like /*)
  48. */
  49. public function __toString() {
  50. return $this->text;
  51. }
  52. /**
  53. * Gets the reformatted comment text.
  54. *
  55. * "Reformatted" here means that we try to clean up the whitespace at the
  56. * starts of the lines. This is necessary because we receive the comments
  57. * without trailing whitespace on the first line, but with trailing whitespace
  58. * on all subsequent lines.
  59. *
  60. * @return mixed|string
  61. */
  62. public function getReformattedText() {
  63. $text = trim($this->text);
  64. $newlinePos = strpos($text, "\n");
  65. if (false === $newlinePos) {
  66. // Single line comments don't need further processing
  67. return $text;
  68. } elseif (preg_match('((*BSR_ANYCRLF)(*ANYCRLF)^.*(?:\R\s+\*.*)+$)', $text)) {
  69. // Multi line comment of the type
  70. //
  71. // /*
  72. // * Some text.
  73. // * Some more text.
  74. // */
  75. //
  76. // is handled by replacing the whitespace sequences before the * by a single space
  77. return preg_replace('(^\s+\*)m', ' *', $this->text);
  78. } elseif (preg_match('(^/\*\*?\s*[\r\n])', $text) && preg_match('(\n(\s*)\*/$)', $text, $matches)) {
  79. // Multi line comment of the type
  80. //
  81. // /*
  82. // Some text.
  83. // Some more text.
  84. // */
  85. //
  86. // is handled by removing the whitespace sequence on the line before the closing
  87. // */ on all lines. So if the last line is " */", then " " is removed at the
  88. // start of all lines.
  89. return preg_replace('(^' . preg_quote($matches[1]) . ')m', '', $text);
  90. } elseif (preg_match('(^/\*\*?\s*(?!\s))', $text, $matches)) {
  91. // Multi line comment of the type
  92. //
  93. // /* Some text.
  94. // Some more text.
  95. // Indented text.
  96. // Even more text. */
  97. //
  98. // is handled by removing the difference between the shortest whitespace prefix on all
  99. // lines and the length of the "/* " opening sequence.
  100. $prefixLen = $this->getShortestWhitespacePrefixLen(substr($text, $newlinePos + 1));
  101. $removeLen = $prefixLen - strlen($matches[0]);
  102. return preg_replace('(^\s{' . $removeLen . '})m', '', $text);
  103. }
  104. // No idea how to format this comment, so simply return as is
  105. return $text;
  106. }
  107. private function getShortestWhitespacePrefixLen($str) {
  108. $lines = explode("\n", $str);
  109. $shortestPrefixLen = INF;
  110. foreach ($lines as $line) {
  111. preg_match('(^\s*)', $line, $matches);
  112. $prefixLen = strlen($matches[0]);
  113. if ($prefixLen < $shortestPrefixLen) {
  114. $shortestPrefixLen = $prefixLen;
  115. }
  116. }
  117. return $shortestPrefixLen;
  118. }
  119. public function jsonSerialize() {
  120. // Technically not a node, but we make it look like one anyway
  121. $type = $this instanceof Comment\Doc ? 'Comment_Doc' : 'Comment';
  122. return [
  123. 'nodeType' => $type,
  124. 'text' => $this->text,
  125. 'line' => $this->line,
  126. 'filePos' => $this->filePos,
  127. ];
  128. }
  129. }