Skip to content

Commit 2996f4e

Browse files
committed
Fix
1 parent 169bc33 commit 2996f4e

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

lib/PhpParser/Internal/TokenStream.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,17 @@ public function getIndentationBefore(int $pos): int {
220220
*
221221
* @return string Code corresponding to token range, adjusted for indentation
222222
*/
223-
public function getTokenCode(int $from, int $to, int $indent): string {
223+
public function getTokenCode(int $from, int $to, int $indent, bool $removeTrailingComma = false): string {
224224
$tokens = $this->tokens;
225225
$result = '';
226226
for ($pos = $from; $pos < $to; $pos++) {
227227
$token = $tokens[$pos];
228228
$id = $token->id;
229+
230+
if ($removeTrailingComma && $token->text === ',') {
231+
$token->text = '';
232+
}
233+
229234
$text = $token->text;
230235
if ($id === \T_CONSTANT_ENCAPSED_STRING || $id === \T_ENCAPSED_AND_WHITESPACE) {
231236
$result .= $text;

lib/PhpParser/PrettyPrinterAbstract.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpParser\Node\Expr;
1111
use PhpParser\Node\Expr\AssignOp;
1212
use PhpParser\Node\Expr\BinaryOp;
13+
use PhpParser\Node\Expr\CallLike;
1314
use PhpParser\Node\Expr\Cast;
1415
use PhpParser\Node\IntersectionType;
1516
use PhpParser\Node\MatchArm;
@@ -764,7 +765,12 @@ protected function p(
764765
$pos = $subEndPos + 1;
765766
}
766767

767-
$result .= $this->origTokens->getTokenCode($pos, $endPos + 1, $indentAdjustment);
768+
if ($node instanceof CallLike && substr(trim($result), -3) === '...') {
769+
$result .= $this->origTokens->getTokenCode($pos, $endPos + 1, $indentAdjustment, true);
770+
} else {
771+
$result .= $this->origTokens->getTokenCode($pos, $endPos + 1, $indentAdjustment);
772+
}
773+
768774
return $result;
769775
}
770776

test/PhpParser/PrettyPrinterTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,10 @@ public function testTrailingCommaArgToFirstClassCallable(): void {
312312
$traverser = new NodeTraverser(new NodeVisitor\CloningVisitor());
313313

314314
$oldStmts = $parser->parse(<<<'CODE'
315-
<?php strlen("test",);
315+
<?php
316+
strlen(
317+
"test",
318+
);
316319
CODE
317320
);
318321
$oldTokens = $parser->getTokens();
@@ -323,7 +326,10 @@ public function testTrailingCommaArgToFirstClassCallable(): void {
323326

324327
$prettyPrinter = new PrettyPrinter\Standard();
325328
$expected = <<<'CODE'
326-
<?php strlen(...);
329+
<?php
330+
strlen(
331+
...
332+
);
327333
CODE;
328334

329335
$this->assertEquals(

0 commit comments

Comments
 (0)