Skip to content

False positive for array_filter #3559

@GrahamCampbell

Description

@GrahamCampbell

Consider the following example:

$f = function (...$args): void {
    foreach (array_filter($args) as $k => $v) {}
};

We know that $args is an array, so array_filter($args) must be an array also, but PHPStan warns that it could be null, incorrectly:

Argument of an invalid type (array|null) supplied for foreach, only iterables are supported. 

The full context in which this was discovered:

    /**
     * @param mixed $value as passed via Request transfer options.
     */
    private function add_debug(RequestInterface $request, array &$options, $value, array &$params): void
    {
        if ($value === false) {
            return;
        }

        static $map = [
            \STREAM_NOTIFY_CONNECT       => 'CONNECT',
            \STREAM_NOTIFY_AUTH_REQUIRED => 'AUTH_REQUIRED',
            \STREAM_NOTIFY_AUTH_RESULT   => 'AUTH_RESULT',
            \STREAM_NOTIFY_MIME_TYPE_IS  => 'MIME_TYPE_IS',
            \STREAM_NOTIFY_FILE_SIZE_IS  => 'FILE_SIZE_IS',
            \STREAM_NOTIFY_REDIRECTED    => 'REDIRECTED',
            \STREAM_NOTIFY_PROGRESS      => 'PROGRESS',
            \STREAM_NOTIFY_FAILURE       => 'FAILURE',
            \STREAM_NOTIFY_COMPLETED     => 'COMPLETED',
            \STREAM_NOTIFY_RESOLVE       => 'RESOLVE',
        ];
        static $args = ['severity', 'message', 'message_code',
            'bytes_transferred', 'bytes_max'];

        $value = Utils::debugResource($value);
        $ident = $request->getMethod() . ' ' . $request->getUri()->withFragment('');
        self::addNotification(
            $params,
            static function (int $code, ...$passed) use ($ident, $value, $map, $args): void {
                \fprintf($value, '<%s> [%s] ', $ident, $map[$code]);
                foreach (\array_filter($passed) as $i => $v) {
                    \fwrite($value, $args[$i] . ': "' . $v . '" ');
                }
                \fwrite($value, "\n");
            }
        );
    }
 ------ --------------------------------------------------------------------- 
  Line   Handler/StreamHandler.php                                            
 ------ --------------------------------------------------------------------- 
  544    Argument of an invalid type (array|null) supplied for foreach, only  
         iterables are supported.                                             
 ------ --------------------------------------------------------------------- 

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions