Skip to content

Improve support of FILTER_REQUIRE_ARRAY#4410

Merged
ondrejmirtes merged 2 commits intophpstan:2.1.xfrom
VincentLanglet:filterVarArray
Oct 6, 2025
Merged

Improve support of FILTER_REQUIRE_ARRAY#4410
ondrejmirtes merged 2 commits intophpstan:2.1.xfrom
VincentLanglet:filterVarArray

Conversation

@VincentLanglet
Copy link
Copy Markdown
Contributor

@VincentLanglet VincentLanglet commented Oct 5, 2025

Closes phpstan/phpstan#11863

After this merge I'll work on phpstan/phpstan#11485
I think it will require to change hasFlag signature to return a TrinaryLogic

cc @herndlm

assertType('array<int>|null', filter_var($mixed, FILTER_VALIDATE_INT, ['flags' => FILTER_REQUIRE_ARRAY|FILTER_NULL_ON_FAILURE]));
assertType('array<string, int>|false', filter_var($stringMixedMap, FILTER_VALIDATE_INT, ['flags' => FILTER_REQUIRE_ARRAY]));
assertType('array<string, int>|null', filter_var($stringMixedMap, FILTER_VALIDATE_INT, ['flags' => FILTER_REQUIRE_ARRAY|FILTER_NULL_ON_FAILURE]));
assertType('array<int|false>|false', filter_var($mixed, FILTER_VALIDATE_INT, ['flags' => FILTER_REQUIRE_ARRAY]));
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mixed can either be

  • array with valid values
  • array with invalid values (will be array)
  • non-array (will be false)

assertType('array<string, int>|null', filter_var($stringMixedMap, FILTER_VALIDATE_INT, ['flags' => FILTER_REQUIRE_ARRAY|FILTER_NULL_ON_FAILURE]));
assertType('array<int|false>|false', filter_var($mixed, FILTER_VALIDATE_INT, ['flags' => FILTER_REQUIRE_ARRAY]));
assertType('array<int|null>|null', filter_var($mixed, FILTER_VALIDATE_INT, ['flags' => FILTER_REQUIRE_ARRAY|FILTER_NULL_ON_FAILURE]));
assertType('array<string, int|false>', filter_var($stringMixedMap, FILTER_VALIDATE_INT, ['flags' => FILTER_REQUIRE_ARRAY]));
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we always have an array so the result cannot be false (but an array with false values)

@ondrejmirtes ondrejmirtes merged commit 782382d into phpstan:2.1.x Oct 6, 2025
545 of 550 checks passed
@ondrejmirtes
Copy link
Copy Markdown
Member

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Return type of filter_var() with array argument FILTER_REQUIRE_ARRAY option

2 participants