Skip to content

Conversation

BackEndTea
Copy link
Contributor

@BackEndTea BackEndTea commented Jan 1, 2020

This adds support for the FILTER_NULL_ON_FAILURE flag.

This doesn't cover all possible edge cases of the filter_var function, namely:

  • Default values

Fixes phpstan/phpstan#1959

@BackEndTea BackEndTea force-pushed the fix/filter_var_nullable branch from bc4027b to 12f1892 Compare January 1, 2020 12:42
This works for all validates, but not sanitizes. For now this lacks support for bit masks, e.g.: `FILTER_FLAG_IPV4 | FILTER_NULL_ON_FAILURE`. The reason for this is that there is also a `FILTER_FORCE_ARRAY` flag, which puts the result in an array. This option, or bit masks, aren't supported either right now. So adding support for bitmasks without `FILTER_FORCE_ARRAY` would result in more false positives
@BackEndTea BackEndTea force-pushed the fix/filter_var_nullable branch from 12f1892 to e83948c Compare January 1, 2020 13:07
@ondrejmirtes
Copy link
Member

Hi, build is failing, but I like the code, will merge once the build passes :)

This includes support for bit masks, and force array. It also updates the old implementation of retrieving the constant, to use the type system as well, allow usage of variables.
@BackEndTea BackEndTea force-pushed the fix/filter_var_nullable branch from 00d334e to bc1a93c Compare January 2, 2020 12:07
@BackEndTea
Copy link
Contributor Author

Small thing to note:

This does not support default values yet, as that has some weird behavior in combination with array inputs: https://3v4l.org/NbAhc. This wasn't supported before, so it shouldn't introduce any new errors.

@BackEndTea
Copy link
Contributor Author

This does introduce a dependency on ext-filter: https://travis-ci.org/phpstan/phpstan-src/jobs/631844108#L402 Which can be disabled according to the documentation.

Should we introduce an explicit dependency in composer.json?

@ondrejmirtes
Copy link
Member

@BackEndTea No, the code should check whether the constant exists first.

All of the constants got added way before php 7.1, so there is no need to check for each of them individually.
@BackEndTea BackEndTea force-pushed the fix/filter_var_nullable branch from f3ccc98 to b2955d2 Compare January 2, 2020 12:36
@BackEndTea
Copy link
Contributor Author

I've added the check, but only for 1 constant, as that should mean the rest of them are available as well, since all of them got introduced somewhere around PHP 5.2. So checking every single one individually doesn't add much benefit.

@ondrejmirtes
Copy link
Member

Great, thank you!

@ondrejmirtes ondrejmirtes merged commit e15cfcd into phpstan:master Jan 2, 2020
@BackEndTea BackEndTea deleted the fix/filter_var_nullable branch January 2, 2020 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants