2012-05-06 17:49:04 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class PHPParser_Comment
|
|
|
|
{
|
|
|
|
protected $text;
|
2012-06-06 15:33:38 +02:00
|
|
|
protected $line;
|
|
|
|
|
2012-05-06 17:49:04 +02:00
|
|
|
/**
|
|
|
|
* Constructs a comment node.
|
|
|
|
*
|
|
|
|
* @param string $text Comment text (including comment delimiters like /*)
|
2012-06-08 17:55:35 +02:00
|
|
|
* @param int $line Line number the comment started on
|
2012-05-06 17:49:04 +02:00
|
|
|
*/
|
2012-06-08 17:55:35 +02:00
|
|
|
public function __construct($text, $line = -1) {
|
2012-05-06 17:49:04 +02:00
|
|
|
$this->text = $text;
|
2012-06-06 15:33:38 +02:00
|
|
|
$this->line = $line;
|
2012-05-06 17:49:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the comment text.
|
|
|
|
*
|
|
|
|
* @return string The comment text (including comment delimiters like /*)
|
|
|
|
*/
|
|
|
|
public function getText() {
|
|
|
|
return $this->text;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the comment text.
|
|
|
|
*
|
|
|
|
* @param string $text The comment text (including comment delimiters like /*)
|
|
|
|
*/
|
|
|
|
public function setText($text) {
|
|
|
|
$this->text = $text;
|
|
|
|
}
|
|
|
|
|
2012-06-06 15:33:38 +02:00
|
|
|
/**
|
|
|
|
* Gets the line number the comment started on.
|
|
|
|
*
|
|
|
|
* @return int Line number
|
|
|
|
*/
|
|
|
|
public function getLine() {
|
|
|
|
return $this->line;
|
|
|
|
}
|
|
|
|
|
2012-06-08 17:55:35 +02:00
|
|
|
/**
|
|
|
|
* Sets the line number the comment started on.
|
|
|
|
*
|
2012-08-02 17:01:33 +02:00
|
|
|
* @param int $line Line number
|
2012-06-08 17:55:35 +02:00
|
|
|
*/
|
|
|
|
public function setLine($line) {
|
|
|
|
$this->line = $line;
|
|
|
|
}
|
|
|
|
|
2012-05-06 17:49:04 +02:00
|
|
|
/**
|
|
|
|
* Gets the comment text.
|
|
|
|
*
|
|
|
|
* @return string The comment text (including comment delimiters like /*)
|
|
|
|
*/
|
|
|
|
public function __toString() {
|
|
|
|
return $this->text;
|
|
|
|
}
|
2012-05-11 16:18:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the reformatted comment text.
|
|
|
|
*
|
|
|
|
* "Reformatted" here means that we try to clean up the whitespace at the
|
|
|
|
* starts of the lines. This is necessary because we receive the comments
|
|
|
|
* without trailing whitespace on the first line, but with trailing whitespace
|
|
|
|
* on all subsequent lines.
|
|
|
|
*
|
|
|
|
* @return mixed|string
|
|
|
|
*/
|
|
|
|
public function getReformattedText() {
|
|
|
|
$text = trim($this->text);
|
|
|
|
if (false === strpos($text, "\n")) {
|
|
|
|
// Single line comments don't need further processing
|
|
|
|
return $text;
|
2012-05-11 20:38:05 +02:00
|
|
|
} elseif (preg_match('((*BSR_ANYCRLF)(*ANYCRLF)^.*(?:\R\s+\*.*)+$)', $text)) {
|
2012-05-11 16:18:14 +02:00
|
|
|
// Multi line comment of the type
|
|
|
|
//
|
|
|
|
// /*
|
|
|
|
// * Some text.
|
|
|
|
// * Some more text.
|
|
|
|
// */
|
|
|
|
//
|
|
|
|
// is handled by replacing the whitespace sequences before the * by a single space
|
|
|
|
return preg_replace('(^\s+\*)m', ' *', $this->text);
|
2012-05-11 18:45:55 +02:00
|
|
|
} elseif (preg_match('(^/\*\*?\s*[\r\n])', $text) && preg_match('(\n(\s*)\*/$)', $text, $matches)) {
|
2012-05-11 16:18:14 +02:00
|
|
|
// Multi line comment of the type
|
|
|
|
//
|
|
|
|
// /*
|
|
|
|
// Some text.
|
|
|
|
// Some more text.
|
|
|
|
// */
|
|
|
|
//
|
|
|
|
// is handled by removing the whitespace sequence on the line before the closing
|
|
|
|
// */ on all lines. So if the last line is " */", then " " is removed at the
|
|
|
|
// start of all lines.
|
|
|
|
return preg_replace('(^' . preg_quote($matches[1]) . ')m', '', $text);
|
|
|
|
} elseif (preg_match('(^/\*\*?\s*(?!\s))', $text, $matches)) {
|
|
|
|
// Multi line comment of the type
|
|
|
|
//
|
|
|
|
// /* Some text.
|
|
|
|
// Some more text.
|
|
|
|
// Even more text. */
|
|
|
|
//
|
|
|
|
// is handled by taking the length of the "/* " segment and leaving only that
|
|
|
|
// many space characters before the lines. Thus in the above example only three
|
|
|
|
// space characters are left at the start of every line.
|
|
|
|
return preg_replace('(^\s*(?= {' . strlen($matches[0]) . '}(?!\s)))m', '', $text);
|
|
|
|
}
|
|
|
|
|
|
|
|
// No idea how to format this comment, so simply return as is
|
|
|
|
return $text;
|
|
|
|
}
|
2012-05-06 17:49:04 +02:00
|
|
|
}
|