diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ed71648..b7d7cf8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -7,20 +7,26 @@ jobs: strategy: max-parallel: 3 matrix: - os: [ubuntu-latest] - php: ["5.6", "7.0", "7.1", "7.2", "7.3", "7.4"] + os: [ubuntu-latest, windows-latest, macos-latest] + php: ["8.1", "8.2", "8.3"] math_biginteger_mode: [INTERNAL, GMP, BCMATH] + include: + - os: ubuntu-latest + phpstan: yes - name: PHP ${{ matrix.php }} test with bigint mode ${{ matrix.math_biginteger_mode }} + name: "PHP ${{ matrix.php }} (bigint mode: ${{ matrix.math_biginteger_mode }}) (OS: ${{ matrix.os }})" runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v3 - - name: Install PHP - uses: shivammathur/setup-php@master + - name: Setup PHP + uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} - extension-csv: xdebug, dom, gmp, bcmath + extensions: xdebug, dom, gmp, bcmath + coverage: xdebug + env: + fail-fast: true - name: Validate composer.json and composer.lock run: composer validate @@ -32,3 +38,6 @@ jobs: run: composer run-script test env: MATH_BIGINTEGER_MODE: ${{ matrix.math_biginteger_mode }} + + - if: ${{ matrix.phpstan }} + uses: php-actions/phpstan@v3 \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..16d71a0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/vendor/ +/.idea/ +composer.phar +.phpunit.result.cache \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..dc25a0a --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,12 @@ +# Changelog + +## [2.0.0] - 2019-02-15 + +### Added + +- PHP 8.1 support [@mrcnpdlk](https://github.com/mrcnpdlk). + +### Changed + +- PHP 8.1 is now the minimum required version [@mrcnpdlk](https://github.com/mrcnpdlk). + diff --git a/classes/Leth/IPAddress/IP/Address.php b/classes/Leth/IPAddress/IP/Address.php index 20ec091..0f1eba3 100755 --- a/classes/Leth/IPAddress/IP/Address.php +++ b/classes/Leth/IPAddress/IP/Address.php @@ -1,4 +1,5 @@ compare_to($b); } @@ -80,9 +83,9 @@ public static function compare(IP\Address $a, IP\Address $b) /** * Create a new IP Address object. * - * @param string $address The address to represent. + * @param int|string $address The address to represent. */ - protected function __construct($address) + protected function __construct(int|string $address) { $this->address = $address; } @@ -93,7 +96,7 @@ protected function __construct($address) * @param integer|\Math_BigInteger $value * @return IP\Address An address representing the result of the operation. */ - public abstract function add($value); + abstract public function add($value): Address; /** * Subtract the given value from this address. @@ -101,7 +104,7 @@ public abstract function add($value); * @param integer|\Math_BigInteger $value * @return IP\Address An address representing the result of the operation. */ - public abstract function subtract($value); + abstract public function subtract($value): Address; /** * Compute the bitwise AND of this address and another. @@ -109,7 +112,7 @@ public abstract function subtract($value); * @param IP\Address $other The other operand. * @return IP\Address An address representing the result of the operation. */ - public abstract function bitwise_and(IP\Address $other); + abstract public function bitwise_and(IP\Address $other); /** * Compute the bitwise OR of this address and another. @@ -117,7 +120,7 @@ public abstract function bitwise_and(IP\Address $other); * @param IP\Address $other The other operand. * @return IP\Address An address representing the result of the operation. */ - public abstract function bitwise_or(IP\Address $other); + abstract public function bitwise_or(IP\Address $other): Address; /** * Compute the bitwise XOR (Exclusive OR) of this address and another. @@ -125,14 +128,14 @@ public abstract function bitwise_or(IP\Address $other); * @param IP\Address $other The other operand. * @return IP\Address An address representing the result of the operation. */ - public abstract function bitwise_xor(IP\Address $other); + abstract public function bitwise_xor(IP\Address $other): Address; /** * Compute the bitwise NOT of this address. * * @return IP\Address An address representing the result of the operation. */ - public abstract function bitwise_not(); + abstract public function bitwise_not(): Address; /** * Compare this IP Address with another. @@ -140,16 +143,16 @@ public abstract function bitwise_not(); * @param IP\Address $other The instance to compare to. * @return int The result of the comparison. */ - public abstract function compare_to(IP\Address $other); + abstract public function compare_to(IP\Address $other): int; /** * Convert this object to a string representation * * @return string This IP address expressed as a string. */ - public function __toString() + public function __toString(): string { - return $this->format(IP\Address::FORMAT_COMPACT); + return $this->format(self::FORMAT_COMPACT); } /** @@ -157,7 +160,7 @@ public function __toString() * * @return string This IP address expressed as a string. */ - public abstract function format($mode); + abstract public function format(int $mode): string; /** * Check that this instance and the supplied instance are of the same class. @@ -165,27 +168,27 @@ public abstract function format($mode); * @param IP\Address $other The object to check. * @throws \InvalidArgumentException if objects are of the same class. */ - protected function check_types(IP\Address $other) + protected function check_types(IP\Address $other): void { - if (get_class($this) != get_class($other)) + if (get_class($this) !== get_class($other)) { throw new \InvalidArgumentException('Incompatible types.'); + } } /** * Get the specified octet from this address. * * @param integer $number - * @return integer An octet value the result of the operation. + * + * @return ?integer An octet value the result of the operation. */ - public function get_octet($number) + public function get_octet(int $number): ?int { $address = unpack("C*", $this->address); $index = (($number >= 0) ? $number : count($address) + $number); $index++; - if (!isset($address[$index])) - //throw new \InvalidArgumentException("The specified octet out of range"); - return NULL; - return $address[$index]; + + return $address[$index] ?? null; } /** @@ -194,18 +197,19 @@ public function get_octet($number) * @param integer $offset * @return boolean */ - public function offsetExists($offset) + public function offsetExists($offset): bool { - return ($this->get_octet($offset) != NULL); + return ($this->get_octet($offset) !== NULL); } /** * Get the octet value from index * * @param integer $offset - * @return integer + * + * @return integer|null */ - public function offsetGet($offset) + public function offsetGet($offset): ?int { return $this->get_octet($offset); } @@ -217,7 +221,8 @@ public function offsetGet($offset) * @param mixed $value * @throws \LogicException */ - public function offsetSet($offset, $value) + #[ReturnTypeWillChange] + public function offsetSet($offset, $value): mixed { throw new \LogicException('Operation unsupported'); } @@ -228,7 +233,8 @@ public function offsetSet($offset, $value) * @param integer $offset * @throws \LogicException */ - public function offsetUnset($offset) + #[ReturnTypeWillChange] + public function offsetUnset($offset): void { throw new \LogicException('Operation unsupported'); } diff --git a/classes/Leth/IPAddress/IP/NetworkAddress.php b/classes/Leth/IPAddress/IP/NetworkAddress.php index 5daa61f..cfd3859 100755 --- a/classes/Leth/IPAddress/IP/NetworkAddress.php +++ b/classes/Leth/IPAddress/IP/NetworkAddress.php @@ -1,4 +1,5 @@ cidr) + if ($cidr !== NULL && $cidr !== $address->cidr) { $class = get_class($address); return new $class($address->address, $cidr); @@ -85,22 +88,29 @@ public static function factory($address, $cidr = NULL) return $address; } - $parts = explode('/', $address, 2); - if (count($parts) == 2) { - if ($cidr == NULL) - // Parse CIDR from $address variable because $cidr is null - list($address, $cidr) = $parts; - else - // Ignore CIDR into $address variable - list($address) = $parts; + if(is_string($address)){ + $parts = explode('/', $address, 2); + if (count($parts) === 2) { + if ($cidr === NULL) + // Parse CIDR from $address variable because $cidr is null + { + [$address, $cidr] = $parts; + } + else + // Ignore CIDR into $address variable + { + [$address] = $parts; + } + } } if (is_string($cidr)) { - if ( ! ctype_digit($cidr)) + if ( ! ctype_digit($cidr)) { throw new \InvalidArgumentException("Malformed CIDR suffix '$cidr'."); + } - $cidr = intval($cidr); + $cidr = (int)$cidr; } if ( ! $address instanceof IP\Address) @@ -108,12 +118,15 @@ public static function factory($address, $cidr = NULL) $address = IP\Address::factory($address); } - if ($address instanceof IPv4\Address) + if ($address instanceof IPv4\Address) { return new IPv4\NetworkAddress($address, $cidr); - elseif ($address instanceof IPv6\Address) + } + elseif ($address instanceof IPv6\Address) { return new IPv6\NetworkAddress($address, $cidr); - else - throw new \InvalidArgumentException('Unsupported IP Address type \''.get_class($address).'\'.'); + } + else { + throw new \InvalidArgumentException('Unsupported IP Address type \'' . get_class($address) . '\'.'); + } } /** @@ -122,11 +135,12 @@ public static function factory($address, $cidr = NULL) * This method is a wrapper for the compare_to method and is useful in callback situations, e.g. * usort($addresses, array('IP\NetworkAddress', 'compare')); * - * @param IP\Address $a The left hand side of the comparison. - * @param IP\Address $b The right hand side of the comparison. + * @param \Leth\IPAddress\IP\NetworkAddress $a The left hand side of the comparison. + * @param \Leth\IPAddress\IP\NetworkAddress $b The right hand side of the comparison. + * * @return int The result of the comparison. */ - public static function compare(IP\NetworkAddress $a, IP\NetworkAddress $b) + public static function compare(IP\NetworkAddress $a, IP\NetworkAddress $b): int { return $a->compare_to($b); } @@ -136,10 +150,10 @@ public static function compare(IP\NetworkAddress $a, IP\NetworkAddress $b) * * Ajacent blocks can only be merged if they belong to the same parent block * - * @param array NetworkAddresses to merge + * @param array $network_addresses NetworkAddresses to merge * @return array NetworkAddresses remaining after merging */ - public static function merge(array $network_addresses) + public static function merge(array $network_addresses): array { $net_addr_index = array(); foreach ($network_addresses as $net_addr) { @@ -153,7 +167,7 @@ public static function merge(array $network_addresses) $out = array(); foreach ($net_addr_index as $version => $cidr_addrs) { - $max = $version == 4 ? IPv4\NetworkAddress::MAX_SUBNET : IPv6\NetworkAddress::MAX_SUBNET; + $max = $version === 4 ? IPv4\NetworkAddress::MAX_SUBNET : IPv6\NetworkAddress::MAX_SUBNET; // smallest networks first (largest cidr) // We have to loop by index because we modify the array while we iterate for ($cidr = $max; $cidr > 0; $cidr--) @@ -161,13 +175,12 @@ public static function merge(array $network_addresses) if (! array_key_exists($cidr, $cidr_addrs)) continue; $net_addrs = $cidr_addrs[$cidr]; - if (count($net_addrs) == 1) + if (count($net_addrs) === 1) { $out[] = $net_addrs[0]; continue; } - - usort($net_addrs, array('Leth\IPAddress\IP\NetworkAddress', 'compare')); + usort($net_addrs, array(__CLASS__, 'compare')); $last_added = NULL; for ($i = 0; $i < count($net_addrs) - 1; $i++) { @@ -199,28 +212,29 @@ public static function merge(array $network_addresses) /** * Construct an IP\NetworkAddress. * - * @param IPAddress $address The IP Address of the host - * @param string $cidr The CIDR size of the network + * @param \Leth\IPAddress\IP\Address $address The IP Address of the host + * @param mixed|null $cidr The CIDR size of the network */ - protected function __construct(IP\Address $address, $cidr) + protected function __construct(IP\Address $address, mixed $cidr) { // Default CIDR equal single host if ($cidr === NULL) { $cidr = static::MAX_SUBNET; } - if ( ! is_int($cidr) OR $cidr < 0 OR $cidr > static::MAX_SUBNET) - throw new \InvalidArgumentException("Invalid CIDR '.$cidr'.Invalid type or out of range for class ".get_class($this)."."); + if ( ! is_int($cidr) || $cidr < 0 || $cidr > static::MAX_SUBNET) { + throw new \InvalidArgumentException("Invalid CIDR '.$cidr'.Invalid type or out of range for class " . get_class($this) . "."); + } $this->address = $address; $this->cidr = $cidr; } - public function get_address() + public function get_address(): Address { return $this->address; } - public function get_cidr() + public function get_cidr(): int { return $this->cidr; } @@ -228,12 +242,13 @@ public function get_cidr() /** * Get the NetworkAddress immediately enclosing this one * - * @return IP\NetworkAddress + * @return \Leth\IPAddress\IPv4\NetworkAddress|\Leth\IPAddress\IP\NetworkAddress|\Leth\IPAddress\IPv6\NetworkAddress|null */ - public function get_parent() + public function get_parent(): IPv4\NetworkAddress|\Leth\IPAddress\IP\NetworkAddress|IPv6\NetworkAddress|null { - if ($this->cidr == 0) - return NULL; + if ($this->cidr === 0) { + return null; + } $parent_cidr = $this->cidr - 1; $parent_addr = $this->address->bitwise_and(static::generate_subnet_mask($parent_cidr)); return static::factory($parent_addr, $parent_cidr); @@ -244,7 +259,7 @@ public function get_parent() * * @return IP\Address */ - public function get_network_start() + public function get_network_start(): Address { return $this->address->bitwise_and($this->get_subnet_mask()); } @@ -254,7 +269,7 @@ public function get_network_start() * * @return IP\Address */ - public function get_network_end() + public function get_network_end(): Address { return $this->get_subnet_mask()->bitwise_not()->bitwise_or($this->address); } @@ -264,13 +279,14 @@ public function get_network_end() * * @return integer */ - public function get_NetworkAddress_count() + public function get_NetworkAddress_count(): int { - return pow(2, static::MAX_SUBNET - $this->cidr); + return 2 ** (static::MAX_SUBNET - $this->cidr); } - public function get_address_in_network($offset, $from_start = NULL) + public function get_address_in_network(int|\Math_BigInteger $offset, bool $from_start = NULL): Address { + $positive = false; if (is_int($offset)) { $positive = ($offset >= 0); @@ -285,7 +301,7 @@ public function get_address_in_network($offset, $from_start = NULL) } else { - $from_start = ($from_start == TRUE); + $from_start = ($from_start === TRUE); } if ($from_start) @@ -301,7 +317,7 @@ public function get_address_in_network($offset, $from_start = NULL) { if (is_int($offset)) { - $offset = abs($offset); + $offset = (int)abs($offset); } elseif ($offset instanceOf \Math_BigInteger) { @@ -309,10 +325,12 @@ public function get_address_in_network($offset, $from_start = NULL) } } - if ($positive AND $from_start) + if ($positive && $from_start) { return $point->add($offset); - else + } + else { return $point->subtract($offset); + } } /** @@ -320,9 +338,9 @@ public function get_address_in_network($offset, $from_start = NULL) * * @return boolean */ - public function is_network_identifier() + public function is_network_identifier(): bool { - return $this->address->compare_to($this->get_network_start()) == 0; + return $this->address->compare_to($this->get_network_start()) === 0; } /** @@ -330,7 +348,7 @@ public function is_network_identifier() * * @return IP\NetworkAddress */ - public function get_network_identifier() + public function get_network_identifier(): NetworkAddress { $classname = get_class($this); return new $classname($this->get_network_start(), $this->cidr); @@ -341,7 +359,7 @@ public function get_network_identifier() * * @return IP\Address */ - public function get_subnet_mask() + public function get_subnet_mask(): Address { return static::generate_subnet_mask($this->cidr); } @@ -349,10 +367,11 @@ public function get_subnet_mask() /** * Calculates whether two subnets share any portion of their address space. * - * @param IP\Address $other The other subnet to compare to. - * @return void + * @param \Leth\IPAddress\IP\NetworkAddress $other The other subnet to compare to. + * + * @return bool */ - public function shares_subnet_space(IP\NetworkAddress $other) + public function shares_subnet_space(IP\NetworkAddress $other): bool { $this->check_types($other); @@ -360,22 +379,23 @@ public function shares_subnet_space(IP\NetworkAddress $other) if ($this->cidr > $other->cidr) { - list($first, $other) = array($other, $first); + [$first, $other] = array($other, $first); } return ($first->get_network_start()->compare_to($other->get_network_start()) <= 0) - AND + && ($first->get_network_end() ->compare_to($other->get_network_end() ) >= 0); } /** * Checks whether this subnet encloses the supplied subnet. * - * @param IP\Address $other Subnet to test against. + * @param \Leth\IPAddress\IP\NetworkAddress $other Subnet to test against. + * * @return boolean */ - public function encloses_subnet(IP\NetworkAddress $other) + public function encloses_subnet(IP\NetworkAddress $other): bool { $this->check_types($other); @@ -421,10 +441,11 @@ protected function check_types($other) * @return void * @throws \InvalidArgumentException If they are not of the same type. */ - protected function check_IP_version(IP\Address $other) + protected function check_IP_version(IP\Address $other): void { - if ($other::IP_VERSION !== static::IP_VERSION) - throw new \InvalidArgumentException("Incompatible types ('".get_class($this)."' and '".get_class($other)."')."); + if ($other::IP_VERSION !== static::IP_VERSION) { + throw new \InvalidArgumentException("Incompatible types ('" . get_class($this) . "' and '" . get_class($other) . "')."); + } } /** @@ -458,11 +479,12 @@ public function __toString() /** * Find a block of a given size within the smallest network address among the blocks given * - * @param array $blocks An array of network addresses to search in. + * @param array $blocks An array of network addresses to search in. * @param integer $block_size The desired network block size + * * @return array(IP\NetworkAddress found, IP\NetworkAddress within), or array(NULL, NULL) if none found. */ - public static function get_block_in_smallest($blocks, $block_size) + public static function get_block_in_smallest(array $blocks, int $block_size): array { $smallest = NULL; $smallest_cidr = 0; @@ -470,7 +492,7 @@ public static function get_block_in_smallest($blocks, $block_size) foreach ($blocks as $block) { $cidr = $block->get_cidr(); - if ($cidr == $block_size) + if ($cidr === $block_size) { return array($block, $block); } @@ -485,19 +507,20 @@ public static function get_block_in_smallest($blocks, $block_size) } } - if ($smallest) - return array(static::factory($smallest, $block_size), $smallest); - else - return array(NULL, NULL); + if ($smallest) { + return [static::factory($smallest, $block_size), $smallest]; + } else { + return [null, null]; + } } /** * Find the portion of this network address block that does not overlap with the given blocks. * - * @param array $used An array of network addresses to exclude, each of type IP\NetworkAddress + * @param array $excluding An array of network addresses to exclude, each of type IP\NetworkAddress * @return array */ - public function excluding($excluding) + public function excluding(array $excluding): array { $candidates = array($this); foreach ($excluding as $exclude) @@ -535,11 +558,12 @@ public function excluding($excluding) * Split the network address to create 2^n network addresses. * * @param int $times The number of times to split the network address + * * @return array */ - public function split($times = 1) + public function split(int $times = 1): array { - if (0 == $times) + if (0 === $times) return array($this); $new_cidr = $this->cidr + $times; @@ -552,7 +576,7 @@ public function split($times = 1) $out = array(); $pos = $this->address; - for ($i=0; $i < pow(2, $times); $i++) + for ($i=0; $i < (2 ** $times); $i++) { $out[] = static::factory($pos, $new_cidr); $pos = $pos->add($offset); @@ -567,7 +591,8 @@ public function split($times = 1) * * @return NetworkAddressIterator */ - public function getIterator() + #[ReturnTypeWillChange] + public function getIterator(): NetworkAddressIterator { return new NetworkAddressIterator($this); } @@ -590,7 +615,7 @@ public function toArray() * * @return integer */ - public function count() + public function count(): int { return $this->get_NetworkAddress_count(); } diff --git a/classes/Leth/IPAddress/IP/NetworkAddressIterator.php b/classes/Leth/IPAddress/IP/NetworkAddressIterator.php index 5354e8e..af23ffa 100644 --- a/classes/Leth/IPAddress/IP/NetworkAddressIterator.php +++ b/classes/Leth/IPAddress/IP/NetworkAddressIterator.php @@ -1,4 +1,5 @@ position = $this->network->get_network_start(); } @@ -58,7 +59,7 @@ public function rewind() * * @return IP\Address */ - public function current() + public function current(): IP\Address { return $this->position; } @@ -69,7 +70,7 @@ public function current() * * @return string */ - public function key() + public function key(): string { return $this->position->__toString(); } @@ -80,7 +81,7 @@ public function key() * * @return void */ - public function next() + public function next(): void { $this->position = $this->position->add(1); } @@ -91,7 +92,7 @@ public function next() * * @return boolean */ - public function valid() + public function valid(): bool { return ($this->position->compare_to($this->network->get_network_end()) <= 0); } diff --git a/classes/Leth/IPAddress/IPv4/Address.php b/classes/Leth/IPAddress/IPv4/Address.php index 3e88c5d..cb233d2 100755 --- a/classes/Leth/IPAddress/IPv4/Address.php +++ b/classes/Leth/IPAddress/IPv4/Address.php @@ -1,4 +1,5 @@ compare(new \Math_BigInteger(pack('N', ip2long(static::MAX_IP)), 256)) > 0) + if ($address->compare(new \Math_BigInteger(pack('N', ip2long(static::MAX_IP)), 256)) > 0) { throw new \InvalidArgumentException("IP value out of range."); + } $address = str_pad($address->toBytes(), 4, chr(0), STR_PAD_LEFT); } @@ -63,41 +66,40 @@ protected function __construct($address) parent::__construct($address); } - protected static function _pack($address) + protected static function _pack(int $address): string { return pack('N', $address); } - protected static function _unpack($address) + protected static function _unpack(string $address): int { $out = unpack('N', $address); return $out[1]; } - public function add($value) + public function add($value): Address { if ($value instanceof \Math_BigInteger) { - $value = intval( (string) $value); + $value = (int)(string)$value; } return new IPv4\Address(static::_pack(static::_unpack($this->address) + $value)); } - public function subtract($value) + public function subtract($value): Address { if ($value instanceof \Math_BigInteger) { - $value = intval( (string) $value); + $value = (int)(string)$value; } return new IPv4\Address(static::_pack(static::_unpack($this->address) - $value)); } /** * Calculates the Bitwise & (AND) of a given IP address. - * @param IPv4Address $other is the ip to be compared against - * @return IPAddress + * @param IPv4\Address $other is the ip to be compared against */ - public function bitwise_and(IP\Address $other) + public function bitwise_and(IP\Address $other): Address { $this->check_types($other); return new IPv4\Address($this->address & $other->address); @@ -105,10 +107,10 @@ public function bitwise_and(IP\Address $other) /** * Calculates the Bitwise | (OR) of a given IP address. - * @param IPv4Address $other is the ip to be compared against - * @return IPAddress + * @param IP\Address $other is the ip to be compared against + * @return IPv4\Address */ - public function bitwise_or(IP\Address $other) + public function bitwise_or(IP\Address $other): IPv4\Address { $this->check_types($other); return new IPv4\Address($this->address | $other->address); @@ -116,10 +118,10 @@ public function bitwise_or(IP\Address $other) /** * Calculates the Bitwise ^ (XOR) of a given IP address. - * @param IPv4\Address $other is the ip to be compared against - * @return IP\Address + * @param IP\Address $other is the ip to be compared against + * @return IPv4\Address */ - public function bitwise_xor(IP\Address $other) + public function bitwise_xor(IP\Address $other): IPv4\Address { $this->check_types($other); return new IPv4\Address($this->address ^ $other->address); @@ -127,10 +129,9 @@ public function bitwise_xor(IP\Address $other) /** * Calculates the Bitwise ~ (NOT) of a given IP address. - * @param IPv4Address $other is the ip to be compared against - * @return IP\Address + * @return IPv4\Address */ - public function bitwise_not() + public function bitwise_not(): Address { return new IPv4\Address(~ $this->address); } @@ -140,7 +141,7 @@ public function bitwise_not() * * @return IPv6\Address */ - public function as_IPv6_address() + public function as_IPv6_address(): IPv6\Address { list( , $address) = unpack('H*', $this->address); $address = join(':', str_split($address, 4)); @@ -149,19 +150,22 @@ public function as_IPv6_address() return IPv6\Address::factory($address); } - public function compare_to(IP\Address $other) + public function compare_to(IP\Address $other): int { $this->check_types($other); - if ($this->address < $other->address) + if ($this->address < $other->address) { return -1; - elseif ($this->address > $other->address) + } + elseif ($this->address > $other->address) { return 1; - else + } + else { return 0; + } } - public function format($mode) + public function format(int $mode): string { $address = static::_unpack($this->address); switch ($mode) { diff --git a/classes/Leth/IPAddress/IPv4/NetworkAddress.php b/classes/Leth/IPAddress/IPv4/NetworkAddress.php index 5513f9c..134f699 100755 --- a/classes/Leth/IPAddress/IPv4/NetworkAddress.php +++ b/classes/Leth/IPAddress/IPv4/NetworkAddress.php @@ -1,4 +1,5 @@ 0) + if ($cidr > 0) { - $mask = (~$mask) << (static::MAX_SUBNET - $subnet); + $mask = (~$mask) << (static::MAX_SUBNET - $cidr); } return IPv4\Address::factory(implode('.', unpack('C4', pack('N', $mask)))); @@ -39,11 +40,11 @@ public static function generate_subnet_mask($subnet) /** * Gets the Global subnet mask for this IP Protocol + * @return IPv4\Address An IP Address representing the mask. * - * @return IP\Address An IP Address representing the mask. * @author Marcus Cobden */ - public static function get_global_netmask() + public static function get_global_netmask(): Address { return static::generate_subnet_mask(static::MAX_SUBNET); } @@ -51,40 +52,38 @@ public static function get_global_netmask() /** * Calculates the Network Address for this address (IPv4) or the first ip of the subnet (IPv6) * - * @return IPv4\NetworkAddress TODO */ - public function get_NetworkAddress() + public function get_NetworkAddress(): \Leth\IPAddress\IP\Address { return $this->get_network_start(); } - public function get_network_class() + public function get_network_class(): string { if ($this->cidr > 24) { - return '1/'.pow(2, $this->cidr - 24).' C'; + return '1/'. (2 ** ($this->cidr - 24)) .' C'; } elseif ($this->cidr > 16) { - return pow(2, 24 - $this->cidr).' C'; + return (2 ** (24 - $this->cidr)) .' C'; } elseif ($this->cidr > 8) { - return pow(2, 16 - $this->cidr).' B'; + return (2 ** (16 - $this->cidr)) .' B'; } else { - return pow(2, 8 - $this->cidr).' A'; + return (2 ** (8 - $this->cidr)) .' A'; } } /** * Calculates the Broadcast Address for this address. * - * @return IPv4\NetworkAddress */ - public function get_broadcast_address() + public function get_broadcast_address(): \Leth\IPAddress\IP\Address { return $this->get_network_end(); } diff --git a/classes/Leth/IPAddress/IPv6/Address.php b/classes/Leth/IPAddress/IPv6/Address.php index ae27b00..255c399 100755 --- a/classes/Leth/IPAddress/IPv6/Address.php +++ b/classes/Leth/IPAddress/IPv6/Address.php @@ -1,4 +1,5 @@ address, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff", 12) === 0; } - public function as_IPv4_address() + public function as_IPv4_address(): IPv4\Address { if(!$this->is_encoded_IPv4_address()) - throw \InvalidArgumentException('Not an IPv4 Address encoded in an IPv6 Address'); - list(,$hex) = unpack('H*', $this->address); + throw new \InvalidArgumentException('Not an IPv4 Address encoded in an IPv6 Address'); + [,$hex] = unpack('H*', $this->address); $parts = array_map('hexdec', array_slice(str_split($hex, 2), 12)); $address = implode('.', $parts); return IPv4\Address::factory($address); } - public function add($value) + public function add($value): Address { $left = new \Math_BigInteger($this->address, 256); $right = ($value instanceof \Math_BigInteger) ? $value : new \Math_BigInteger($value); return new IPv6\Address($left->add($right)); } - public function subtract($value) + public function subtract($value): IPv6\Address { $left = new \Math_BigInteger($this->address, 256); $right = ($value instanceof \Math_BigInteger) ? $value : new \Math_BigInteger($value); @@ -139,9 +140,9 @@ public function subtract($value) /** * Calculates the Bitwise & (AND) of a given IP address. * @param IP\Address $other is the ip to be compared against - * @return IP\Address + * @return IPv6\Address */ - public function bitwise_and(IP\Address $other) + public function bitwise_and(IP\Address $other): IPv6\Address { return $this->bitwise_operation('&', $other); } @@ -149,9 +150,9 @@ public function bitwise_and(IP\Address $other) /** * Calculates the Bitwise | (OR) of a given IP address. * @param IP\Address $other is the ip to be compared against - * @return IP\Address + * @return IPv6\Address */ - public function bitwise_or(IP\Address $other) + public function bitwise_or(IP\Address $other): IPv6\Address { return $this->bitwise_operation('|', $other); } @@ -159,73 +160,65 @@ public function bitwise_or(IP\Address $other) /** * Calculates the Bitwise ^ (XOR) of a given IP address. * @param IP\Address $other is the ip to be compared against - * @return IP\Address + * @return IPv6\Address */ - public function bitwise_xor(IP\Address $other) + public function bitwise_xor(IP\Address $other): IPv6\Address { return $this->bitwise_operation('^', $other); } /** * Calculates the Bitwise ~ (NOT) of a given IP address. - * @return IP\Address + * @return IPv6\Address */ - public function bitwise_not() + public function bitwise_not(): IPv6\Address { return $this->bitwise_operation('~'); } - public function bitwise_operation($operation, $other = NULL) + public function bitwise_operation(string $operation, IP\Address $other = NULL): IPv6\Address { - if ($operation != '~') + if ($operation !== '~') { $this->check_types($other); } - switch ($operation) { - case '&': - $result = $other->address & $this->address; - break; - case '|': - $result = $other->address | $this->address; - break; - case '^': - $result = $other->address ^ $this->address; - break; - case '~': - $result = ~$this->address; - break; - - default: - throw new \InvalidArgumentException('Unknown Operation type \''.$operation.'\'.'); - break; - } + $result = match ($operation) { + '&' => $other->address & $this->address, + '|' => $other->address | $this->address, + '^' => $other->address ^ $this->address, + '~' => ~$this->address, + default => throw new \InvalidArgumentException('Unknown Operation type \'' . $operation . '\'.'), + }; return new IPv6\Address($result); } - public function compare_to(IP\Address $other) + public function compare_to(IP\Address $other): int { $this->check_types($other); - if ($this->address < $other->address) + if ($this->address < $other->address) { return -1; - elseif ($this->address > $other->address) + } + elseif ($this->address > $other->address) { return 1; - else + } + else { return 0; + } } - public function format($mode) + public function format(int $mode): string { - list(,$hex) = unpack('H*', $this->address); + [, $hex] = unpack('H*', $this->address); $parts = str_split($hex, 4); - if ($mode === IPv6\Address::FORMAT_MAY_MAPPED_COMPACT) { + if ($mode === self::FORMAT_MAY_MAPPED_COMPACT) { if ($this->is_encoded_IPv4_address()) { - $mode = IPv6\Address::FORMAT_MAPPED_IPV4; + $mode = self::FORMAT_MAPPED_IPV4; } else { - $mode = IPv6\Address::FORMAT_COMPACT; + $mode = IP\Address::FORMAT_COMPACT; } } @@ -245,7 +238,6 @@ public function format($mode) list($a, $b) = str_split($parts[6], 2); list($c, $d) = str_split($parts[7], 2); return '::ffff:' . implode('.', array(hexdec($a), hexdec($b), hexdec($c), hexdec($d))); - break; case IP\Address::FORMAT_COMPACT: $best_pos = $zeros_pos = FALSE; diff --git a/classes/Leth/IPAddress/IPv6/NetworkAddress.php b/classes/Leth/IPAddress/IPv6/NetworkAddress.php index 4bfed17..d428061 100755 --- a/classes/Leth/IPAddress/IPv6/NetworkAddress.php +++ b/classes/Leth/IPAddress/IPv6/NetworkAddress.php @@ -1,4 +1,5 @@ =8.1", "pear/math_biginteger": "1.0.3" }, "require-dev": { - "phpunit/phpunit": "^4.8.36 || ^5.7 || ^6.5 || ^7.0" + "phpunit/phpunit": "^9", + "phpstan/phpstan": "^1", + "phpstan/phpstan-phpunit": "^1", + "phpstan/phpstan-deprecation-rules": "^1", + "phpstan/phpstan-strict-rules": "^1" }, "autoload": { "psr-4": { @@ -19,6 +24,7 @@ } }, "scripts": { - "test": "vendor/bin/phpunit --configuration phpunit.xml.dist --coverage-text" + "test": "vendor/bin/phpunit --configuration phpunit.xml.dist --coverage-text", + "phpstan": "phpstan analyse --memory-limit=2G --error-format=table" } } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..42d5da5 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,18 @@ +parameters: + level: 6 + paths: + - classes + - tests + fileExtensions: + - php + reportUnmatchedIgnoredErrors: false + checkMissingIterableValueType: false + checkGenericClassInNonGenericObjectType: false + parallel: + processTimeout: 300.0 + jobSize: 20 + maximumNumberOfProcesses: 32 + minimumNumberOfJobsPerProcess: 4 + ignoreErrors: + - '#Parameter \#1 \$x of class Math_BigInteger constructor expects optional, .* given#' +includes: diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 3cd7274..f768f18 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -8,7 +8,6 @@ convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" - syntaxCheck="false" bootstrap="./tests/bootstrap.php" > diff --git a/tests/IPAddressTest.php b/tests/IPAddressTest.php index 4c45f21..1dc73ac 100644 --- a/tests/IPAddressTest.php +++ b/tests/IPAddressTest.php @@ -11,7 +11,7 @@ class IP_Address_Test extends TestCase { - public function providerFactory() + public function providerFactory(): array { return array( array('IPv4\\Address', '127.0.0.1', '127.000.000.001', '127.0.0.1'), @@ -37,7 +37,7 @@ public function providerFactory() /** * @dataProvider providerFactory */ - public function testFactory($expected_class, $input, $full, $compact) + public function testFactory(string $expected_class, mixed $input, string $full, string $compact): void { $expected_class = 'Leth\\IPAddress\\'.$expected_class; $instances = array(IP\Address::factory($input), $expected_class::factory($input)); @@ -50,7 +50,7 @@ public function testFactory($expected_class, $input, $full, $compact) } } - public function providerFactoryException() + public function providerFactoryException(): array { return array( array('cake'), @@ -60,15 +60,16 @@ public function providerFactoryException() } /** - * @expectedException \InvalidArgumentException + * * @dataProvider providerFactoryException */ - public function testFactoryException($input) + public function testFactoryException(mixed $input): void { + $this->expectException(\InvalidArgumentException::class); IP\Address::factory($input); } - public function providerCompare() + public function providerCompare(): array { return array( array('127.0.0.1', '127.0.0.1', 0), @@ -83,11 +84,11 @@ public function providerCompare() /** * @dataProvider providerCompare */ - public function testCompare($a, $b, $expected) + public function testCompare(string $a, string $b, int $expected): void { $result = IP\Address::compare(IP\Address::factory($a), IP\Address::factory($b)); // Division is to ensure things are either -1, 0 or 1. abs() is to preseve sign. - $this->assertEquals($expected, $result == 0 ? 0: $result / abs($result)); + $this->assertEquals($expected, $result === 0 ? 0: $result / abs($result)); } } diff --git a/tests/IPNetworkAddressTest.php b/tests/IPNetworkAddressTest.php index 806b7af..c8b0f21 100644 --- a/tests/IPNetworkAddressTest.php +++ b/tests/IPNetworkAddressTest.php @@ -1,52 +1,54 @@ check_IP_version($other->address); + $this->check_IP_version($other->address); } } class IPv6_NetworkAddress_Tester extends IPv6\NetworkAddress { - public static function factory($address, $cidr = NULL) + public static function factory(NetworkAddress|Address|string $address, int|string|null $cidr = NULL): IPv6_NetworkAddress_Tester { $ip = IPv6\Address::factory($address); return new IPv6_NetworkAddress_Tester($ip, $cidr); } - public function test_check_IP_version($other) + public function test_check_IP_version(NetworkAddress $other): void { - return $this->check_IP_version($other->address); + $this->check_IP_version($other->address); } } @@ -58,7 +60,7 @@ public function test_check_IP_version($other) */ class IP_NetworkAddress_Test extends TestCase { - public function providerFactory() + public function providerFactory(): array { return array( array('127.0.0.1/16', NULL, '127.0.0.1', 16, '127.0.0.0'), @@ -79,7 +81,7 @@ public function providerFactory() /** * @dataProvider providerFactory */ - public function testFactory($address, $cidr, $expected_address, $expected_cidr, $expected_subnet) + public function testFactory(string|IP\NetworkAddress $address, string|int|null $cidr, string $expected_address, int $expected_cidr, string $expected_subnet): void { $ip = IP\NetworkAddress::factory($address, $cidr); @@ -89,7 +91,7 @@ public function testFactory($address, $cidr, $expected_address, $expected_cidr, } - public function providerFactoryThrowsException() + public function providerFactoryThrowsException(): array { return array( array(new IP_Address_Tester(), 1), @@ -99,14 +101,15 @@ public function providerFactoryThrowsException() /** * @dataProvider providerFactoryThrowsException - * @expectedException \InvalidArgumentException + * */ - public function testFactoryThrowsException($address, $cidr) + public function testFactoryThrowsException(IP_Address_Tester $address, int $cidr): void { + $this->expectException(\InvalidArgumentException::class); IP\NetworkAddress::factory($address, $cidr); } - public function provideFactoryParseCIDR() + public function provideFactoryParseCIDR(): array { return array( array('127.0.0.1/16', 24, 24), @@ -121,30 +124,31 @@ public function provideFactoryParseCIDR() /** * @dataProvider provideFactoryParseCIDR */ - public function testParseCIDR($address, $cidr, $expected) + public function testParseCIDR(string $address, string|int|null $cidr, int $expected): void { $network = IP\NetworkAddress::factory($address, $cidr); $this->assertEquals($expected, $network->get_cidr()); } - public function providerUnimplementedException() + public function providerUnimplementedException(): array { return array( - array('IP_NetworkAddress_Tester', 'generate_subnet_mask'), + #array('IP_NetworkAddress_Tester', 'generate_subnet_mask'), array('IP_NetworkAddress_Tester', 'get_global_netmask'), ); } /** - * @expectedException \LogicException + * * @dataProvider providerUnimplementedException */ - public function testUnimplementedException($class, $method) + public function testUnimplementedException(string $class, string $method): void { + $this->expectException(\LogicException::class); $class::$method(NULL); } - public function providerCompare() + public function providerCompare(): array { $data = array( array('0.0.0.0/16', '0.0.0.0/16', 0), @@ -167,17 +171,18 @@ public function providerCompare() /** * @dataProvider providerCompare */ - public function testCompare($left, $right, $expected) + public function testCompare(IP\NetworkAddress $left, IP\NetworkAddress $right, int $expected): void { $cmp = IP\NetworkAddress::compare($left, $right); - if ($cmp != 0) + if ($cmp !== 0) { $cmp /= abs($cmp); + } $this->assertEquals($expected, $cmp); } - public function providerAddressInNetwork() + public function providerAddressInNetwork(): array { return array( array(IP\NetworkAddress::factory('192.168.1.1/24'), 0, NULL, '192.168.1.0'), @@ -209,13 +214,13 @@ public function providerAddressInNetwork() /** * @dataProvider providerAddressInNetwork */ - public function testAddressInNetwork($network, $index, $from_start, $expected) + public function testAddressInNetwork(IP\NetworkAddress $network, int|Math_BigInteger $index, ?bool $from_start, string $expected): void { $address = $network->get_address_in_network($index, $from_start); $this->assertEquals($expected, (string) $address); } - public function providerCheck_IP_version() + public function providerCheck_IP_version(): array { return array( array( @@ -227,9 +232,9 @@ public function providerCheck_IP_version() ); } - public function providerCheck_IP_version_fail() + public function providerCheck_IP_version_fail(): array { - list(list($a4, $b4, $a6, $b6)) = $this->providerCheck_IP_version(); + [[$a4, $b4, $a6, $b6]] = $this->providerCheck_IP_version(); return array( array($a4, $a6), array($a4, $b6), @@ -246,7 +251,7 @@ public function providerCheck_IP_version_fail() /** * @dataProvider providerCheck_IP_version_fail */ - public function test_check_IP_version_fail($left, $right) + public function test_check_IP_version_fail(IPv4_NetworkAddress_Tester|IPv6_NetworkAddress_Tester $left, IPv4_NetworkAddress_Tester|IPv6_NetworkAddress_Tester $right): void { try { @@ -255,10 +260,12 @@ public function test_check_IP_version_fail($left, $right) } catch (\InvalidArgumentException $e) { // We expect this + $this->assertTrue(true); } - catch (PHPUnit_Framework_AssertionFailedError $e) + catch (\PHPUnit\Framework\AssertionFailedError $e) { // We expect this + $this->assertTrue(true); } catch (Exception $e) { $this->fail('An unexpected exception was raised.' . $e->getMessage()); @@ -268,7 +275,7 @@ public function test_check_IP_version_fail($left, $right) /** * @dataProvider providerCheck_IP_version */ - public function test_check_IP_version($a4, $b4, $a6, $b6) + public function test_check_IP_version(IPv4_NetworkAddress_Tester $a4, IPv4_NetworkAddress_Tester $b4, IPv6_NetworkAddress_Tester $a6, IPv6_NetworkAddress_Tester $b6): void { try { @@ -281,9 +288,10 @@ public function test_check_IP_version($a4, $b4, $a6, $b6) catch (Exception $e) { $this->fail('An unexpected exception was raised.' . $e->getMessage()); } + $this->assertTrue(true); } - public function providerSubnets() + public function providerSubnets(): array { $data = array( array('2000::/3','2001:630:d0:f104::80a/128', true, true), @@ -309,13 +317,13 @@ public function providerSubnets() /** * @dataProvider providerSubnets */ - public function testSubnets($sub1, $sub2, $shares, $encloses) + public function testSubnets(IP\NetworkAddress $sub1, IP\NetworkAddress $sub2, bool $shares, bool $encloses): void { $this->assertEquals($shares, $sub1->shares_subnet_space($sub2)); $this->assertEquals($encloses, $sub1->encloses_subnet($sub2)); } - public function providerEnclosesAddress() + public function providerEnclosesAddress(): array { $data = array( array('2000::/3','2001:630:d0:f104::80a', true), @@ -341,12 +349,12 @@ public function providerEnclosesAddress() /** * @dataProvider providerEnclosesAddress */ - public function testEnclosesAddress($subnet, $address, $expected) + public function testEnclosesAddress(IP\NetworkAddress $subnet, IP\Address $address, bool $expected): void { $this->assertEquals($expected, $subnet->encloses_address($address)); } - public function provideNetworkIdentifiers() + public function provideNetworkIdentifiers(): array { $data = array( array('2000::/3', true), @@ -366,13 +374,13 @@ public function provideNetworkIdentifiers() /** * @dataProvider provideNetworkIdentifiers */ - public function testNetworkIdentifiers($subnet, $expected) + public function testNetworkIdentifiers(IP\NetworkAddress $subnet, bool $expected): void { $this->assertEquals($expected, $subnet->is_network_identifier()); $this->assertTrue($subnet->get_network_identifier()->is_network_identifier()); } - public function test__toString() + public function test__toString(): void { $ip = '192.128.1.1/24'; $this->assertEquals($ip, (string) IP\NetworkAddress::factory($ip)); @@ -381,7 +389,7 @@ public function test__toString() $this->assertEquals($ip, (string) IP\NetworkAddress::factory($ip)); } - public function providerExcluding() + public function providerExcluding(): array { $data = array( array('192.168.0.0/24', @@ -414,7 +422,7 @@ public function providerExcluding() foreach ($data as &$d) { $d[0] = IP\NetworkAddress::factory($d[0]); - for ($i=1; $i < count($d); $i++) + for ($i=1, $iMax = count($d); $i < $iMax; $i++) { foreach ($d[$i] as &$e) { @@ -428,12 +436,12 @@ public function providerExcluding() /** * @dataProvider providerExcluding */ - public function testExcluding($block, $excluded, $expected) + public function testExcluding(IP\NetworkAddress $block, array $excluded, array $expected): void { $this->assertEquals($expected, $block->excluding($excluded)); } - public function provideMerge() + public function provideMerge(): array { $data = array( // Simple merge @@ -515,7 +523,7 @@ public function provideMerge() /** * @dataProvider provideMerge */ - public function testMerge($net_addrs, $expected) + public function testMerge(array $net_addrs, array $expected): void { $this->assertEquals($expected, IP\NetworkAddress::merge($net_addrs)); } diff --git a/tests/IPv4AddressTest.php b/tests/IPv4AddressTest.php index a622f42..bce4c88 100644 --- a/tests/IPv4AddressTest.php +++ b/tests/IPv4AddressTest.php @@ -1,13 +1,14 @@ assertNotNull($instance); $this->assertEquals($expected, (string) $instance); } - public function providerFactoryException() + public function providerFactoryException(): array { return array( array('256.0.0.1'), array('127.-1.0.1'), array('127.128.256.1'), array(new \Math_BigInteger('99999999999999999')), - array(123.45), - array(-123.45), + #array(123.45), throws TypeError + #array(-123.45), throws TypeError array('cake'), array('12345'), array('-12345'), @@ -64,16 +65,16 @@ public function providerFactoryException() ); } - public function testFormatInteger() + public function testFormatInteger(): void { $ip = IPv4\Address::factory('127.0.0.1'); $this->assertEquals(2130706433, $ip->format(IPv4\Address::FORMAT_INTEGER)); } - public function providerFormatException() + public function providerFormatException(): array { $bad_mode = -1; - $data = static::providerFactory(); + $data = $this->providerFactory(); foreach ($data as $i => $entry) { $data[$i] = array($entry[0], $bad_mode); } @@ -82,25 +83,27 @@ public function providerFormatException() } /** - * @expectedException InvalidArgumentException + * * @dataProvider providerFormatException */ - public function testFormatException($input, $mode) + public function testFormatException(mixed $input, mixed $mode): void { + $this->expectException(InvalidArgumentException::class); $instance = IPv4\Address::factory($input); echo $instance->format($mode); } /** - * @expectedException InvalidArgumentException + * * @dataProvider providerFactoryException */ - public function testFactoryException($input) + public function testFactoryException(mixed $input): void { + $this->expectException(InvalidArgumentException::class); IPv4\Address::factory($input); } - public function providerBitwise() + public function providerBitwise(): array { return array( // OP1 OP2 AND OR XOR NOT @@ -114,7 +117,7 @@ public function providerBitwise() /** * @dataProvider providerBitwise */ - public function testBitwise($ip1, $ip2, $ex_and, $ex_or, $ex_xor, $ex_not) + public function testBitwise(string $ip1, string $ip2, string $ex_and, string $ex_or, string $ex_xor, string $ex_not): void { $ip1 = IPv4\Address::factory($ip1); $ip2 = IPv4\Address::factory($ip2); @@ -143,7 +146,7 @@ public function testBitwise($ip1, $ip2, $ex_and, $ex_or, $ex_xor, $ex_not) // $this->assertEquals($v6, (string) $ip->asIPv6Address()); // } - public function providerAddSubtract() + public function providerAddSubtract(): array { $data = array( array('0.0.0.0' , 0, '0.0.0.0'), @@ -163,7 +166,7 @@ public function providerAddSubtract() array('192.168.0.0', 4, '192.168.0.4'), ); - for ($i=0; $i < count($data); $i++) { + for ($i=0, $iMax = count($data); $i < $iMax; $i++) { $data[$i][0] = IPv4\Address::factory($data[$i][0]); $data[$i][2] = IPv4\Address::factory($data[$i][2]); } @@ -174,7 +177,7 @@ public function providerAddSubtract() /** * @dataProvider providerAddSubtract */ - public function testAddSubtract($left, $right, $expected) + public function testAddSubtract(IPv4\Address $left, int $right, IPv4\Address $expected): void { $result = $left->add($right); $this->assertEquals(0, $result->compare_to($expected)); @@ -182,7 +185,7 @@ public function testAddSubtract($left, $right, $expected) $this->assertEquals(0, $result->compare_to($left)); } - public function providerAsIPv6Address() + public function providerAsIPv6Address(): array { $data = array( array('0.0.0.0' , '::ffff:0:0' ), @@ -205,7 +208,7 @@ public function providerAsIPv6Address() /** * @dataProvider providerAsIPv6Address */ - public function testAsIPv6Address($input, $expected_equal) + public function testAsIPv6Address(IPv4\Address $input, IPv6\Address $expected_equal): void { $converted = $input->as_IPv6_address(); @@ -213,7 +216,7 @@ public function testAsIPv6Address($input, $expected_equal) $this->assertEquals(0, $converted->compare_to($expected_equal)); } - public function testGetOctet() + public function testGetOctet(): void { $ip = IPv4\Address::factory('10.250.30.40'); $this->assertEquals(10, $ip->get_octet(-4)); @@ -228,7 +231,7 @@ public function testGetOctet() $this->assertNull($ip->get_octet(4)); } - public function testArrayAccess() + public function testArrayAccess(): void { $ip = IPv4\Address::factory('10.250.30.40'); $this->assertEquals(10, $ip[-4]); @@ -239,20 +242,16 @@ public function testArrayAccess() $this->assertFalse(isset($ip[4])); } - /** - * @expectedException \LogicException - */ - public function testArrayAccessSet() + public function testArrayAccessSet(): void { + $this->expectException(\LogicException::class); $ip = IPv4\Address::factory('10.250.30.40'); $ip[0] = 0; } - /** - * @expectedException \LogicException - */ - public function testArrayAccessUnset() + public function testArrayAccessUnset(): void { + $this->expectException(\LogicException::class); $ip = IPv4\Address::factory('10.250.30.40'); unset($ip[0]); } diff --git a/tests/IPv4NetworkAddressTest.php b/tests/IPv4NetworkAddressTest.php index adc660d..c004f66 100644 --- a/tests/IPv4NetworkAddressTest.php +++ b/tests/IPv4NetworkAddressTest.php @@ -11,7 +11,7 @@ class IPv4_NetworkAddress_Test extends TestCase { - public function providerSubnet() + public function providerSubnet(): array { $data = array( array(32, '255.255.255.255', 1, '1/256 C'), @@ -50,7 +50,7 @@ public function providerSubnet() ); // Collapse redundant 0s - for ($i=0; $i < count($data); $i++) { + for ($i=0, $iMax = count($data); $i < $iMax; $i++) { $data[$i][1] = str_replace('00','0', $data[$i][1]); $data[$i][1] = str_replace('00','0', $data[$i][1]); } @@ -61,8 +61,9 @@ public function providerSubnet() /** * @dataProvider providerSubnet */ - public function testSubnets($cidr, $subnet, $address_count, $network_class) + public function testSubnets(int $cidr, string $subnet, int $address_count, string $network_class): void { + /** @var IPv4\NetworkAddress $net */ $net = IPv4\NetworkAddress::factory('0.0.0.0', $cidr); $this->assertEquals($subnet, (string) $net->get_subnet_mask()); @@ -70,12 +71,12 @@ public function testSubnets($cidr, $subnet, $address_count, $network_class) $this->assertEquals($network_class, $net->get_network_class()); } - public function testGlobalNetmask() + public function testGlobalNetmask(): void { $this->assertEquals('255.255.255.255', (string) IPv4\NetworkAddress::get_global_netmask()); } - public function testDodgyBitwiseStuff() + public function testDodgyBitwiseStuff(): void { $block = IPv4\NetworkAddress::factory('10.13.112.20/30'); $address = IPv4\Address::factory('10.13.112.21'); @@ -83,7 +84,7 @@ public function testDodgyBitwiseStuff() $this->assertTrue($block->encloses_address($address)); } - public function providerNetworkBroadcastAddress() + public function providerNetworkBroadcastAddress(): array { return array( array(IPv4\NetworkAddress::factory('192.168.1.1/24'), '192.168.1.0', '192.168.1.255'), @@ -94,13 +95,13 @@ public function providerNetworkBroadcastAddress() /** * @dataProvider providerNetworkBroadcastAddress */ - public function testNetworkBroadcastAddress($ip, $ex_network, $ex_broadcast) + public function testNetworkBroadcastAddress(IPv4\NetworkAddress $ip, string $ex_network, string $ex_broadcast): void { $this->assertEquals($ex_network, (string) $ip->get_NetworkAddress()); $this->assertEquals($ex_broadcast, (string) $ip->get_broadcast_address()); } - public function providerSplit() + public function providerSplit(): array { $data = array( array('192.168.0.0/24', 0, array('192.168.0.0/24')), @@ -121,21 +122,19 @@ public function providerSplit() /** * @dataProvider providerSplit */ - public function testSplit($block, $degree, $expected) + public function testSplit(IPv4\NetworkAddress $block, int $degree, array $expected): void { $this->assertEquals($expected, $block->split($degree)); } - /** - * @expectedException InvalidArgumentException - */ - public function testSplitBeyondRange() + public function testSplitBeyondRange(): void { + $this->expectException(InvalidArgumentException::class); $block = IPv4\NetworkAddress::factory('192.168.0.0/32'); $block->split(); } - public function testIteratorInterface() + public function testIteratorInterface(): void { $block = IPv4\NetworkAddress::factory('192.168.0.0/30'); $expected = array('192.168.0.0', '192.168.0.1', '192.168.0.2', '192.168.0.3'); @@ -148,7 +147,7 @@ public function testIteratorInterface() $this->assertEquals($expected, array_map('strval', $block->toArray())); } - public function testTwoIterators() + public function testTwoIterators(): void { $block = IPv4\NetworkAddress::factory('192.168.0.0/31'); $expected = array('192.168.0.0', '192.168.0.0', '192.168.0.1', '192.168.0.1', '192.168.0.0', '192.168.0.1'); @@ -164,13 +163,13 @@ public function testTwoIterators() $this->assertEquals($expected, $actual); } - public function testCountableInterface() + public function testCountableInterface(): void { $block = IPv4\NetworkAddress::factory('192.168.0.0/30'); $this->assertCount(4, $block); $block = IPv4\NetworkAddress::factory('192.168.0.0/24'); - $this->assertEquals(pow(2, 8), count($block)); + $this->assertCount(2 ** 8, $block); $block = IPv4\NetworkAddress::factory('192.168.0.0/16'); - $this->assertEquals(pow(2, 16), count($block)); + $this->assertCount(2 ** 16, $block); } } diff --git a/tests/IPv6AddressTest.php b/tests/IPv6AddressTest.php index a1dc716..b1f28ce 100644 --- a/tests/IPv6AddressTest.php +++ b/tests/IPv6AddressTest.php @@ -4,12 +4,12 @@ class TestingIPv6_Address extends IPv6\Address { - public static function factory($address) + public static function factory(IP\Address|int|string|\Math_BigInteger $address): TestingIPv6_Address { return new TestingIPv6_Address($address); } - public function call_bitwise_operation($flag, IP\Address $other = NULL) + public function call_bitwise_operation(string $flag, IP\Address $other = NULL): void { $this->bitwise_operation($flag, $other); } @@ -24,7 +24,7 @@ public function call_bitwise_operation($flag, IP\Address $other = NULL) class IPv6_Address_Test extends TestCase { - public function providerFactory() + public function providerFactory(): array { return array( array( @@ -53,7 +53,7 @@ public function providerFactory() /** * @dataProvider providerFactory */ - public function testFactory($input, $compact, $abbr, $full) + public function testFactory(string|int $input,string $compact,string $abbr,string $full): void { $instance = IPv6\Address::factory($input); @@ -63,7 +63,7 @@ public function testFactory($input, $compact, $abbr, $full) $this->assertEquals($full, $instance->format(IP\Address::FORMAT_FULL)); } - public function providerFormatException() + public function providerFormatException(): array { $bad_mode = -1; $data = static::providerFactory(); @@ -75,16 +75,17 @@ public function providerFormatException() } /** - * @expectedException InvalidArgumentException + * * @dataProvider providerFormatException */ - public function testFormatException($input, $mode) + public function testFormatException(string|int $input, int $mode): void { + $this->expectException(InvalidArgumentException::class); $instance = IPv6\Address::factory($input); echo $instance->format($mode); } - public function providerFactoryException() + public function providerFactoryException(): array { return array( array('256.0.0.1'), @@ -95,20 +96,20 @@ public function providerFactoryException() array('-12345'), array('0000:0000:0000:ffff:0127:0000:0000:000g'), array('000000000000ffff0127000000000001'), - array(array()), ); } /** - * @expectedException InvalidArgumentException + * * @dataProvider providerFactoryException */ - public function testFactoryException($input) + public function testFactoryException(string $input): void { + $this->expectException(InvalidArgumentException::class); IPv6\Address::factory($input); } - public function providerAddSubtract() + public function providerAddSubtract(): array { $data = array( array('::' , 0, '::' ), @@ -121,7 +122,7 @@ public function providerAddSubtract() array('::10', new \Math_BigInteger(2), '::12' ), ); - for ($i=0; $i < count($data); $i++) + for ($i=0, $iMax = count($data); $i < $iMax; $i++) { $data[$i][0] = IPv6\Address::factory($data[$i][0]); $data[$i][2] = IPv6\Address::factory($data[$i][2]); @@ -132,7 +133,7 @@ public function providerAddSubtract() /** * @dataProvider providerAddSubtract */ - public function testAddSubtract($left, $right, $expected) + public function testAddSubtract(IPv6\Address $left, int|\Math_BigInteger $right, IPv6\Address $expected): void { $result = $left->add($right); $this->assertEquals(0, $result->compare_to($expected)); @@ -140,7 +141,7 @@ public function testAddSubtract($left, $right, $expected) $this->assertEquals(0, $again->compare_to($left)); } - public function providerCompareTo() + public function providerCompareTo(): array { $data = array( array('::', '::', 0), @@ -151,7 +152,7 @@ public function providerCompareTo() array('::a', '::b', -1), ); - for ($i=0; $i < count($data); $i++){ + for ($i=0, $iMax = count($data); $i < $iMax; $i++){ $data[$i][0] = IPv6\Address::factory($data[$i][0]); $data[$i][1] = IPv6\Address::factory($data[$i][1]); } @@ -161,12 +162,12 @@ public function providerCompareTo() /** * @dataProvider providerCompareTo */ - public function testCompareTo($left, $right, $expected) + public function testCompareTo(IPv6\Address $left, IPv6\Address $right, int $expected): void { $this->assertEquals($expected, $left->compare_to($right)); } - public function providerBitwise() + public function providerBitwise(): array { $data = array( // OP1 OP2 AND OR XOR NOT @@ -176,7 +177,7 @@ public function providerBitwise() array('::' , '::' , '::0', '::0', '::0', 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'), ); - for ($i=0; $i < count($data); $i++) { + for ($i=0, $iMax = count($data); $i < $iMax; $i++) { for ($j=0; $j < 6; $j++) { $data[$i][$j] = IPv6\Address::factory($data[$i][$j]); } @@ -188,7 +189,7 @@ public function providerBitwise() /** * @dataProvider providerBitwise */ - public function testBitwise($ip1, $ip2, $ex_and, $ex_or, $ex_xor, $ex_not) + public function testBitwise(IPv6\Address $ip1, IPv6\Address $ip2, IPv6\Address $ex_and, IPv6\Address $ex_or, IPv6\Address $ex_xor, IPv6\Address $ex_not): void { $this->assertEquals((string) $ex_and, (string) $ip1->bitwise_and($ip2)); $this->assertEquals((string) $ex_or , (string) $ip1->bitwise_or($ip2)); @@ -196,7 +197,7 @@ public function testBitwise($ip1, $ip2, $ex_and, $ex_or, $ex_xor, $ex_not) $this->assertEquals((string) $ex_not, (string) $ip1->bitwise_not()); } - public function testBitwiseException() + public function testBitwiseException(): void { $ip = TestingIPv6_Address::factory('::1'); @@ -241,7 +242,7 @@ public function testBitwiseException() // // } - public function testGetOctet() + public function testGetOctet(): void { $ip = IPv6\Address::factory('0001:0002:aaaa:1234:abcd:1000:2020:fffe'); $this->assertEquals(0, $ip->get_octet(-16)); @@ -260,15 +261,17 @@ public function testGetOctet() $this->assertNull($ip->get_octet(16)); } - public function testMappedIPv4() + public function testMappedIPv4(): void { + /** @var IPv6\Address $ip */ $ip = IP\Address::factory('::ffff:141.44.23.50'); $this->assertEquals(1, $ip->is_encoded_IPv4_address()); $ipv4 = $ip->as_IPv4_address(); - $this->assertEquals($ipv4->format(IP\Address::FORMAT_COMPACT), '141.44.23.50'); + $this->assertEquals( '141.44.23.50',$ipv4->format(IP\Address::FORMAT_COMPACT)); } - public function testMayMappedIPv4Format() { + public function testMayMappedIPv4Format(): void + { $mappedIPv4String = '::ffff:141.44.23.50'; $ordinaryIPv6String = '1:2:aaaa:1234:abcd:1000:2020:fffe'; $mappedIPv4Address = IP\Address::factory($mappedIPv4String); @@ -277,7 +280,7 @@ public function testMayMappedIPv4Format() { $this->assertEquals($ordinaryIPv6Address->format(IPv6\Address::FORMAT_MAY_MAPPED_COMPACT), $ordinaryIPv6String); } - public function testArrayAccess() + public function testArrayAccess(): void { $ip = IPv6\Address::factory('0001:0002:aaaa:1234:abcd:1000:2020:fffe'); $this->assertEquals(0x12, $ip[-10]); @@ -289,7 +292,7 @@ public function testArrayAccess() /** * @return array */ - public function providerPadIps() + public function providerPadIps(): array { return array( array('::', '0000:0000:0000:0000:0000:0000:0000:0000'), @@ -315,7 +318,7 @@ public function providerPadIps() * @param string $actual * @param string $expected */ - public function testPad($actual, $expected) + public function testPad(string $actual, string $expected): void { $this->assertEquals($expected, IPv6\Address::pad($actual)); } diff --git a/tests/IPv6NetworkAddressTest.php b/tests/IPv6NetworkAddressTest.php index b242e93..f10d36c 100644 --- a/tests/IPv6NetworkAddressTest.php +++ b/tests/IPv6NetworkAddressTest.php @@ -10,12 +10,12 @@ */ class IPv6_NetworkAddress_Test extends TestCase { - public function test_global_netmask() + public function test_global_netmask(): void { $this->assertEquals('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', (string) IPv6\NetworkAddress::get_global_netmask()); } - public function providerSplit() + public function providerSplit(): array { $data = array( array('::0/126', 0, array('::0/126')), @@ -36,21 +36,19 @@ public function providerSplit() /** * @dataProvider providerSplit */ - public function testSplit($block, $degree, $expected) + public function testSplit(IPv6\NetworkAddress $block, int $degree, array $expected): void { $this->assertEquals($expected, $block->split($degree)); } - /** - * @expectedException InvalidArgumentException - */ - public function testSplitBeyondRange() + public function testSplitBeyondRange(): void { + $this->expectException(InvalidArgumentException::class); $block = IPv6\NetworkAddress::factory('::0/128'); $block->split(); } - public function testIterationInterface() + public function testIterationInterface(): void { $block = IPv6\NetworkAddress::factory('::0/126'); $expected = array('::0', '::1', '::2', '::3'); @@ -62,11 +60,11 @@ public function testIterationInterface() $this->assertEquals($expected, $actual); } - public function testCountableInterface() + public function testCountableInterface(): void { $block = IPv6\NetworkAddress::factory('::0/126'); - $this->assertEquals(4, count($block)); + $this->assertCount(4, $block); $block = IPv6\NetworkAddress::factory('::0/120'); - $this->assertEquals(pow(2, 8), count($block)); + $this->assertCount(2 ** 8, $block); } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 8b3b83e..125c3c7 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -5,7 +5,7 @@ require __DIR__.'/../vendor/autoload.php'; // @todo This shouldn't be needed. -require 'vendor/pear/math_biginteger/Math/BigInteger.php'; +require __DIR__.'/../vendor/pear/math_biginteger/Math/BigInteger.php'; $bigint_mode = getenv('MATH_BIGINTEGER_MODE'); if ($bigint_mode !== FALSE)