Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FilterVarArrayDynamicReturnTypeExtension #2257

Merged
merged 7 commits into from Apr 11, 2023
Merged

Add FilterVarArrayDynamicReturnTypeExtension #2257

merged 7 commits into from Apr 11, 2023

Conversation

zonuexe
Copy link
Contributor

@zonuexe zonuexe commented Feb 25, 2023

Add a return type extension for filter_var_array() and filter_input_array().

This function is defined with the following signature:

filter_var_array(array $array, array|int $options = FILTER_DEFAULT, bool $add_empty = true): array|false|null

This function supports three types of filters:

// bit flag
$values = filter_var_array($_POST, FILTER_VALIDATE_INT);

// simple array
$values = filter_var_array($_POST, [
    'key' => FILTER_VALIDATE_INT,
]);

// complex array
$values = filter_var_array($_POST,[
    'key' => [
        'filter' => FILTER_VALIDATE_INT,
        'options' => ['min_range' => 1, 'max_range' => 10],
    ],
]);

The array keys returned from filter_var_array() change depending on how the filter is passed, so the implementation is complex.

filter passed by key is determined by
bit flag $options
simple array $array
complex array $array

This PR was implemented thanks to @herndlm's FilterFunctionReturnTypeHelper #2190 and #2010.

@zonuexe zonuexe changed the title Add FilterVarArrayDynamicReturnTypeExtension Add FilterVarArrayDynamicReturnTypeExtension Feb 25, 2023
@herndlm
Copy link
Contributor

herndlm commented Feb 25, 2023

I was hoping it might be possible to reuse that thing, but didn't plan it through :) feel also free to change it if needed. Reminds me that I also still need to finish filter_input..

@zonuexe
Copy link
Contributor Author

zonuexe commented Feb 25, 2023

Since the processing for filter_input_array() is almost common to filter_var_array(), I started moving the code into FilterFunctionReturnTypeHelper.

@zonuexe zonuexe marked this pull request as draft February 25, 2023 17:04
@herndlm
Copy link
Contributor

herndlm commented Feb 25, 2023

I started the filter_input support via #2010 but this is not the latest state and I wasn't happy yet with the local refactor. Will try to push it soon.
Or do you also have already something (in mind)?
Moving the part that deals with arrays into the helper makes sense to me 👍

@zonuexe
Copy link
Contributor Author

zonuexe commented Feb 25, 2023

@herndlm No changes planned except for the two methods I added to the FilterFunctionReturnTypeHelper.

@zonuexe zonuexe marked this pull request as ready for review February 25, 2023 18:11
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@zonuexe
Copy link
Contributor Author

zonuexe commented Feb 25, 2023

I intended to implement FilterInputArrayDynamicReturnTypeExtension as a pared-down version of FilterVarArrayDynamicReturnTypeExtension, but it's probably simpler to support both functions in one extension. So the method move to FilterFunctionReturnTypeHelper has been reverted.

@herndlm
Copy link
Contributor

herndlm commented Feb 25, 2023

a missing interesting test case for filter_input_array might be one with the FILTER_NULL_ON_FAILURE flag set.

An array value will be false if the filter fails, or null if the variable is not set. Or if the flag FILTER_NULL_ON_FAILURE is used, it returns false if the variable is not set and null if the filter fails.

I handled this incorrectly in #2010 initially but added a new method in the helper now that should deal with this and maybe you can make use of it here too.

@herndlm
Copy link
Contributor

herndlm commented Apr 8, 2023

hi @zonuexe, the filter_input stuff has been merged, maybe it is useful here in some way

@zonuexe
Copy link
Contributor Author

zonuexe commented Apr 9, 2023

@herndlm Thanks for the reminder!

@zonuexe
Copy link
Contributor Author

zonuexe commented Apr 9, 2023

@ondrejmirtes Could you please review this PR?

@ondrejmirtes ondrejmirtes merged commit f8fd310 into phpstan:1.10.x Apr 11, 2023
370 of 377 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

@zonuexe zonuexe deleted the feature/improve-filter_var_array branch April 11, 2023 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants