PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/PhpParser/Comment.php

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