From 29a4d5e66221d8eba6114ff77e23c5d33e9644c9 Mon Sep 17 00:00:00 2001 From: Ruud Kamphuis Date: Sun, 15 Mar 2026 10:25:43 +0100 Subject: [PATCH] Add trailing comma via suffixLast when addCommaAfterEachArgument is false When addCommaAfterEachArgument is false, dumpCall previously added no commas at all. PHP CS Fixer expects a trailing comma on the last argument in multiline function calls. Use suffixLast to add a comma only to the last argument line instead of every line. --- src/CodeGenerator.php | 4 ++-- tests/CodeGeneratorTest.php | 45 +++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/CodeGenerator.php b/src/CodeGenerator.php index e5bc9d7..b844277 100644 --- a/src/CodeGenerator.php +++ b/src/CodeGenerator.php @@ -639,7 +639,7 @@ public function dumpCall( } yield sprintf('->%s(', $method); - yield Group::indent($addCommaAfterEachArgument ? $this->allSuffix(',', $args) : $args); + yield Group::indent($addCommaAfterEachArgument ? $this->allSuffix(',', $args) : $this->suffixLast(',', $args)); yield ')'; }); @@ -668,7 +668,7 @@ public function dumpCall( } yield sprintf('%s(', $call); - yield Group::indent($addCommaAfterEachArgument ? $this->allSuffix(',', $args) : $args); + yield Group::indent($addCommaAfterEachArgument ? $this->allSuffix(',', $args) : $this->suffixLast(',', $args)); yield ')'; } diff --git a/tests/CodeGeneratorTest.php b/tests/CodeGeneratorTest.php index dcb4f69..03038de 100644 --- a/tests/CodeGeneratorTest.php +++ b/tests/CodeGeneratorTest.php @@ -1025,6 +1025,51 @@ public function testDumpCallWithMultipleArgsOnIterable() : void ); } + public function testDumpCallWithoutCommaAfterEachArgumentAddsTrailingComma() : void + { + $this->assertDump( + <<<'PHP' + Helper::process( + match (true) { + $a => 1, + default => 2, + }, + ) + PHP, + $this->generator->dumpCall('App\\Utils\\Helper', 'process', [ + 'match (true) {', + $this->generator->indent(function () { + yield '$a => 1,'; + yield 'default => 2,'; + }), + '}', + ], true, false), + ); + } + + public function testDumpCallOnIterableWithoutCommaAfterEachArgumentAddsTrailingComma() : void + { + $this->assertDump( + <<<'PHP' + $object + ->method( + match (true) { + $a => 1, + default => 2, + }, + ) + PHP, + $this->generator->dumpCall(['$object'], 'method', [ + 'match (true) {', + $this->generator->indent(function () { + yield '$a => 1,'; + yield 'default => 2,'; + }), + '}', + ], false, false), + ); + } + public function testDumpFileWithNoImportsHasNoExtraNewline() : void { $this->generator = new CodeGenerator('App\\Services');