Skip to content

filter_var() with a bool and FILTER_VALIDATE_DOMAIN / FILTER_VALIDATE_REGEXP is inferred as false #14125

Closed

Description

@orklah

filter_var() with a bool input and FILTER_VALIDATE_DOMAIN or FILTER_VALIDATE_REGEXP is inferred as false, but PHP converts the bool to a string first (true → '1', false → '') and filters that.

<?php
function f(bool $b) {
    $a = filter_var(true, FILTER_VALIDATE_DOMAIN); // PHP: '1', Psalm: false
    $c = filter_var($b, FILTER_VALIDATE_DOMAIN); // PHP: string, Psalm: false
    $d = filter_var(false, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '/^$/']]); // PHP: '', Psalm: false
    $e = filter_var($b, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '/^1?$/']]); // PHP: string, Psalm: false
}

https://psalm.dev/r/08f9458f66

Cause: the FILTER_VALIDATE_DOMAIN and FILTER_VALIDATE_REGEXP branches of FilterUtils::getReturnType() have no TBool handling, so a bool only sets $can_fail.

Activity

  1. psalm-github-bot commented on Nov 17, 2026

    @psalm-github-bot

    I found these snippets:

    https://psalm.dev/r/08f9458f66
    <?php
    function f(bool $b) {
        /** @psalm-trace $a */
        $a = filter_var(true, FILTER_VALIDATE_DOMAIN);
        /** @psalm-trace $c */
        $c = filter_var($b, FILTER_VALIDATE_DOMAIN);
        /** @psalm-trace $d */
        $d = filter_var(false, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '/^$/']]);
        /** @psalm-trace $e */
        $e = filter_var($b, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '/^1?$/']]);
    }
    Psalm output (using commit a32c0d4):
    
    INFO: Trace - 4:5 - $a: false
    
    INFO: Trace - 6:5 - $c: false
    
    INFO: Trace - 8:5 - $d: false
    
    INFO: Trace - 10:5 - $e: false
    
    INFO: UnusedVariable - 4:5 - $a is never referenced or the value is not used
    
    INFO: UnusedVariable - 6:5 - $c is never referenced or the value is not used
    
    INFO: UnusedVariable - 8:5 - $d is never referenced or the value is not used
    
    INFO: UnusedVariable - 10:5 - $e is never referenced or the value is not used
    
    INFO: MissingReturnType - 2:10 - Method f does not have a return type, expecting void
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions