diff --git a/src/Formatters/ArrayParametersOverViewWith.php b/src/Formatters/ArrayParametersOverViewWith.php index 4bebc9a..003a3b4 100644 --- a/src/Formatters/ArrayParametersOverViewWith.php +++ b/src/Formatters/ArrayParametersOverViewWith.php @@ -78,16 +78,32 @@ public function enterNode(Node $node): Node|int|null return null; } + $existingArrayItems = []; + + // Check if `view` has a second argument and extract array items + if (isset($node->getArgs()[1])) { + $secondArg = $node->getArgs()[1]->value; + if ($secondArg instanceof Array_) { + $existingArrayItems = $secondArg->items; + } + } + + // Merge existing array items with `with` parameters + $mergedItems = array_merge( + $existingArrayItems, + array_map(function ($viewWith) { + return new ArrayItem( + $viewWith[1]->value, + $viewWith[0]->value, + ); + }, array_reverse($this->viewWith)) + ); + return new FuncCall( new Name('view'), [ $node->getArgs()[0], - new Arg(new Array_(array_map(function ($viewWith) { - return new ArrayItem( - $viewWith[1]->value, - $viewWith[0]->value, - ); - }, array_reverse($this->viewWith)), [ + new Arg(new Array_($mergedItems, [ 'kind' => Array_::KIND_SHORT, ])), ] diff --git a/tests/Formatting/Formatters/ArrayParametersOverViewWithTest.php b/tests/Formatting/Formatters/ArrayParametersOverViewWithTest.php index 7385899..5fb20c7 100644 --- a/tests/Formatting/Formatters/ArrayParametersOverViewWithTest.php +++ b/tests/Formatting/Formatters/ArrayParametersOverViewWithTest.php @@ -208,4 +208,76 @@ function edit() $this->assertSame($expected, $formatted); } + + /** @test */ + public function it_merges_parameters_passed_using_with_and_array_parameters(): void + { + $file = <<<'file' + 1234])->with('first', 'yes'); + } + } + file; + + $expected = <<<'file' + 1234, 'first' => 'yes']); + } + } + file; + + $formatted = (new TFormat)->format(new ArrayParametersOverViewWith($file)); + + $this->assertSame($expected, $formatted); + } + + /** @test */ + public function it_merges_array_parameters_with_multiple_with_calls(): void + { + $file = <<<'file' + 1234])->with('first', 'yes')->with('second', 'yes'); + } + } + file; + + $expected = <<<'file' + 1234, 'first' => 'yes', 'second' => 'yes']); + } + } + file; + + $formatted = (new TFormat)->format(new ArrayParametersOverViewWith($file)); + + $this->assertSame($expected, $formatted); + } }