Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/IP.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace IPTools;

use IPTools\Exception\IpException;
use LogicException;

/**
* @author Safarov Alisher <alisher.safarov@outlook.com>
Expand Down Expand Up @@ -46,7 +47,7 @@ public function __toString()
}

/**
* @param string ip
* @param string $ip
* @return IP
*/
public static function parse($ip)
Expand Down Expand Up @@ -102,10 +103,9 @@ public static function parseHex($hexIP)
}

/**
* @param string|int $longIP
* @return IP
*/
public static function parseLong($longIP, $version=self::IP_V4)
public static function parseLong(int|string $longIP, $version=self::IP_V4)
{
if ($version === self::IP_V4) {
$ip = new self(long2ip($longIP));
Expand All @@ -131,17 +131,18 @@ public static function parseInAddr($inAddr)
}

/**
* @phpstan-return IP::IP_V4|IP::IP_V6
* @return string
*/
public function getVersion()
{
$version = '';

if (filter_var(inet_ntop($this->in_addr), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$version = self::IP_V4;
} elseif (filter_var(inet_ntop($this->in_addr), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
$version = self::IP_V6;
}
} else {
throw new LogicException("Invalid IP address format");
}

return $version;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Network.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ public function __toString()
* @param string $data
* @return Network
*/
public static function parse($data)
public static function parse(string $data)
{
if (preg_match('~^(.+?)/(\d+)$~', $data, $matches)) {
$ip = IP::parse($matches[1]);
$netmask = self::prefix2netmask((int)$matches[2], $ip->getVersion());
} elseif (strpos($data,' ')) {
list($ip, $netmask) = explode(' ', $data, 2);
[$ip, $netmask] = explode(' ', $data, 2);
$ip = IP::parse($ip);
$netmask = IP::parse($netmask);
} else {
Expand Down