Skip to content

Commit

Permalink
Merge pull request #10815 from pilif/filter_var-named-args
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan committed Mar 20, 2024
2 parents 98eab1f + debffec commit 2c1ac98
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Psalm\Type\Union;
use UnexpectedValueException;

use function array_flip;
use function array_search;
use function in_array;
use function is_array;
Expand Down Expand Up @@ -48,7 +49,16 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Psalm\Type\Union;
use UnexpectedValueException;

use function array_flip;
use function is_array;
use function is_int;

Expand Down Expand Up @@ -37,7 +38,16 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
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();
Expand Down
8 changes: 8 additions & 0 deletions tests/FunctionCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,9 @@ function foo(string $s) : void {
],
'filterInput' => [
'code' => '<?php
$a = filter_input(INPUT_GET, "foo", options: FILTER_FORCE_ARRAY);
assert(is_array($a));
function filterInt(string $s) : int {
$filtered = filter_var($s, FILTER_VALIDATE_INT);
if ($filtered === false) {
Expand Down Expand Up @@ -1110,6 +1113,11 @@ function filterFloatWithDefault(string $s) : float {
],
'filterVar' => [
'code' => '<?php
function namedArgs(): string {
$a = filter_var("a", options: FILTER_FORCE_ARRAY);
return $a[0];
}
function filterInt(string $s) : int {
$filtered = filter_var($s, FILTER_VALIDATE_INT);
if ($filtered === false) {
Expand Down

0 comments on commit 2c1ac98

Please sign in to comment.