2017-08-18 22:57:27 +02:00
|
|
|
<?php declare(strict_types=1);
|
2012-05-06 17:49:04 +02:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser;
|
|
|
|
|
2016-07-25 20:57:53 +02:00
|
|
|
class Comment implements \JsonSerializable
|
2012-05-06 17:49:04 +02:00
|
|
|
{
|
|
|
|
protected $text;
|
2020-02-09 16:53:46 +01:00
|
|
|
protected $startLine;
|
|
|
|
protected $startFilePos;
|
|
|
|
protected $startTokenPos;
|
|
|
|
protected $endLine;
|
|
|
|
protected $endFilePos;
|
|
|
|
protected $endTokenPos;
|
2012-06-06 15:33:38 +02:00
|
|
|
|
2012-05-06 17:49:04 +02:00
|
|
|
/**
|
|
|
|
* Constructs a comment node.
|
|
|
|
*
|
2017-11-04 17:45:14 +01:00
|
|
|
* @param string $text Comment text (including comment delimiters like /*)
|
|
|
|
* @param int $startLine Line number the comment started on
|
|
|
|
* @param int $startFilePos File offset the comment started on
|
|
|
|
* @param int $startTokenPos Token offset the comment started on
|
2012-05-06 17:49:04 +02:00
|
|
|
*/
|
2017-11-04 17:45:14 +01:00
|
|
|
public function __construct(
|
2020-02-09 16:53:46 +01:00
|
|
|
string $text,
|
|
|
|
int $startLine = -1, int $startFilePos = -1, int $startTokenPos = -1,
|
|
|
|
int $endLine = -1, int $endFilePos = -1, int $endTokenPos = -1
|
2017-11-04 17:45:14 +01:00
|
|
|
) {
|
2012-05-06 17:49:04 +02:00
|
|
|
$this->text = $text;
|
2020-02-09 16:53:46 +01:00
|
|
|
$this->startLine = $startLine;
|
|
|
|
$this->startFilePos = $startFilePos;
|
|
|
|
$this->startTokenPos = $startTokenPos;
|
|
|
|
$this->endLine = $endLine;
|
|
|
|
$this->endFilePos = $endFilePos;
|
|
|
|
$this->endTokenPos = $endTokenPos;
|
2012-05-06 17:49:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the comment text.
|
|
|
|
*
|
|
|
|
* @return string The comment text (including comment delimiters like /*)
|
|
|
|
*/
|
2017-04-28 21:40:59 +02:00
|
|
|
public function getText() : string {
|
2012-05-06 17:49:04 +02:00
|
|
|
return $this->text;
|
|
|
|
}
|
|
|
|
|
2012-06-06 15:33:38 +02:00
|
|
|
/**
|
|
|
|
* Gets the line number the comment started on.
|
|
|
|
*
|
2020-02-09 16:53:46 +01:00
|
|
|
* @return int Line number (or -1 if not available)
|
|
|
|
*/
|
|
|
|
public function getStartLine() : int {
|
|
|
|
return $this->startLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the file offset the comment started on.
|
|
|
|
*
|
|
|
|
* @return int File offset (or -1 if not available)
|
|
|
|
*/
|
|
|
|
public function getStartFilePos() : int {
|
|
|
|
return $this->startFilePos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the token offset the comment started on.
|
|
|
|
*
|
|
|
|
* @return int Token offset (or -1 if not available)
|
|
|
|
*/
|
|
|
|
public function getStartTokenPos() : int {
|
|
|
|
return $this->startTokenPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the line number the comment ends on.
|
|
|
|
*
|
|
|
|
* @return int Line number (or -1 if not available)
|
|
|
|
*/
|
|
|
|
public function getEndLine() : int {
|
|
|
|
return $this->endLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the file offset the comment ends on.
|
|
|
|
*
|
|
|
|
* @return int File offset (or -1 if not available)
|
|
|
|
*/
|
|
|
|
public function getEndFilePos() : int {
|
|
|
|
return $this->endFilePos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the token offset the comment ends on.
|
|
|
|
*
|
|
|
|
* @return int Token offset (or -1 if not available)
|
|
|
|
*/
|
|
|
|
public function getEndTokenPos() : int {
|
|
|
|
return $this->endTokenPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the line number the comment started on.
|
|
|
|
*
|
|
|
|
* @deprecated Use getStartLine() instead
|
|
|
|
*
|
2012-06-06 15:33:38 +02:00
|
|
|
* @return int Line number
|
|
|
|
*/
|
2017-04-28 21:40:59 +02:00
|
|
|
public function getLine() : int {
|
2020-02-09 16:53:46 +01:00
|
|
|
return $this->startLine;
|
2012-06-06 15:33:38 +02:00
|
|
|
}
|
|
|
|
|
2016-04-02 00:54:01 +02:00
|
|
|
/**
|
|
|
|
* Gets the file offset the comment started on.
|
|
|
|
*
|
2020-02-09 16:53:46 +01:00
|
|
|
* @deprecated Use getStartFilePos() instead
|
|
|
|
*
|
2016-04-02 00:54:01 +02:00
|
|
|
* @return int File offset
|
|
|
|
*/
|
2017-04-28 21:40:59 +02:00
|
|
|
public function getFilePos() : int {
|
2020-02-09 16:53:46 +01:00
|
|
|
return $this->startFilePos;
|
2016-04-02 00:54:01 +02:00
|
|
|
}
|
|
|
|
|
2017-11-04 17:45:14 +01:00
|
|
|
/**
|
|
|
|
* Gets the token offset the comment started on.
|
|
|
|
*
|
2020-02-09 16:53:46 +01:00
|
|
|
* @deprecated Use getStartTokenPos() instead
|
|
|
|
*
|
2017-11-04 17:45:14 +01:00
|
|
|
* @return int Token offset
|
|
|
|
*/
|
|
|
|
public function getTokenPos() : int {
|
2020-02-09 16:53:46 +01:00
|
|
|
return $this->startTokenPos;
|
2017-11-04 17:45:14 +01:00
|
|
|
}
|
|
|
|
|
2012-05-06 17:49:04 +02:00
|
|
|
/**
|
|
|
|
* Gets the comment text.
|
|
|
|
*
|
|
|
|
* @return string The comment text (including comment delimiters like /*)
|
|
|
|
*/
|
2017-04-28 21:40:59 +02:00
|
|
|
public function __toString() : string {
|
2012-05-06 17:49:04 +02:00
|
|
|
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);
|
2016-04-07 04:39:32 +02:00
|
|
|
$newlinePos = strpos($text, "\n");
|
|
|
|
if (false === $newlinePos) {
|
2012-05-11 16:18:14 +02:00
|
|
|
// 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.
|
2016-04-07 04:39:32 +02:00
|
|
|
// Indented text.
|
2012-05-11 16:18:14 +02:00
|
|
|
// Even more text. */
|
|
|
|
//
|
2016-04-07 04:39:32 +02:00
|
|
|
// is handled by removing the difference between the shortest whitespace prefix on all
|
|
|
|
// lines and the length of the "/* " opening sequence.
|
|
|
|
$prefixLen = $this->getShortestWhitespacePrefixLen(substr($text, $newlinePos + 1));
|
|
|
|
$removeLen = $prefixLen - strlen($matches[0]);
|
|
|
|
return preg_replace('(^\s{' . $removeLen . '})m', '', $text);
|
2012-05-11 16:18:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// No idea how to format this comment, so simply return as is
|
|
|
|
return $text;
|
|
|
|
}
|
2016-04-07 04:39:32 +02:00
|
|
|
|
2017-01-24 08:38:55 +01:00
|
|
|
/**
|
2017-01-26 00:16:54 +01:00
|
|
|
* Get length of shortest whitespace prefix (at the start of a line).
|
|
|
|
*
|
|
|
|
* If there is a line with no prefix whitespace, 0 is a valid return value.
|
|
|
|
*
|
|
|
|
* @param string $str String to check
|
|
|
|
* @return int Length in characters. Tabs count as single characters.
|
2017-01-24 08:38:55 +01:00
|
|
|
*/
|
2017-04-28 21:40:59 +02:00
|
|
|
private function getShortestWhitespacePrefixLen(string $str) : int {
|
2016-04-07 04:39:32 +02:00
|
|
|
$lines = explode("\n", $str);
|
2017-11-10 23:33:12 +01:00
|
|
|
$shortestPrefixLen = \INF;
|
2016-04-07 04:39:32 +02:00
|
|
|
foreach ($lines as $line) {
|
|
|
|
preg_match('(^\s*)', $line, $matches);
|
|
|
|
$prefixLen = strlen($matches[0]);
|
|
|
|
if ($prefixLen < $shortestPrefixLen) {
|
|
|
|
$shortestPrefixLen = $prefixLen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $shortestPrefixLen;
|
|
|
|
}
|
2016-07-25 20:57:53 +02:00
|
|
|
|
2017-01-24 08:38:55 +01:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
* @psalm-return array{nodeType:string, text:mixed, line:mixed, filePos:mixed}
|
|
|
|
*/
|
2017-04-28 21:40:59 +02:00
|
|
|
public function jsonSerialize() : array {
|
2016-07-25 20:57:53 +02:00
|
|
|
// Technically not a node, but we make it look like one anyway
|
|
|
|
$type = $this instanceof Comment\Doc ? 'Comment_Doc' : 'Comment';
|
|
|
|
return [
|
|
|
|
'nodeType' => $type,
|
|
|
|
'text' => $this->text,
|
2020-02-09 16:53:46 +01:00
|
|
|
// TODO: Rename these to include "start".
|
|
|
|
'line' => $this->startLine,
|
|
|
|
'filePos' => $this->startFilePos,
|
|
|
|
'tokenPos' => $this->startTokenPos,
|
|
|
|
'endLine' => $this->endLine,
|
|
|
|
'endFilePos' => $this->endFilePos,
|
|
|
|
'endTokenPos' => $this->endTokenPos,
|
2016-07-25 20:57:53 +02:00
|
|
|
];
|
|
|
|
}
|
2018-01-10 18:04:06 +01:00
|
|
|
}
|