11<?php
2+ declare (strict_types=1 );
23/*
34 * This file is part of the PHP-IPAddress library.
45 *
1819 */
1920namespace Leth \IPAddress \IP ;
2021use \Leth \IPAddress \IP , \Leth \IPAddress \IPv4 , \Leth \IPAddress \IPv6 ;
22+ use ReturnTypeWillChange ;
2123
2224/**
2325 * An abstract representation of an IP Address.
2628 */
2729abstract class Address implements \ArrayAccess
2830{
29- const IP_VERSION = -1 ;
30- const FORMAT_FULL = 0 ;
31- const FORMAT_COMPACT = 1 ;
31+ public const IP_VERSION = -1 ;
32+ public const FORMAT_FULL = 0 ;
33+ public const FORMAT_COMPACT = 1 ;
3234
3335 /**
3436 * Internal representation of the address. Format may vary.
35- * @var mixed
37+ * @var numeric
3638 */
3739 protected $ address ;
3840
3941 /**
4042 * Create an IP address object from the supplied address.
4143 *
42- * @param string $address The address to represent.
43- * @return IP\Address An instance of a subclass of IP\Address; either IPv4\Address or IPv6\Address
44+ * @param IP\Address|int|string|\Math_BigInteger $address The address to represent.
45+ *
46+ * @return \Leth\IPAddress\IP\Address|\Leth\IPAddress\IPv4\Address|\Leth\IPAddress\IPv6\Address An instance of a subclass of IP\Address; either IPv4\Address or IPv6\Address
4447 */
45- public static function factory ($ address )
48+ public static function factory (IP \ Address | int | string | \ Math_BigInteger $ address ): IP \ Address | Ipv4 \ Address | IPv6 \ Address
4649 {
47- if ($ address instanceof IP \Address )
50+ if ($ address instanceof self )
4851 {
4952 return $ address ;
5053 }
51- elseif (is_int ($ address ) OR (is_string ($ address ) AND filter_var ($ address , FILTER_VALIDATE_IP , FILTER_FLAG_IPV4 )))
54+ elseif (is_int ($ address ) || (is_string ($ address ) && filter_var ($ address , FILTER_VALIDATE_IP , FILTER_FLAG_IPV4 )))
5255 {
5356 return IPv4 \Address::factory ($ address );
5457 }
55- elseif ($ address instanceof \Math_BigInteger OR (is_string ($ address ) AND filter_var ($ address , FILTER_VALIDATE_IP , FILTER_FLAG_IPV6 )))
58+ elseif ($ address instanceof \Math_BigInteger || (is_string ($ address ) && filter_var ($ address , FILTER_VALIDATE_IP , FILTER_FLAG_IPV6 )))
5659 {
5760 return IPv6 \Address::factory ($ address );
5861 }
@@ -72,17 +75,17 @@ public static function factory($address)
7275 * @param IP\Address $b The right hand side of the comparison.
7376 * @return int The result of the comparison.
7477 */
75- public static function compare (IP \Address $ a , IP \Address $ b )
78+ public static function compare (IP \Address $ a , IP \Address $ b ): int
7679 {
7780 return $ a ->compare_to ($ b );
7881 }
7982
8083 /**
8184 * Create a new IP Address object.
8285 *
83- * @param string $address The address to represent.
86+ * @param int| string $address The address to represent.
8487 */
85- protected function __construct ($ address )
88+ protected function __construct (int | string $ address )
8689 {
8790 $ this ->address = $ address ;
8891 }
@@ -93,99 +96,99 @@ protected function __construct($address)
9396 * @param integer|\Math_BigInteger $value
9497 * @return IP\Address An address representing the result of the operation.
9598 */
96- public abstract function add ($ value );
99+ abstract public function add ($ value ): Address ;
97100
98101 /**
99102 * Subtract the given value from this address.
100103 *
101104 * @param integer|\Math_BigInteger $value
102105 * @return IP\Address An address representing the result of the operation.
103106 */
104- public abstract function subtract ($ value );
107+ abstract public function subtract ($ value ): Address ;
105108
106109 /**
107110 * Compute the bitwise AND of this address and another.
108111 *
109112 * @param IP\Address $other The other operand.
110113 * @return IP\Address An address representing the result of the operation.
111114 */
112- public abstract function bitwise_and (IP \Address $ other );
115+ abstract public function bitwise_and (IP \Address $ other );
113116
114117 /**
115118 * Compute the bitwise OR of this address and another.
116119 *
117120 * @param IP\Address $other The other operand.
118121 * @return IP\Address An address representing the result of the operation.
119122 */
120- public abstract function bitwise_or (IP \Address $ other );
123+ abstract public function bitwise_or (IP \Address $ other ): Address ;
121124
122125 /**
123126 * Compute the bitwise XOR (Exclusive OR) of this address and another.
124127 *
125128 * @param IP\Address $other The other operand.
126129 * @return IP\Address An address representing the result of the operation.
127130 */
128- public abstract function bitwise_xor (IP \Address $ other );
131+ abstract public function bitwise_xor (IP \Address $ other ): Address ;
129132
130133 /**
131134 * Compute the bitwise NOT of this address.
132135 *
133136 * @return IP\Address An address representing the result of the operation.
134137 */
135- public abstract function bitwise_not ();
138+ abstract public function bitwise_not (): Address ;
136139
137140 /**
138141 * Compare this IP Address with another.
139142 *
140143 * @param IP\Address $other The instance to compare to.
141144 * @return int The result of the comparison.
142145 */
143- public abstract function compare_to (IP \Address $ other );
146+ abstract public function compare_to (IP \Address $ other ): int ;
144147
145148 /**
146149 * Convert this object to a string representation
147150 *
148151 * @return string This IP address expressed as a string.
149152 */
150- public function __toString ()
153+ public function __toString (): string
151154 {
152- return $ this ->format (IP \Address ::FORMAT_COMPACT );
155+ return $ this ->format (self ::FORMAT_COMPACT );
153156 }
154157
155158 /**
156159 * Return the string representation of the address
157160 *
158161 * @return string This IP address expressed as a string.
159162 */
160- public abstract function format ($ mode );
163+ abstract public function format (int $ mode ): string ;
161164
162165 /**
163166 * Check that this instance and the supplied instance are of the same class.
164167 *
165168 * @param IP\Address $other The object to check.
166169 * @throws \InvalidArgumentException if objects are of the same class.
167170 */
168- protected function check_types (IP \Address $ other )
171+ protected function check_types (IP \Address $ other ): void
169172 {
170- if (get_class ($ this ) != get_class ($ other ))
173+ if (get_class ($ this ) !== get_class ($ other )) {
171174 throw new \InvalidArgumentException ('Incompatible types. ' );
175+ }
172176 }
173177
174178 /**
175179 * Get the specified octet from this address.
176180 *
177181 * @param integer $number
178- * @return integer An octet value the result of the operation.
182+ *
183+ * @return ?integer An octet value the result of the operation.
179184 */
180- public function get_octet ($ number )
185+ public function get_octet (int $ number ): ? int
181186 {
182187 $ address = unpack ("C* " , $ this ->address );
183188 $ index = (($ number >= 0 ) ? $ number : count ($ address ) + $ number );
184189 $ index ++;
185- if (!isset ($ address [$ index ]))
186- //throw new \InvalidArgumentException("The specified octet out of range");
187- return NULL ;
188- return $ address [$ index ];
190+
191+ return $ address [$ index ] ?? null ;
189192 }
190193
191194 /**
@@ -194,18 +197,19 @@ public function get_octet($number)
194197 * @param integer $offset
195198 * @return boolean
196199 */
197- public function offsetExists ($ offset )
200+ public function offsetExists ($ offset ): bool
198201 {
199- return ($ this ->get_octet ($ offset ) != NULL );
202+ return ($ this ->get_octet ($ offset ) !== NULL );
200203 }
201204
202205 /**
203206 * Get the octet value from index
204207 *
205208 * @param integer $offset
206- * @return integer
209+ *
210+ * @return integer|null
207211 */
208- public function offsetGet ($ offset )
212+ public function offsetGet ($ offset ): ? int
209213 {
210214 return $ this ->get_octet ($ offset );
211215 }
@@ -217,7 +221,8 @@ public function offsetGet($offset)
217221 * @param mixed $value
218222 * @throws \LogicException
219223 */
220- public function offsetSet ($ offset , $ value )
224+ #[ReturnTypeWillChange]
225+ public function offsetSet ($ offset , $ value ): mixed
221226 {
222227 throw new \LogicException ('Operation unsupported ' );
223228 }
@@ -228,7 +233,8 @@ public function offsetSet($offset, $value)
228233 * @param integer $offset
229234 * @throws \LogicException
230235 */
231- public function offsetUnset ($ offset )
236+ #[ReturnTypeWillChange]
237+ public function offsetUnset ($ offset ): void
232238 {
233239 throw new \LogicException ('Operation unsupported ' );
234240 }
0 commit comments