1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-11 08:49:52 +01:00

Merge pull request #10815 from pilif/filter_var-named-args

This commit is contained in:
Bruce Weirdan 2024-03-20 01:49:26 +01:00 committed by GitHub
commit 2c1ac98439
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 2 deletions

View File

@ -12,6 +12,7 @@ use Psalm\Type\Atomic\TKeyedArray;
use Psalm\Type\Union; use Psalm\Type\Union;
use UnexpectedValueException; use UnexpectedValueException;
use function array_flip;
use function array_search; use function array_search;
use function in_array; use function in_array;
use function is_array; use function is_array;
@ -48,7 +49,16 @@ class FilterInputReturnTypeProvider implements FunctionReturnTypeProviderInterfa
throw new UnexpectedValueException('Expected StatementsAnalyzer not StatementsSource'); 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(); $function_id = $event->getFunctionId();
$code_location = $event->getCodeLocation(); $code_location = $event->getCodeLocation();
$codebase = $statements_analyzer->getCodebase(); $codebase = $statements_analyzer->getCodebase();

View File

@ -9,6 +9,7 @@ use Psalm\Type;
use Psalm\Type\Union; use Psalm\Type\Union;
use UnexpectedValueException; use UnexpectedValueException;
use function array_flip;
use function is_array; use function is_array;
use function is_int; use function is_int;
@ -37,7 +38,16 @@ final class FilterVarReturnTypeProvider implements FunctionReturnTypeProviderInt
throw new UnexpectedValueException(); 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(); $function_id = $event->getFunctionId();
$code_location = $event->getCodeLocation(); $code_location = $event->getCodeLocation();
$codebase = $statements_analyzer->getCodebase(); $codebase = $statements_analyzer->getCodebase();

View File

@ -1075,6 +1075,9 @@ class FunctionCallTest extends TestCase
], ],
'filterInput' => [ 'filterInput' => [
'code' => '<?php 'code' => '<?php
$a = filter_input(INPUT_GET, "foo", options: FILTER_FORCE_ARRAY);
assert(is_array($a));
function filterInt(string $s) : int { function filterInt(string $s) : int {
$filtered = filter_var($s, FILTER_VALIDATE_INT); $filtered = filter_var($s, FILTER_VALIDATE_INT);
if ($filtered === false) { if ($filtered === false) {
@ -1110,6 +1113,11 @@ class FunctionCallTest extends TestCase
], ],
'filterVar' => [ 'filterVar' => [
'code' => '<?php 'code' => '<?php
function namedArgs(): string {
$a = filter_var("a", options: FILTER_FORCE_ARRAY);
return $a[0];
}
function filterInt(string $s) : int { function filterInt(string $s) : int {
$filtered = filter_var($s, FILTER_VALIDATE_INT); $filtered = filter_var($s, FILTER_VALIDATE_INT);
if ($filtered === false) { if ($filtered === false) {