From 0d006fbeaf45f5a5a79d7d8d141fc864aa7f7eeb Mon Sep 17 00:00:00 2001 From: Mateus Junges Date: Wed, 9 Oct 2024 11:57:43 -0300 Subject: [PATCH 1/3] Merge existing parameters with `with` parameters --- .../ArrayParametersOverViewWith.php | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) 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, ])), ] From ce9b139c59530a6593b92dd904c07dd74bb4707a Mon Sep 17 00:00:00 2001 From: Mateus Junges Date: Wed, 9 Oct 2024 11:57:53 -0300 Subject: [PATCH 2/3] Add test --- .../ArrayParametersOverViewWithTest.php | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/Formatting/Formatters/ArrayParametersOverViewWithTest.php b/tests/Formatting/Formatters/ArrayParametersOverViewWithTest.php index 7385899..ec682ce 100644 --- a/tests/Formatting/Formatters/ArrayParametersOverViewWithTest.php +++ b/tests/Formatting/Formatters/ArrayParametersOverViewWithTest.php @@ -208,4 +208,40 @@ 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); + } } From ce54dd6a18696f87f58e430ecc0151b4ba282bf7 Mon Sep 17 00:00:00 2001 From: Mateus Junges Date: Wed, 9 Oct 2024 15:30:29 -0300 Subject: [PATCH 3/3] Add test case for multiple chained with calls --- .../ArrayParametersOverViewWithTest.php | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/Formatting/Formatters/ArrayParametersOverViewWithTest.php b/tests/Formatting/Formatters/ArrayParametersOverViewWithTest.php index ec682ce..5fb20c7 100644 --- a/tests/Formatting/Formatters/ArrayParametersOverViewWithTest.php +++ b/tests/Formatting/Formatters/ArrayParametersOverViewWithTest.php @@ -244,4 +244,40 @@ function index() $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); + } }