Skip to content

Fix filter_var throw-on-failure detection#6042

Merged
staabm merged 1 commit into
phpstan:2.2.xfrom
simPod:fix-filter-var-throw-on-failure-upstream
Jul 13, 2026
Merged

Fix filter_var throw-on-failure detection#6042
staabm merged 1 commit into
phpstan:2.2.xfrom
simPod:fix-filter-var-throw-on-failure-upstream

Conversation

@simPod

@simPod simPod commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Problem

filter_var() accepts options and flags in its third argument.

FilterVarThrowTypeExtension was reading the fourth argument instead, so valid calls like this were not treated as throwing:

filter_var($value, FILTER_VALIDATE_MAC, FILTER_THROW_ON_FAILURE);

That made catch (Filter\FilterFailedException) look dead even though the return type helper already narrowed the return type correctly.

Fix

Read the third argument in FilterVarThrowTypeExtension and reuse FilterFunctionReturnTypeHelper::hasFlag().

This keeps throw detection consistent for both forms:

filter_var($value, FILTER_VALIDATE_MAC, FILTER_THROW_ON_FAILURE);
filter_var($value, FILTER_VALIDATE_MAC, ['flags' => FILTER_THROW_ON_FAILURE]);

@simPod simPod marked this pull request as ready for review July 12, 2026 08:37
@phpstan-bot

Copy link
Copy Markdown
Collaborator

This pull request has been marked as ready for review.

): ?Type
{
if (!isset($funcCall->getArgs()[3])) {
if (!isset($funcCall->getArgs()[2])) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

thats a interessting bug.

seems the tests in #4495 were not sufficient

@canvural could you double check?

@staabm staabm merged commit 3f716d4 into phpstan:2.2.x Jul 13, 2026
678 checks passed
@staabm

staabm commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

thank you @simPod

@simPod simPod deleted the fix-filter-var-throw-on-failure-upstream branch July 14, 2026 07:37
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.

4 participants