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