Skip to content

Commit 1f504d2

Browse files
committed
Don't trim in Comment::getReformattedText()
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.
1 parent c55c7a2 commit 1f504d2

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

lib/PhpParser/Comment.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function __toString() : string {
150150
* @return mixed|string
151151
*/
152152
public function getReformattedText() {
153-
$text = trim($this->text);
153+
$text = $this->text;
154154
$newlinePos = strpos($text, "\n");
155155
if (false === $newlinePos) {
156156
// Single line comments don't need further processing

test/PhpParser/CommentTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testReformatting($commentText, $reformattedText) {
3131

3232
public function provideTestReformatting() {
3333
return [
34-
['// Some text' . "\n", '// Some text'],
34+
['// Some text', '// Some text'],
3535
['/* Some text */', '/* Some text */'],
3636
[
3737
'/**

test/code/formatPreservation/classMethodNop.test

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class Foo {
6565
}
6666
}
6767
-----
68-
$stmts[0]->stmts[0]->stmts[0]->setAttribute('comments', [new Comment("// I'm a new comment\n")]);
68+
$stmts[0]->stmts[0]->stmts[0]->setAttribute('comments', [new Comment("// I'm a new comment")]);
6969
-----
7070
<?php
7171
class Foo {
@@ -74,4 +74,4 @@ class Foo {
7474
// I'm a new comment
7575

7676
}
77-
}
77+
}

0 commit comments

Comments
 (0)