Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/PhpParser/Internal/TokenStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,19 @@ public function getIndentationBefore(int $pos): int {
*
* @return string Code corresponding to token range, adjusted for indentation
*/
public function getTokenCode(int $from, int $to, int $indent): string {
public function getTokenCode(int $from, int $to, int $indent, bool $removeTrailingComma = false): string {
$tokens = $this->tokens;
$result = '';
for ($pos = $from; $pos < $to; $pos++) {
$token = $tokens[$pos];
$id = $token->id;

if ($removeTrailingComma && $token->text === ',') {
$token->text = '';
// don't try to remove trailing comma multiple times
$removeTrailingComma = false;
}

$text = $token->text;
if ($id === \T_CONSTANT_ENCAPSED_STRING || $id === \T_ENCAPSED_AND_WHITESPACE) {
$result .= $text;
Expand Down
8 changes: 7 additions & 1 deletion lib/PhpParser/PrettyPrinterAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\AssignOp;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\Cast;
use PhpParser\Node\IntersectionType;
use PhpParser\Node\MatchArm;
Expand Down Expand Up @@ -765,7 +766,12 @@ protected function p(
$pos = $subEndPos + 1;
}

$result .= $this->origTokens->getTokenCode($pos, $endPos + 1, $indentAdjustment);
if ($node instanceof CallLike && substr(trim($result), -3) === '...' && $node->isFirstClassCallable()) {
$result .= $this->origTokens->getTokenCode($pos, $endPos + 1, $indentAdjustment, true);
} else {
$result .= $this->origTokens->getTokenCode($pos, $endPos + 1, $indentAdjustment);
}

return $result;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Replace args with VariadicPlaceholder should remove trailing comma
-----
<?php
strlen(
'test',
);
-----
$stmts[0]->expr->args = [new Node\VariadicPlaceholder()];
-----
<?php
strlen(
...
);