Search code examples
phpphpstan

PHPstan does not like preg_match parameters


PHPstan gives this error

Parameter #4 $flags of function preg_match expects TFlags of 0|256|512|768, int given.

to this code

    function my_preg_match(
        string $pattern,
        string $subject,
        array &$matches = null,
        int $flags = 0,
        int $offset = 0): bool
    {
        $result = preg_match($pattern, $subject, $matches, $flags, $offset);

        if ($result === 1) {
            return true;
        }

        if ($result === 0) {
            return false;
        }

        throw new Exception(preg_last_error_msg());
    }

The code works.

Pleas explain me the error and help me fixing it to satisfy PHPstan.


Solution

  • Add the following to the function's docblock to match preg_match()'s function signature.

    /**
    * @param 0|256|512|768 $flags
    */
    

    See: