Skip to content

Commit f1f2dd5

Browse files
authored
Merge pull request #3 from mesour/php8
Support for PHP 8
2 parents acd6dd1 + c7a18dd commit f1f2dd5

File tree

9 files changed

+147
-31
lines changed

9 files changed

+147
-31
lines changed

.travis.yml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
language: php
22

33
php:
4-
- 7.1
5-
- 7.2
6-
- 7.3
7-
- 7.4
84
- 8.0
9-
10-
matrix:
11-
allow_failures:
12-
- php: 8.0
5+
- 8.1
136

147
install:
158
- composer install --no-interaction --prefer-source
169

1710
script:
1811
- vendor/bin/parallel-lint src tests
19-
- vendor/bin/phpcs --standard=ruleset.xml --extensions=php,phpt --encoding=utf-8 --tab-width=4 -sp src tests
2012
- vendor/bin/phpstan analyse -l 7 -c phpstan.neon src tests
2113
- vendor/bin/tester -p php tests/ -s -c tests/php.ini

README.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,31 @@ IpAddressNormalizer::compressIpV6('2001:0db8:0800:0000:0000:ff00:0042:8329');
3838
// result is: 2001:db8:800::ff00:42:8329
3939
```
4040

41-
# Tests
41+
### Usage: `Mesour\IpAddresses\IpAddress`
4242

43-
- Run tests: `bin/check-tests`
44-
- PHP Stan: `bin/check-stan`
45-
- Code style: `bin/check-cs`
43+
Normalize IPv4:
44+
```php
45+
$ipAddress = IpAddress::create('127.0.0.1');
46+
47+
$ipAddress->isIpV4(); // result is: true
48+
$ipAddress->isIpV6(); // result is: false
49+
$ipAddress->getValue(); // result is: 127.0.0.1
50+
```
51+
52+
Normalize IPv6:
53+
54+
- Result of `getValue` is compressed IPv6 value
55+
56+
```php
57+
$ipAddress = IpAddress::create('2001:0db8:0800:0000:0000:ff00:0042:8329');
58+
59+
$ipAddress->isIpV4(); // result is: false
60+
$ipAddress->isIpV6(); // result is: true
61+
$ipAddress->getValue(); // result is: 2001:db8:800::ff00:42:8329
62+
```
63+
64+
# Development
65+
66+
- Syntax check: `vendor/bin/parallel-lint src tests`
67+
- PHP Stan: `vendor/bin/phpstan analyse -l 7 -c phpstan.neon src tests`
68+
- Run tests: `vendor/bin/tester -p php tests/ -s -c tests/php.ini`

composer.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@
88
"homepage": "http://mesour.com"
99
}],
1010
"require": {
11-
"php": ">=7.1"
11+
"php": ">=8.0"
1212
},
1313
"require-dev": {
1414
"tracy/tracy": "~2.3.0",
15-
"slevomat/coding-standard": "^6.4",
16-
"consistence/coding-standard": "^3.10",
17-
"phpstan/phpstan": "^0.12.42",
15+
"phpstan/phpstan": "^1.8.5",
1816
"nette/robot-loader": "^2.4",
1917
"nette/tester": "^2.3",
20-
"jakub-onderka/php-parallel-lint": "^1.0"
18+
"php-parallel-lint/php-parallel-lint": "^1.3"
2119
},
2220
"autoload": {
2321
"classmap": ["src/"]

phpstan.neon

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
parameters:
2-
autoload_directories:
3-
- %rootDir%/../../../src
4-
- %rootDir%/../../../tests
52

6-
excludes_analyse:
3+
excludePaths:
74
- %rootDir%/../../../tests/bootstrap.php
85
- %rootDir%/../../../tests/environment.php
96

107
ignoreErrors:
11-
- '#Parameter \#1 \$var of function count expects array\|Countable, array<int, string>\|string given\.#'
12-
- '#Parameter \#2 \$str of function explode expects string, array<int, string>\|string given\.#'
13-
- '#Parameter \#1 \$arr1 of function array_merge expects array, array<int, string>\|string given\.#'
14-
- '#Parameter \#2 \.\.\.\$args of function array_merge expects array, array<int, string>\|string given\.#'
8+
- '*Parameter #2 $string of function explode expects string, array<int, string>\|string given.*'
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Mesour\IpAddresses;
6+
7+
/** @author Matouš Němec <mesour.com> */
8+
final class IpAddress
9+
{
10+
11+
private string $ipAddress;
12+
13+
private bool $ipV4;
14+
15+
private function __construct(string $ipAddress, bool $ipV4)
16+
{
17+
$this->ipAddress = $ipAddress;
18+
$this->ipV4 = $ipV4;
19+
}
20+
21+
public static function create(string $ipAddress): self
22+
{
23+
if (IpAddressValidator::isIpV4($ipAddress)) {
24+
return new self($ipAddress, true);
25+
} elseif (IpAddressValidator::isIpV6($ipAddress)) {
26+
return new self(IpAddressNormalizer::compressIpV6($ipAddress), false);
27+
} else {
28+
throw new IpAddressIsInvalidException();
29+
}
30+
}
31+
32+
public function isIpV4(): bool
33+
{
34+
return $this->ipV4;
35+
}
36+
37+
public function isIpV6(): bool
38+
{
39+
return !$this->isIpV4();
40+
}
41+
42+
public function getValue(): string
43+
{
44+
return $this->ipAddress;
45+
}
46+
47+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Mesour\IpAddresses;
6+
7+
/** @author Matouš Němec <mesour.com> */
8+
final class IpAddressIsInvalidException extends \RuntimeException
9+
{
10+
11+
}

src/Mesour/IpAddresses/IpAddressNormalizer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ abstract class IpAddressNormalizer
1010

1111
final public static function normalizeIpV6(string $address): string
1212
{
13-
if (\strpos($address, '::') !== false) {
13+
if (\str_contains($address, '::')) {
1414
$part = \explode('::', $address);
1515
$part[0] = \explode(':', $part[0]);
1616
$part[1] = \explode(':', $part[1]);
1717
$missing = [];
1818

1919
for ($i = 0; $i < 8 - (\count($part[0]) + \count($part[1])); $i++) {
20-
\array_push($missing, '0000');
20+
$missing[] = '0000';
2121
}
2222

2323
$missing = \array_merge($part[0], $missing);
@@ -45,15 +45,15 @@ final public static function normalizeIpV6(string $address): string
4545

4646
final public static function compressIpV6(string $ip): string
4747
{
48-
if (\substr($ip, 0, 4) === '0000') {
48+
if (\str_starts_with($ip, '0000')) {
4949
$ip = \substr_replace($ip, ':0', 0, 4);
5050
}
5151

5252
$ip = \str_replace(':0000', ':0', $ip);
5353
$ip = \preg_replace('/:0{1,3}(?=\w)/', ':', $ip);
5454
$z = ':0:0:0:0:0:0:0:';
5555

56-
while (\strpos($ip, '::') === false && \strlen($z) >= 5) {
56+
while (!\str_contains($ip, '::') && \strlen($z) >= 5) {
5757
$pos = \strpos($ip, $z);
5858

5959
if ($pos !== false) {

tests/Mesour/IpAddressesTests/IpAddressNormalizerTest.phpt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use Tester\Assert;
1010
require_once __DIR__ . '/../../bootstrap.php';
1111
require_once __DIR__ . '/BaseTestCase.php';
1212

13-
class IpAddressNormalizerTest extends BaseTestCase
13+
/**
14+
* @testCase
15+
*/
16+
final class IpAddressNormalizerTest extends BaseTestCase
1417
{
1518

1619
public function testCompress(): void
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Mesour\IpAddressesTests;
6+
7+
use Mesour\IpAddresses\IpAddress;
8+
use Mesour\IpAddresses\IpAddressIsInvalidException;
9+
use Tester\Assert;
10+
11+
require_once __DIR__ . '/../../bootstrap.php';
12+
require_once __DIR__ . '/BaseTestCase.php';
13+
14+
/**
15+
* @testCase
16+
*/
17+
final class IpAddressTest extends BaseTestCase
18+
{
19+
20+
public function testIpCheckAndValue(): void
21+
{
22+
$ipAddress = IpAddress::create('0000:0db8:0800:0000:0000:ff00:0042:8329');
23+
Assert::false($ipAddress->isIpV4());
24+
Assert::true($ipAddress->isIpV6());
25+
Assert::same('0:db8:800::ff00:42:8329', $ipAddress->getValue());
26+
27+
$ipAddress = IpAddress::create('0:db8:800::ff00:42:8329');
28+
Assert::false($ipAddress->isIpV4());
29+
Assert::true($ipAddress->isIpV6());
30+
Assert::same('0:db8:800::ff00:42:8329', $ipAddress->getValue());
31+
32+
$ipAddress = IpAddress::create('127.0.0.1');
33+
Assert::true($ipAddress->isIpV4());
34+
Assert::false($ipAddress->isIpV6());
35+
Assert::same('127.0.0.1', $ipAddress->getValue());
36+
}
37+
38+
public function testInvalidIp(): void
39+
{
40+
Assert::exception(function (): void {
41+
IpAddress::create('123');
42+
}, IpAddressIsInvalidException::class);
43+
}
44+
45+
}
46+
47+
$test = new IpAddressTest();
48+
$test->run();

0 commit comments

Comments
 (0)