From f0c8627a42cc6ae5f2801551d0f8ea8ebbd3ee95 Mon Sep 17 00:00:00 2001 From: Davey Shafik Date: Sun, 26 Jan 2025 02:44:36 -0800 Subject: [PATCH] feat: Add the ability to strip extra params --- src/Bag/Attributes/StripExtraParameters.php | 13 +++ src/Bag/Pipelines/InputPipeline.php | 2 + src/Bag/Pipelines/Pipes/ExtraParameters.php | 6 ++ .../Pipelines/Pipes/StripExtraParameters.php | 36 +++++++ .../Values/StripExtraParametersBag.php | 19 ++++ .../Pipes/StripExtraParametersTest.php | 99 +++++++++++++++++++ 6 files changed, 175 insertions(+) create mode 100644 src/Bag/Attributes/StripExtraParameters.php create mode 100644 src/Bag/Pipelines/Pipes/StripExtraParameters.php create mode 100644 tests/Fixtures/Values/StripExtraParametersBag.php create mode 100644 tests/Unit/Pipelines/Pipes/StripExtraParametersTest.php diff --git a/src/Bag/Attributes/StripExtraParameters.php b/src/Bag/Attributes/StripExtraParameters.php new file mode 100644 index 0000000..4bb8a66 --- /dev/null +++ b/src/Bag/Attributes/StripExtraParameters.php @@ -0,0 +1,13 @@ +bagClassname), StripExtraParameters::class) ?? false) !== false) { + return $input; + } + $extra = collect(); $input->values->each(function (mixed $_, string $key) use ($input, $extra) { if ($input->params->has($key)) { diff --git a/src/Bag/Pipelines/Pipes/StripExtraParameters.php b/src/Bag/Pipelines/Pipes/StripExtraParameters.php new file mode 100644 index 0000000..beacd03 --- /dev/null +++ b/src/Bag/Pipelines/Pipes/StripExtraParameters.php @@ -0,0 +1,36 @@ + $input + * @return BagInput + */ + public function __invoke(BagInput $input): BagInput + { + if ($input->variadic) { + return $input; + } + + $extra = collect(); + $input->values->each(function (mixed $_, string $key) use ($input, $extra) { + if ($input->params->has($key)) { + return; + } + + $extra->add($key); + }); + + $input->values = $input->values->except($extra); + + return $input; + } +} diff --git a/tests/Fixtures/Values/StripExtraParametersBag.php b/tests/Fixtures/Values/StripExtraParametersBag.php new file mode 100644 index 0000000..7386187 --- /dev/null +++ b/tests/Fixtures/Values/StripExtraParametersBag.php @@ -0,0 +1,19 @@ + 'Davey Shafik', + 'age' => 40, + 'email' => 'davey@php.net', + 'test' => true, + ])); + $input = (new ProcessParameters())($input, fn (BagInput $input) => $input); + $input = (new MapInput())($input, fn (BagInput $input) => $input); + $input = (new IsVariadic())($input, fn (BagInput $input) => $input); + $input = (new ExtraParameters())($input, fn (BagInput $input) => $input); + + $pipe = new StripExtraParameters(); + $input = $pipe($input); + + expect($input) + ->toBeInstanceOf(BagInput::class) + ->and($input->values) + ->toBeInstanceOf(Collection::class) + ->and($input->values->toArray()) + ->toBe([ + 'name' => 'Davey Shafik', + 'age' => 40, + 'email' => 'davey@php.net', + ]); +}); + + +test('it does not error on non variadic with extra positional parameters ignore', function () { + $input = new BagInput(StripExtraParametersBag::class, collect([ + 'Davey Shafik', + 40, + 'davey@php.net', + true, + ])); + $input = (new ProcessParameters())($input, fn (BagInput $input) => $input); + $input = (new ProcessArguments())($input, fn (BagInput $input) => $input); + $input = (new MapInput())($input, fn (BagInput $input) => $input); + $input = (new IsVariadic())($input, fn (BagInput $input) => $input); + $input = (new ExtraParameters())($input, fn (BagInput $input) => $input); + + $pipe = new StripExtraParameters(); + $input = $pipe($input); + + expect($input) + ->toBeInstanceOf(BagInput::class) + ->and($input->values) + ->toBeInstanceOf(Collection::class) + ->and($input->values->toArray()) + ->toBe([ + 'name' => 'Davey Shafik', + 'age' => 40, + 'email' => 'davey@php.net', + ]); +}); + +test('it does not strip extra parameters with variadic', function () { + $input = new BagInput(VariadicBag::class, collect([ + 'name' => 'Davey Shafik', + 'age' => 40, + 'email' => 'davey@php.net', + 'test' => true, + ])); + $input = (new ProcessParameters())($input, fn (BagInput $input) => $input); + $input = (new MapInput())($input, fn (BagInput $input) => $input); + $input = (new IsVariadic())($input, fn (BagInput $input) => $input); + $input = (new ExtraParameters())($input, fn (BagInput $input) => $input); + + $pipe = new StripExtraParameters(); + $input = $pipe($input); + + expect($input) + ->toBeInstanceOf(BagInput::class) + ->and($input->values) + ->toBeInstanceOf(Collection::class) + ->and($input->values->toArray()) + ->toBe([ + 'name' => 'Davey Shafik', + 'age' => 40, + 'email' => 'davey@php.net', + 'test' => true, + ]); +});