aboutsummaryrefslogtreecommitdiffstats
path: root/src/Net/IPv6.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Net/IPv6.php')
-rw-r--r--src/Net/IPv6.php72
1 files changed, 19 insertions, 53 deletions
diff --git a/src/Net/IPv6.php b/src/Net/IPv6.php
index da25f83..db93152 100644
--- a/src/Net/IPv6.php
+++ b/src/Net/IPv6.php
@@ -1,55 +1,15 @@
<?php
+// SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue
+// SPDX-License-Identifier: BSD-3-Clause
+
declare(strict_types=1);
-/**
- * SimplePie
- *
- * A PHP-Based RSS and Atom Feed Framework.
- * Takes the hard work out of managing a complete RSS/Atom solution.
- *
- * Copyright (c) 2004-2022, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification, are
- * permitted provided that the following conditions are met:
- *
- * * Redistributions of source code must retain the above copyright notice, this list of
- * conditions and the following disclaimer.
- *
- * * Redistributions in binary form must reproduce the above copyright notice, this list
- * of conditions and the following disclaimer in the documentation and/or other materials
- * provided with the distribution.
- *
- * * Neither the name of the SimplePie Team nor the names of its contributors may be used
- * to endorse or promote products derived from this software without specific prior
- * written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
- * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
- * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
- * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- * @package SimplePie
- * @copyright 2004-2016 Ryan Parman, Sam Sneddon, Ryan McCue
- * @author Ryan Parman
- * @author Sam Sneddon
- * @author Ryan McCue
- * @link http://simplepie.org/ SimplePie
- * @license http://www.opensource.org/licenses/bsd-license.php BSD License
- */
namespace SimplePie\Net;
/**
* Class to validate and to work with IPv6 addresses.
*
- * @package SimplePie
- * @subpackage HTTP
* @copyright 2003-2005 The PHP Group
* @license http://www.opensource.org/licenses/bsd-license.php
* @link http://pear.php.net/package/Net_IPv6
@@ -63,7 +23,7 @@ class IPv6
/**
* Uncompresses an IPv6 address
*
- * RFC 4291 allows you to compress concecutive zero pieces in an address to
+ * RFC 4291 allows you to compress consecutive zero pieces in an address to
* '::'. This method expects a valid IPv6 address and expands the '::' to
* the required number of zero pieces.
*
@@ -78,7 +38,7 @@ class IPv6
* @param string $ip An IPv6 address
* @return string The uncompressed IPv6 address
*/
- public static function uncompress($ip)
+ public static function uncompress(string $ip)
{
$c1 = -1;
$c2 = -1;
@@ -123,7 +83,7 @@ class IPv6
/**
* Compresses an IPv6 address
*
- * RFC 4291 allows you to compress concecutive zero pieces in an address to
+ * RFC 4291 allows you to compress consecutive zero pieces in an address to
* '::'. This method expects a valid IPv6 address and compresses consecutive
* zero pieces to '::'.
*
@@ -134,14 +94,14 @@ class IPv6
* @param string $ip An IPv6 address
* @return string The compressed IPv6 address
*/
- public static function compress($ip)
+ public static function compress(string $ip)
{
// Prepare the IP to be compressed
$ip = self::uncompress($ip);
$ip_parts = self::split_v6_v4($ip);
// Replace all leading zeros
- $ip_parts[0] = preg_replace('/(^|:)0+([0-9])/', '\1\2', $ip_parts[0]);
+ $ip_parts[0] = (string) preg_replace('/(^|:)0+([0-9])/', '\1\2', $ip_parts[0]);
// Find bunches of zeros
if (preg_match_all('/(?:^|:)(?:0(?::|$))+/', $ip_parts[0], $matches, PREG_OFFSET_CAPTURE)) {
@@ -154,6 +114,7 @@ class IPv6
}
}
+ assert($pos !== null, 'For PHPStan: Since the regex matched, there is at least one match. And because the pattern is non-empty, the loop will always end with $pos ≥ 1.');
$ip_parts[0] = substr_replace($ip_parts[0], '::', $pos, $max);
}
@@ -174,12 +135,13 @@ class IPv6
* 0:0:0:0:0:FFFF:129.144.52.38
*
* @param string $ip An IPv6 address
- * @return array [0] contains the IPv6 represented part, and [1] the IPv4 represented part
+ * @return array{string, string} [0] contains the IPv6 represented part, and [1] the IPv4 represented part
*/
- private static function split_v6_v4($ip)
+ private static function split_v6_v4(string $ip): array
{
if (strpos($ip, '.') !== false) {
$pos = strrpos($ip, ':');
+ assert($pos !== false, 'For PHPStan: IPv6 address must contain colon, since split_v6_v4 is only ever called after uncompress.');
$ipv6_part = substr($ip, 0, $pos);
$ipv4_part = substr($ip, $pos + 1);
return [$ipv6_part, $ipv4_part];
@@ -196,7 +158,7 @@ class IPv6
* @param string $ip An IPv6 address
* @return bool true if $ip is a valid IPv6 address
*/
- public static function check_ipv6($ip)
+ public static function check_ipv6(string $ip)
{
$ip = self::uncompress($ip);
[$ipv6, $ipv4] = self::split_v6_v4($ip);
@@ -222,7 +184,11 @@ class IPv6
// Check the value is valid
$value = hexdec($ipv6_part);
- if (dechex($value) !== strtolower($ipv6_part) || $value < 0 || $value > 0xFFFF) {
+ if ($value < 0 || $value > 0xFFFF) {
+ return false;
+ }
+ assert(is_int($value), 'For PHPStan: $value is only float when $ipv6_part > PHP_INT_MAX');
+ if (dechex($value) !== strtolower($ipv6_part)) {
return false;
}
}
@@ -249,7 +215,7 @@ class IPv6
* @param string $ip An IPv6 address
* @return bool true if $ip is a valid IPv6 address
*/
- public static function checkIPv6($ip)
+ public static function checkIPv6(string $ip)
{
return self::check_ipv6($ip);
}