Skip to content

Commit

Permalink
Don't trim in Comment::getReformattedText()
Browse files Browse the repository at this point in the history
In the past, single-line comments were stored together with the
trailing newline. Later we switched to the PHP8 comment
representation, where the trailing newline is not part of the
comment anymore. As such, there is also no need to trim here.

This is split out from GH-867.
  • Loading branch information
nikic committed Aug 7, 2022
1 parent c55c7a2 commit 1f504d2
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/PhpParser/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function __toString() : string {
* @return mixed|string
*/
public function getReformattedText() {
$text = trim($this->text);
$text = $this->text;
$newlinePos = strpos($text, "\n");
if (false === $newlinePos) {
// Single line comments don't need further processing
Expand Down
2 changes: 1 addition & 1 deletion test/PhpParser/CommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testReformatting($commentText, $reformattedText) {

public function provideTestReformatting() {
return [
['// Some text' . "\n", '// Some text'],
['// Some text', '// Some text'],
['/* Some text */', '/* Some text */'],
[
'/**
Expand Down
4 changes: 2 additions & 2 deletions test/code/formatPreservation/classMethodNop.test
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Foo {
}
}
-----
$stmts[0]->stmts[0]->stmts[0]->setAttribute('comments', [new Comment("// I'm a new comment\n")]);
$stmts[0]->stmts[0]->stmts[0]->setAttribute('comments', [new Comment("// I'm a new comment")]);
-----
<?php
class Foo {
Expand All @@ -74,4 +74,4 @@ class Foo {
// I'm a new comment

}
}
}

0 comments on commit 1f504d2

Please sign in to comment.