Skip to content

Commit

Permalink
Merge pull request #11 from lionser/release/2.0.0
Browse files Browse the repository at this point in the history
release/2.0.0
  • Loading branch information
lionser committed Sep 14, 2022
2 parents 71390b6 + f7171c5 commit 710c681
Show file tree
Hide file tree
Showing 25 changed files with 124 additions and 260 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/.idea
/.idea/
/.phpunit.result.cache
/build
/tests/coverage/
/tests/logs/
/vendor/
/composer.lock
/vendor
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
language: php
php:
- '7.1'
- '7.2'
- '7.3'
- '7.4'
- '8.1'

before_script:
- curl --version
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 dev-sl
Copyright (c) 2019 Lionser

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ coverage:
XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html build/coverage/

phpcs:
vendor/bin/phpcs --standard=PSR2 src/ tests/
vendor/bin/phpcs --standard=PSR12 src/ tests/
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
}
],
"require": {
"php": "^7.1"
"php": "^8.1"
},
"require-dev": {
"php-coveralls/php-coveralls": "^2.1",
"phpunit/phpunit": "^7.5",
"squizlabs/php_codesniffer": "^3.5"
"php-coveralls/php-coveralls": "^2.5",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.7"
},
"config": {
"sort-packages": true
Expand Down
37 changes: 23 additions & 14 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Library for create CIDR from IP rangle">
<directory>tests</directory>
</testsuite>
</testsuites>
<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/9.5/phpunit.xsd"
backupGlobals="false"
colors="true"
>
<testsuites>
<testsuite name="Library for create CIDR from IP range">
<directory>tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
<logging>
<junit outputFile="tests/logs/junit.xml" />
</logging>

<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
<report>
<clover outputFile="tests/logs/clover.xml" />
<html outputDirectory="tests/coverage" lowUpperBound="35" highLowerBound="70" />
</report>
</coverage>
</phpunit>
9 changes: 3 additions & 6 deletions src/Detector/NetmaskDetector.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Lionser\Detector;

Expand All @@ -7,11 +9,6 @@

class NetmaskDetector
{
/**
* @param IpInterface $ip
*
* @return IpInterface
*/
public function detect(IpInterface $ip): IpInterface
{
return new Netmask(long2ip(-(ip2long($ip->getAddress()) & -(ip2long($ip->getAddress())))));
Expand Down
10 changes: 6 additions & 4 deletions src/IP/Version.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Lionser\IP;

final class Version
enum Version: int
{
public const IP_V4 = 4;
public const IP_V6 = 6;
case IP_V4 = 4;
case IP_V6 = 6;
}
31 changes: 4 additions & 27 deletions src/Parser/CidrParser.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Lionser\Parser;

Expand All @@ -12,21 +14,11 @@ class CidrParser implements RangeParserInterface
{
private const MAX_PREFIX_BITS = 32;

/**
* @var NetmaskDetector
*/
private $netmaskDetector;

/**
* @param NetmaskDetector $netmaskDetector
*/
public function __construct(NetmaskDetector $netmaskDetector)
public function __construct(private readonly NetmaskDetector $netmaskDetector)
{
$this->netmaskDetector = $netmaskDetector;
}

/**
* {@inheritdoc}
* @return Cidr[]
*/
public function parseRange(RangeInterface $range): array
Expand All @@ -50,11 +42,6 @@ public function parseRange(RangeInterface $range): array
return $cidrs;
}

/**
* @param int $int
*
* @return int
*/
private function getBitsCount(int $int): int
{
$int = $int & 0xFFFFFFFF;
Expand All @@ -68,21 +55,11 @@ private function getBitsCount(int $int): int
return $int;
}

/**
* @param IpInterface $netmask
*
* @return int
*/
private function convertNetmaskToBits(IpInterface $netmask): int
{
return $this->getBitsCount($netmask->getProperAddress());
}

/**
* @param IpInterface $ip
*
* @return int
*/
private function getMaxBits(IpInterface $ip): int
{
$netmask = $this->netmaskDetector->detect($ip);
Expand Down
7 changes: 3 additions & 4 deletions src/Parser/CidrParserFacade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Lionser\Parser;

Expand All @@ -10,9 +12,6 @@
class CidrParserFacade
{
/**
* @param string $start
* @param string $end
*
* @return Cidr[]
*/
public static function parse(string $start, string $end): array
Expand Down
9 changes: 3 additions & 6 deletions src/Parser/RangeParserInterface.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Lionser\Parser;

use Lionser\ValueObject\IP\RangeInterface;

interface RangeParserInterface
{
/**
* @param RangeInterface $range
*
* @return array
*/
public function parseRange(RangeInterface $range): array;
}
33 changes: 5 additions & 28 deletions src/ValueObject/Cidr.php
Original file line number Diff line number Diff line change
@@ -1,50 +1,27 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Lionser\ValueObject;

use Lionser\ValueObject\IP\IpInterface;

class Cidr
class Cidr implements \Stringable
{
/**
* @var IpInterface
*/
private $ip;

/**
* @var int
*/
private $bits;

/**
* @param IpInterface $ip
* @param int $bits
*/
public function __construct(IpInterface $ip, int $bits)
public function __construct(private readonly IpInterface $ip, private readonly int $bits)
{
$this->ip = $ip;
$this->bits = $bits;
}

/**
* @return IpInterface
*/
public function getIp(): IpInterface
{
return $this->ip;
}

/**
* @return int
*/
public function getBits(): int
{
return $this->bits;
}

/**
* @return string
*/
public function __toString(): string
{
return "{$this->getIp()}/{$this->getBits()}";
Expand Down
37 changes: 9 additions & 28 deletions src/ValueObject/IP/AbstractIp.php
Original file line number Diff line number Diff line change
@@ -1,53 +1,34 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Lionser\ValueObject\IP;

abstract class AbstractIp implements IpInterface
use Lionser\IP\Version;

abstract class AbstractIp implements IpInterface, \Stringable
{
/**
* @var string
*/
protected $address;

/**
* @param string $address
*/
public function __construct(string $address)
public function __construct(protected readonly string $address)
{
$this->address = $address;
}

/**
* {@inheritdoc}
*/
public function getAddress(): string
{
return $this->address;
}

/**
* {@inheritdoc}
*/
public function getProperAddress(): int
{
return ip2long($this->getAddress());
}

/**
* @return int
*/
abstract public function getVersion(): int;
abstract public function getVersion(): Version;

/**
* @return bool
*/
public function isValid(): bool
{
return (bool) filter_var($this->getAddress(), FILTER_VALIDATE_IP);
}
/**
* @return string
*/

public function __toString(): string
{
return $this->getAddress();
Expand Down
10 changes: 3 additions & 7 deletions src/ValueObject/IP/IpInterface.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Lionser\ValueObject\IP;

interface IpInterface
{
/**
* @return string
*/
public function getAddress(): string;

/**
* @return int
*/
public function getProperAddress(): int;
}
9 changes: 4 additions & 5 deletions src/ValueObject/IP/IpV4.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Lionser\ValueObject\IP;

use Lionser\IP\Version;

class IpV4 extends AbstractIp
{
/**
* {@inheritdoc}
*/
public function getVersion(): int
public function getVersion(): Version
{
return Version::IP_V4;
}
Expand Down
Loading

0 comments on commit 710c681

Please sign in to comment.