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 all 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
100 changes: 48 additions & 52 deletions lib/PhpParser/Comment.php
Expand Up @@ -153,56 +153,53 @@ public function __toString(): string {
* without trailing whitespace on the first line, but with trailing whitespace
* on all subsequent lines.
*
* @return mixed|string
* @return string
*/
public function getReformattedText() {
$text = $this->text;
$newlinePos = strpos($text, "\n");
if (false === $newlinePos) {
// Single line comments don't need further processing
return $text;
}
if (preg_match('((*BSR_ANYCRLF)(*ANYCRLF)^.*(?:\R\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', ' *', $this->text);
}
if (preg_match('(^/\*\*?\s*[\r\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);
if (
$this->startLine !== $this->endLine
|| ($this->endLine === -1 && strpos($this->text, "\n") !== false)
) {
if (preg_match('((*BSR_ANYCRLF)(*ANYCRLF)^.*(?:\R\s+\*.*)+$)', $this->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 (string) preg_replace('(^\s+\*)m', ' *', $this->text);
}
if (preg_match('(^/\*+\s*[\r\n])', $this->text) && preg_match('(^(\s*)\*/$)m', $this->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 (string) preg_replace('(^' . preg_quote($matches[1]) . ')m', '', $this->text);
}
if (preg_match('(^/\*+\s*(?!\s))m', $this->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(explode("\n", $this->text, 2)[1]);
$removeLen = $prefixLen - strlen($matches[0]);
return (string) preg_replace('(^\s{' . $removeLen . '})m', '', $this->text);
}
}

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

/**
Expand All @@ -217,7 +214,7 @@ private function getShortestWhitespacePrefixLen(string $str): int {
$lines = explode("\n", $str);
$shortestPrefixLen = \PHP_INT_MAX;
foreach ($lines as $line) {
preg_match('(^\s*)', $line, $matches);
preg_match('(^\s*)m', $line, $matches);
$prefixLen = strlen($matches[0]);
if ($prefixLen < $shortestPrefixLen) {
$shortestPrefixLen = $prefixLen;
Expand All @@ -227,14 +224,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
9 changes: 8 additions & 1 deletion lib/PhpParser/Comment/Doc.php
Expand Up @@ -2,5 +2,12 @@

namespace PhpParser\Comment;

class Doc extends \PhpParser\Comment {
class Doc extends \PhpParser\Comment
{
/**
* @inheritdoc
*/
public function jsonSerialize() : array {
return ['nodeType' => 'Comment_Doc'] + parent::jsonSerialize();
}
}
26 changes: 19 additions & 7 deletions test/PhpParser/CommentTest.php
Expand Up @@ -23,15 +23,22 @@ public function testGetters() {
/**
* @dataProvider provideTestReformatting
*/
public function testReformatting($commentText, $reformattedText) {
$comment = new Comment($commentText);
public function testReformatting(
$commentText,
$reformattedText,
$startLine,
$startFilePos,
$startTokenPos,
$endLine
) {
$comment = new Comment($commentText, $startLine, $startFilePos, $startTokenPos, $endLine);
$this->assertSame($reformattedText, $comment->getReformattedText());
}

public function provideTestReformatting() {
return [
['// Some text', '// Some text'],
['/* Some text */', '/* Some text */'],
['// Some text' . "\n", '// Some text', 1, 10, 2, 1],
['/* Some text */', '/* Some text */', 1, 10, 2, 1],
[
'/**
* Some text.
Expand All @@ -40,7 +47,8 @@ public function provideTestReformatting() {
'/**
* Some text.
* Some more text.
*/'
*/',
1, 10, 2, 4
],
[
'/*
Expand All @@ -50,15 +58,17 @@ public function provideTestReformatting() {
'/*
Some text.
Some more text.
*/'
*/',
1, 10, 2, 4
],
[
'/* Some text.
More text.
Even more text. */',
'/* Some text.
More text.
Even more text. */'
Even more text. */',
1, 10, 2, 3
],
[
'/* Some text.
Expand All @@ -67,13 +77,15 @@ public function provideTestReformatting() {
'/* Some text.
More text.
Indented text. */',
1, 10, 2, 4
],
// invalid comment -> no reformatting
[
'hallo
world',
'hallo
world',
1, 10, 2, 2
],
];
}
Expand Down