Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize comment classes #867

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 14 additions & 17 deletions lib/PhpParser/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,14 @@ public function __toString() : string {
* without trailing whitespace on the first line, but with trailing whitespace
* on all subsequent lines.
*
* @return mixed|string
* @return string|null
WinterSilence marked this conversation as resolved.
Show resolved Hide resolved
*/
public function getReformattedText() {
$text = $this->text;
$newlinePos = strpos($text, "\n");
if (false === $newlinePos) {
if ($this->startLine === $this->endLine) {
// Single line comments don't need further processing
return $text;
return $this->text;
}
if (preg_match('((*BSR_ANYCRLF)(*ANYCRLF)^.*(?:\R\s+\*.*)+$)', $text)) {
if (preg_match('((*BSR_ANYCRLF)(*ANYCRLF)^.*(?:\R\s+\*.*)+$)', $this->text)) {
// Multi line comment of the type
//
// /*
Expand All @@ -167,7 +165,7 @@ public function getReformattedText() {
// is handled by replacing the whitespace sequences before the * by a single space
return preg_replace('(^\s+\*)m', ' *', $this->text);
}
if (preg_match('(^/\*\*?\s*[\r\n])', $text) && preg_match('(\n(\s*)\*/$)', $text, $matches)) {
if (preg_match('(^/\*\*?\s*[\r\n])', $this->text) && preg_match('(\n(\s*)\*/$)', $this->text, $matches)) {
// Multi line comment of the type
//
// /*
Expand All @@ -178,9 +176,9 @@ public function getReformattedText() {
// 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);
return preg_replace('(^' . preg_quote($matches[1]) . ')m', '', $this->text);
}
if (preg_match('(^/\*\*?\s*(?!\s))', $text, $matches)) {
if (preg_match('(^/\*\*?\s*(?!\s))', $this->text, $matches)) {
// Multi line comment of the type
//
// /* Some text.
Expand All @@ -190,13 +188,13 @@ public function getReformattedText() {
//
// 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));
$prefixLen = $this->getShortestWhitespacePrefixLen(explode("\n", $this->text, 2)[1]);
$removeLen = $prefixLen - strlen($matches[0]);
return preg_replace('(^\s{' . $removeLen . '})m', '', $text);
return preg_replace('(^\s{' . $removeLen . '})m', '', $this->text);
}

// No idea how to format this comment, so simply return as is
return $text;
return $this->text;
}

/**
Expand All @@ -209,7 +207,7 @@ public function getReformattedText() {
*/
private function getShortestWhitespacePrefixLen(string $str) : int {
$lines = explode("\n", $str);
$shortestPrefixLen = \INF;
$shortestPrefixLen = 0;
WinterSilence marked this conversation as resolved.
Show resolved Hide resolved
foreach ($lines as $line) {
preg_match('(^\s*)', $line, $matches);
$prefixLen = strlen($matches[0]);
Expand All @@ -221,14 +219,13 @@ private function getShortestWhitespacePrefixLen(string $str) : int {
}

/**
* @return array
* @psalm-return array{nodeType:string, text:mixed, line:mixed, filePos:mixed}
* @psalm-return array{nodeType: string, text: string, line: int, filePos: int, tokenPos: int, endLine: int,
* endFilePos: int, endTokenPos: int}
*/
public function jsonSerialize() : array {
// Technically not a node, but we make it look like one anyway
$type = $this instanceof Comment\Doc ? 'Comment_Doc' : 'Comment';
WinterSilence marked this conversation as resolved.
Show resolved Hide resolved
return [
'nodeType' => $type,
'nodeType' => 'Comment',
'text' => $this->text,
// TODO: Rename these to include "start".
'line' => $this->startLine,
Expand Down
6 changes: 6 additions & 0 deletions lib/PhpParser/Comment/Doc.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@

class Doc extends \PhpParser\Comment
{
/**
* @inheritdoc
*/
public function jsonSerialize() : array {
return ['nodeType' => 'Comment_Doc'] + parent::jsonSerialize();
}
}