function Comment::getReformattedText
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 leading whitespace on the first line, but with leading whitespace on all subsequent lines.
Additionally, this normalizes CRLF newlines to LF newlines.
File
-
vendor/
nikic/ php-parser/ lib/ PhpParser/ Comment.php, line 120
Class
Namespace
PhpParserCode
public function getReformattedText() : string {
$text = str_replace("\r\n", "\n", $this->text);
$newlinePos = strpos($text, "\n");
if (false === $newlinePos) {
// Single line comments don't need further processing
return $text;
}
if (preg_match('(^.*(?:\\n\\s+\\*.*)+$)', $text)) {
// 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', ' *', $text);
}
if (preg_match('(^/\\*\\*?\\s*\\n)', $text) && preg_match('(\\n(\\s*)\\*/$)', $text, $matches)) {
// 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);
}
if (preg_match('(^/\\*\\*?\\s*(?!\\s))', $text, $matches)) {
// Multi line comment of the type
//
// /* Some text.
// Some more text.
// Indented text.
// Even more text. */
//
// 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);
}
// No idea how to format this comment, so simply return as is
return $text;
}