Skip to content

Commit 5fd2375

Browse files
committed
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.
1 parent 274e546 commit 5fd2375

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

src/CodeGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ public function dumpCall(
639639
}
640640

641641
yield sprintf('->%s(', $method);
642-
yield Group::indent($addCommaAfterEachArgument ? $this->allSuffix(',', $args) : $args);
642+
yield Group::indent($addCommaAfterEachArgument ? $this->allSuffix(',', $args) : $this->suffixLast(',', $args));
643643
yield ')';
644644
});
645645

@@ -668,7 +668,7 @@ public function dumpCall(
668668
}
669669

670670
yield sprintf('%s(', $call);
671-
yield Group::indent($addCommaAfterEachArgument ? $this->allSuffix(',', $args) : $args);
671+
yield Group::indent($addCommaAfterEachArgument ? $this->allSuffix(',', $args) : $this->suffixLast(',', $args));
672672
yield ')';
673673
}
674674

tests/CodeGeneratorTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,51 @@ public function testDumpCallWithMultipleArgsOnIterable() : void
10251025
);
10261026
}
10271027

1028+
public function testDumpCallWithoutCommaAfterEachArgumentAddsTrailingComma() : void
1029+
{
1030+
$this->assertDump(
1031+
<<<'PHP'
1032+
Helper::process(
1033+
match (true) {
1034+
$a => 1,
1035+
default => 2,
1036+
},
1037+
)
1038+
PHP,
1039+
$this->generator->dumpCall('App\\Utils\\Helper', 'process', [
1040+
'match (true) {',
1041+
$this->generator->indent(function () {
1042+
yield '$a => 1,';
1043+
yield 'default => 2,';
1044+
}),
1045+
'}',
1046+
], true, false),
1047+
);
1048+
}
1049+
1050+
public function testDumpCallOnIterableWithoutCommaAfterEachArgumentAddsTrailingComma() : void
1051+
{
1052+
$this->assertDump(
1053+
<<<'PHP'
1054+
$object
1055+
->method(
1056+
match (true) {
1057+
$a => 1,
1058+
default => 2,
1059+
},
1060+
)
1061+
PHP,
1062+
$this->generator->dumpCall(['$object'], 'method', [
1063+
'match (true) {',
1064+
$this->generator->indent(function () {
1065+
yield '$a => 1,';
1066+
yield 'default => 2,';
1067+
}),
1068+
'}',
1069+
], false, false),
1070+
);
1071+
}
1072+
10281073
public function testDumpFileWithNoImportsHasNoExtraNewline() : void
10291074
{
10301075
$this->generator = new CodeGenerator('App\\Services');

0 commit comments

Comments
 (0)