From debffecd13bb353d76f1123e9cf809b2c4005068 Mon Sep 17 00:00:00 2001 From: Philip Hofstetter Date: Wed, 13 Mar 2024 09:11:17 +0100 Subject: [PATCH] add support for named arguments for filter_var and filter_input this fixes #10809 --- .../FilterInputReturnTypeProvider.php | 12 +++++++++++- .../FilterVarReturnTypeProvider.php | 12 +++++++++++- tests/FunctionCallTest.php | 8 ++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/Psalm/Internal/Provider/ReturnTypeProvider/FilterInputReturnTypeProvider.php b/src/Psalm/Internal/Provider/ReturnTypeProvider/FilterInputReturnTypeProvider.php index 09b32b2aa..6b410c306 100644 --- a/src/Psalm/Internal/Provider/ReturnTypeProvider/FilterInputReturnTypeProvider.php +++ b/src/Psalm/Internal/Provider/ReturnTypeProvider/FilterInputReturnTypeProvider.php @@ -12,6 +12,7 @@ use Psalm\Type\Atomic\TKeyedArray; use Psalm\Type\Union; use UnexpectedValueException; +use function array_flip; use function array_search; use function in_array; use function is_array; @@ -48,7 +49,16 @@ class FilterInputReturnTypeProvider implements FunctionReturnTypeProviderInterfa throw new UnexpectedValueException('Expected StatementsAnalyzer not StatementsSource'); } - $call_args = $event->getCallArgs(); + $arg_names = array_flip(['type', 'var_name', 'filter', 'options']); + $call_args = []; + foreach ($event->getCallArgs() as $idx => $arg) { + if (isset($arg->name)) { + $call_args[$arg_names[$arg->name->name]] = $arg; + } else { + $call_args[$idx] = $arg; + } + } + $function_id = $event->getFunctionId(); $code_location = $event->getCodeLocation(); $codebase = $statements_analyzer->getCodebase(); diff --git a/src/Psalm/Internal/Provider/ReturnTypeProvider/FilterVarReturnTypeProvider.php b/src/Psalm/Internal/Provider/ReturnTypeProvider/FilterVarReturnTypeProvider.php index c6ea6a15f..76f6caa62 100644 --- a/src/Psalm/Internal/Provider/ReturnTypeProvider/FilterVarReturnTypeProvider.php +++ b/src/Psalm/Internal/Provider/ReturnTypeProvider/FilterVarReturnTypeProvider.php @@ -9,6 +9,7 @@ use Psalm\Type; use Psalm\Type\Union; use UnexpectedValueException; +use function array_flip; use function is_array; use function is_int; @@ -37,7 +38,16 @@ final class FilterVarReturnTypeProvider implements FunctionReturnTypeProviderInt throw new UnexpectedValueException(); } - $call_args = $event->getCallArgs(); + $arg_names = array_flip(['value', 'filter', 'options']); + $call_args = []; + foreach ($event->getCallArgs() as $idx => $arg) { + if (isset($arg->name)) { + $call_args[$arg_names[$arg->name->name]] = $arg; + } else { + $call_args[$idx] = $arg; + } + } + $function_id = $event->getFunctionId(); $code_location = $event->getCodeLocation(); $codebase = $statements_analyzer->getCodebase(); diff --git a/tests/FunctionCallTest.php b/tests/FunctionCallTest.php index d66755266..833b38af4 100644 --- a/tests/FunctionCallTest.php +++ b/tests/FunctionCallTest.php @@ -1075,6 +1075,9 @@ class FunctionCallTest extends TestCase ], 'filterInput' => [ 'code' => ' [ 'code' => '