Skip to content

[HttpFoundation] PHP 8.1 deprecation notice in IpUtils::checkIp() #43350

Closed
@W0rma

Description

@W0rma

Symfony version(s) affected: 5.3

Description
I stumbled upon the method IpUtils::checkIp():

    /**
     * Checks if an IPv4 or IPv6 address is contained in the list of given IPs or subnets.
     *
     * @param string|array $ips List of IPs or subnets (can be a string if only a single one)
     *
     * @return bool Whether the IP is valid
     */
    public static function checkIp(?string $requestIp, $ips)
    {
        if (!\is_array($ips)) {
            $ips = [$ips];
        }

        $method = substr_count($requestIp, ':') > 1 ? 'checkIp6' : 'checkIp4';

        foreach ($ips as $ip) {
            if (self::$method($requestIp, $ip)) {
                return true;
            }
        }

        return false;
    }

substr_count() is called even if $requestIp === null which causes a warning in PHP 8.1 ("substr_count(): Passing null to parameter #1 ($haystack) of type string is deprecated").

How to reproduce

Possible Solution
I guess we could just avoid calling substr_count() if the request ip is null.

Additional context

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions