From cb803c7eaf6c8907c4e8e7102dd160578a96a0c3 Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Tue, 2 Aug 2022 11:50:46 +0200 Subject: [PATCH] refactor: add few typehints --- src/IP.php | 13 +++++++------ src/Network.php | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/IP.php b/src/IP.php index 44cd003..dbac0a0 100644 --- a/src/IP.php +++ b/src/IP.php @@ -2,6 +2,7 @@ namespace IPTools; use IPTools\Exception\IpException; +use LogicException; /** * @author Safarov Alisher @@ -46,7 +47,7 @@ public function __toString() } /** - * @param string ip + * @param string $ip * @return IP */ public static function parse($ip) @@ -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)); @@ -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; } diff --git a/src/Network.php b/src/Network.php index 96b6d4a..f19f5ae 100644 --- a/src/Network.php +++ b/src/Network.php @@ -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 {