From ccb61b2dc7a60c600037ccfe296e48eb20c4046d Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 27 Oct 2025 13:22:37 +0100 Subject: [PATCH 1/2] remove trailing in printer --- lib/PhpParser/PrettyPrinterAbstract.php | 8 +++++++- ...args_to_variadic_remove_trailing_comma.test | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 test/code/formatPreservation/args_to_variadic_remove_trailing_comma.test diff --git a/lib/PhpParser/PrettyPrinterAbstract.php b/lib/PhpParser/PrettyPrinterAbstract.php index 448bc84919..aa3b0a40aa 100644 --- a/lib/PhpParser/PrettyPrinterAbstract.php +++ b/lib/PhpParser/PrettyPrinterAbstract.php @@ -765,7 +765,13 @@ protected function p( $pos = $subEndPos + 1; } - $result .= $this->origTokens->getTokenCode($pos, $endPos + 1, $indentAdjustment); + $tokenCode = $this->origTokens->getTokenCode($pos, $endPos + 1, $indentAdjustment); + if ($node instanceof Expr\CallLike && $node->isFirstClassCallable()) { + $tokenCode = trim($tokenCode, ','); + } + + $result .= $tokenCode; + return $result; } diff --git a/test/code/formatPreservation/args_to_variadic_remove_trailing_comma.test b/test/code/formatPreservation/args_to_variadic_remove_trailing_comma.test new file mode 100644 index 0000000000..ab1bb029a4 --- /dev/null +++ b/test/code/formatPreservation/args_to_variadic_remove_trailing_comma.test @@ -0,0 +1,18 @@ +Replace args with VariadicPlaceholder should remove trailing comma +----- +expr->args = [new Node\VariadicPlaceholder()]; +$stmts[1]->expr->args = [new Node\VariadicPlaceholder()]; +----- + Date: Mon, 27 Oct 2025 13:23:53 +0100 Subject: [PATCH 2/2] improve test --- lib/PhpParser/PrettyPrinterAbstract.php | 2 +- ...rgs_to_variadic_remove_trailing_comma.test | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/PhpParser/PrettyPrinterAbstract.php b/lib/PhpParser/PrettyPrinterAbstract.php index aa3b0a40aa..12ac7e0b74 100644 --- a/lib/PhpParser/PrettyPrinterAbstract.php +++ b/lib/PhpParser/PrettyPrinterAbstract.php @@ -767,7 +767,7 @@ protected function p( $tokenCode = $this->origTokens->getTokenCode($pos, $endPos + 1, $indentAdjustment); if ($node instanceof Expr\CallLike && $node->isFirstClassCallable()) { - $tokenCode = trim($tokenCode, ','); + $tokenCode = str_replace(',', '', $tokenCode); } $result .= $tokenCode; diff --git a/test/code/formatPreservation/args_to_variadic_remove_trailing_comma.test b/test/code/formatPreservation/args_to_variadic_remove_trailing_comma.test index ab1bb029a4..6619002038 100644 --- a/test/code/formatPreservation/args_to_variadic_remove_trailing_comma.test +++ b/test/code/formatPreservation/args_to_variadic_remove_trailing_comma.test @@ -1,18 +1,38 @@ Replace args with VariadicPlaceholder should remove trailing comma ----- strlen(1 , 2 , 3 ,); + +strlen( + 'multiple new lines' + + , +); ----- $stmts[0]->expr->args = [new Node\VariadicPlaceholder()]; $stmts[1]->expr->args = [new Node\VariadicPlaceholder()]; +$stmts[2]->expr->right->args = [new Node\VariadicPlaceholder()]; +$stmts[3]->expr->args = [new Node\VariadicPlaceholder()]; ----- strlen(...); + +strlen( + ... + + +);