S1lentium / IPTools

PHP Library for manipulating network addresses (IPv4 and IPv6)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

php8 support

azpanel opened this issue · comments

During inheritance of Iterator: Uncaught think\exception\ErrorException: Return type of IPTools\Network::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/wwwroot/project/vendor/s1lentium/iptools/src/Network.php:326

This error is due to the fact that in PHP 8.1, return types have been added to the methods of built-in interfaces and classes, a change that may cause some compatibility issues. In your code, the return type of IPTools\Network::current() method is not compatible with the built-in Iterator::current(): mixed, hence the error.

There are two ways to solve this problem:

  1. You can add the #[\ReturnTypeWillChange] attribute to the IPTools\Network::current() method to temporarily suppress this notice. This is a temporary solution and not a long-term one. This attribute might be removed in future versions of PHP. The code for this solution is as follows:
#[\ReturnTypeWillChange]
public function current()
{
    // ... your code here ...
}
  1. A better solution is to modify the return type of the IPTools\Network::current() method to be compatible with Iterator::current(): mixed. You need to check the implementation of this method and ensure it can return a value of any type (mixed type). If it cannot, you may need to modify the implementation of this method or the design of the entire class. The code for this solution might be as follows:
public function current(): mixed
{
    // ... your code here ...
}

Please note that both solutions require you to have the permission to modify the source code. If you cannot modify the source code, you may need to contact the maintainers of this library and ask them to fix the issue.

Call code

public static function calculatingIpv6Subnets(string $ipv6_cidr): array
    {
        $subnets_64 = [];
        $networks = \IPTools\Network::parse($ipv6_cidr)->moveTo('64');
        foreach ($networks as $network) {
            $subnets_64[] = (string) $network;
        }
        return $subnets_64;
    }