Skip to content

Commit

Permalink
Merge pull request #36 from leth/2.0-dev
Browse files Browse the repository at this point in the history
Release 2.0
  • Loading branch information
tabacco authored Aug 11, 2023
2 parents 646bb8e + 2921ba2 commit 1c5acc5
Show file tree
Hide file tree
Showing 20 changed files with 475 additions and 391 deletions.
23 changes: 16 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor/
/.idea/
composer.phar
.phpunit.result.cache
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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).

80 changes: 43 additions & 37 deletions classes/Leth/IPAddress/IP/Address.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/*
* This file is part of the PHP-IPAddress library.
*
Expand All @@ -18,6 +19,7 @@
*/
namespace Leth\IPAddress\IP;
use \Leth\IPAddress\IP, \Leth\IPAddress\IPv4, \Leth\IPAddress\IPv6;
use ReturnTypeWillChange;

/**
* An abstract representation of an IP Address.
Expand All @@ -26,33 +28,34 @@
*/
abstract class Address implements \ArrayAccess
{
const IP_VERSION = -1;
const FORMAT_FULL = 0;
const FORMAT_COMPACT = 1;
public const IP_VERSION = -1;
public const FORMAT_FULL = 0;
public const FORMAT_COMPACT = 1;

/**
* Internal representation of the address. Format may vary.
* @var mixed
* @var numeric
*/
protected $address;

/**
* Create an IP address object from the supplied address.
*
* @param string $address The address to represent.
* @return IP\Address An instance of a subclass of IP\Address; either IPv4\Address or IPv6\Address
* @param IP\Address|int|string|\Math_BigInteger $address The address to represent.
*
* @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
*/
public static function factory($address)
public static function factory(IP\Address|int|string|\Math_BigInteger $address): IP\Address|Ipv4\Address|IPv6\Address
{
if ($address instanceof IP\Address)
if ($address instanceof self)
{
return $address;
}
elseif (is_int($address) OR (is_string($address) AND filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)))
elseif (is_int($address) || (is_string($address) && filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)))
{
return IPv4\Address::factory($address);
}
elseif ($address instanceof \Math_BigInteger OR (is_string($address) AND filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)))
elseif ($address instanceof \Math_BigInteger || (is_string($address) && filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)))
{
return IPv6\Address::factory($address);
}
Expand All @@ -72,17 +75,17 @@ public static function factory($address)
* @param IP\Address $b The right hand side of the comparison.
* @return int The result of the comparison.
*/
public static function compare(IP\Address $a, IP\Address $b)
public static function compare(IP\Address $a, IP\Address $b): int
{
return $a->compare_to($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;
}
Expand All @@ -93,99 +96,99 @@ 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.
*
* @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.
*
* @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.
*
* @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.
*
* @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.
*
* @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);
}

/**
* Return the string representation of the address
*
* @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.
*
* @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;
}

/**
Expand All @@ -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);
}
Expand All @@ -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');
}
Expand All @@ -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');
}
Expand Down
Loading

0 comments on commit 1c5acc5

Please sign in to comment.