Skip to content

Commit

Permalink
refactor: moves compiler infrastructure out of runtime dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
ju1ius committed Feb 22, 2024
1 parent 0c556ef commit 350a751
Show file tree
Hide file tree
Showing 26 changed files with 29 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/phpunit.xml export-ignore
/phpbench.json export-ignore

/compiler export-ignore
/tests export-ignore
/bench export-ignore

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php declare(strict_types=1);

namespace Souplette\FusBup\Exception;
namespace Souplette\FusBup\Compiler\Exception;

use Souplette\FusBup\Compiler\Parser\Rule;
use Souplette\FusBup\Exception\FusBupException;

/**
* An error happened while parsing the public suffix list.
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/Parser/Rule.php → compiler/Parser/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Souplette\FusBup\Compiler\Parser;

use Souplette\FusBup\Compiler\Exception\ParseError;
use Souplette\FusBup\Exception\IdnException;
use Souplette\FusBup\Exception\ParseError;
use Souplette\FusBup\Utils\Idn;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Souplette\FusBup\Compiler\Parser;

use Souplette\FusBup\Exception\ParseError;
use Souplette\FusBup\Compiler\Exception\ParseError;
use Traversable;

final class RuleList implements \IteratorAggregate
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/Compiler/PslParser.php → compiler/PslParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Souplette\FusBup\Compiler;

use Souplette\FusBup\Compiler\Exception\ParseError;
use Souplette\FusBup\Compiler\Parser\Rule;
use Souplette\FusBup\Compiler\Parser\RuleList;
use Souplette\FusBup\Compiler\Parser\RuleType;
use Souplette\FusBup\Compiler\Parser\Section;
use Souplette\FusBup\Exception\ParseError;
use SplFileObject;
use Traversable;

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@
},
"autoload-dev": {
"psr-4": {
"Souplette\\FusBup\\Compiler\\": "compiler/",
"Souplette\\FusBup\\Tests\\": "tests/",
"Souplette\\FusBup\\Benchmarks\\": "bench/"
}
},
"require": {
"php": ">=8.1",
"ext-ctype": "*",
"ext-intl": "*",
"ju1ius/luigi": "^1.0"
"ext-intl": "*"
},
"require-dev": {
"symfony/stopwatch": "^6.2"
"symfony/stopwatch": "^6.2",
"ju1ius/luigi": "^1.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Souplette\FusBup\Exception;

final class PrivateETLDException extends ForbiddenDomainException
final class PrivateEffectiveTLDException extends ForbiddenDomainException
{
public function __construct(string $domain)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Lookup/Dafsa.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Souplette\FusBup\Lookup;

use Souplette\FusBup\Exception\PrivateETLDException;
use Souplette\FusBup\Exception\PrivateEffectiveTLDException;
use Souplette\FusBup\Exception\UnknownTLDException;
use Souplette\FusBup\Lookup\Dafsa\IncrementalLookup;
use Souplette\FusBup\Lookup\Dafsa\Result;
Expand All @@ -25,7 +25,7 @@ public function isEffectiveTLD(string $domain, int $flags = self::FORBID_NONE):
$domain = Idn::toAscii($domain);
try {
[$result, $suffixLength] = $this->reverseLookup($domain, $flags);
} catch (PrivateETLDException) {
} catch (PrivateEffectiveTLDException) {
return false;
}
if ($result === Result::NotFound) {
Expand Down Expand Up @@ -171,7 +171,7 @@ private function reverseLookup(string $key, int $flags): array
continue;
}
if (($value & Result::Private) && ($flags & self::FORBID_PRIVATE)) {
throw new PrivateETLDException($key);
throw new PrivateEffectiveTLDException($key);
}
// Save length and return value.
// Since hosts are looked up from right to left, the last saved value will be from the longest match.
Expand Down
4 changes: 2 additions & 2 deletions src/Lookup/SuffixTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Souplette\FusBup\Lookup;

use Souplette\FusBup\Exception\ForbiddenDomainException;
use Souplette\FusBup\Exception\PrivateETLDException;
use Souplette\FusBup\Exception\PrivateEffectiveTLDException;
use Souplette\FusBup\Exception\UnknownTLDException;
use Souplette\FusBup\Lookup\SuffixTree\Flags;
use Souplette\FusBup\Lookup\SuffixTree\Node;
Expand Down Expand Up @@ -53,7 +53,7 @@ public function split(string $domain, int $flags = self::FORBID_NONE): array
continue;
}
if (($nodeFlags & Flags::PRIVATE) && ($flags & self::FORBID_PRIVATE)) {
throw new PrivateETLDException($domain);
throw new PrivateEffectiveTLDException($domain);
}
if ($nodeFlags & Flags::STORE) {
$matches[] = $path;
Expand Down
18 changes: 9 additions & 9 deletions src/PublicSuffixListInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Souplette\FusBup;

use Souplette\FusBup\Exception\PrivateETLDException;
use Souplette\FusBup\Exception\PrivateEffectiveTLDException;
use Souplette\FusBup\Exception\UnknownTLDException;
use Souplette\FusBup\Lookup\LookupInterface;

Expand All @@ -29,8 +29,8 @@ public function isEffectiveTLD(string $domain, int $flags = self::FORBID_NONE):
* Returns the effective TLD of a domain.
*
* @throws UnknownTLDException If FORBID_UNKNOWN flag is set and the TLD is not in the public suffix list.
* @throws PrivateETLDException If FORBID_PRIVATE flag is set and the effective TLD is not in the ICANN section
* of the public suffix list
* @throws PrivateEffectiveTLDException If FORBID_PRIVATE flag is set and the effective TLD
* is not in the ICANN section of the public suffix list
*/
public function getEffectiveTLD(string $domain, int $flags = self::FORBID_NONE): string;

Expand All @@ -40,26 +40,26 @@ public function getEffectiveTLD(string $domain, int $flags = self::FORBID_NONE):
* @returns array{string, string}
*
* @throws UnknownTLDException If FORBID_UNKNOWN flag is set and the TLD is not in the public suffix list.
* @throws PrivateETLDException If FORBID_PRIVATE flag is set and the effective TLD is not in the ICANN section
* of the public suffix list
* @throws PrivateEffectiveTLDException If FORBID_PRIVATE flag is set and the effective TLD
* is not in the ICANN section of the public suffix list
*/
public function splitEffectiveTLD(string $domain, int $flags = self::FORBID_NONE): ?array;

/**
* Returns the registrable part (AKA eTLD+1) of a domain.
*
* @throws UnknownTLDException If FORBID_UNKNOWN flag is set and the TLD is not in the public suffix list.
* @throws PrivateETLDException If FORBID_PRIVATE flag is set and the effective TLD is not in the ICANN section
* of the public suffix list
* @throws PrivateEffectiveTLDException If FORBID_PRIVATE flag is set and the effective TLD
* is not in the ICANN section of the public suffix list
*/
public function getRegistrableDomain(string $domain, int $flags = self::FORBID_NONE): ?string;

/**
* Splits a domain into it's private and registrable parts.
*
* @throws UnknownTLDException If FORBID_UNKNOWN flag is set and the TLD is not in the public suffix list.
* @throws PrivateETLDException If FORBID_PRIVATE flag is set and the effective TLD is not in the ICANN section
* of the public suffix list
* @throws PrivateEffectiveTLDException If FORBID_PRIVATE flag is set and the effective TLD
* is not in the ICANN section of the public suffix list
*/
public function splitRegistrableDomain(string $domain, int $flags = self::FORBID_NONE): ?array;

Expand Down
2 changes: 1 addition & 1 deletion tests/Compiler/PslParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Souplette\FusBup\Compiler\Exception\ParseError;
use Souplette\FusBup\Compiler\Parser\Rule;
use Souplette\FusBup\Compiler\Parser\RuleList;
use Souplette\FusBup\Compiler\Parser\RuleType;
use Souplette\FusBup\Compiler\Parser\Section;
use Souplette\FusBup\Compiler\PslParser;
use Souplette\FusBup\Exception\ParseError;

final class PslParserTest extends TestCase
{
Expand Down
6 changes: 3 additions & 3 deletions tests/Lookup/LookupTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PHPUnit\Framework\TestCase;
use Souplette\FusBup\Compiler\Parser\Rule;
use Souplette\FusBup\Compiler\Parser\RuleType;
use Souplette\FusBup\Exception\PrivateETLDException;
use Souplette\FusBup\Exception\PrivateEffectiveTLDException;
use Souplette\FusBup\Exception\UnknownTLDException;
use Souplette\FusBup\Lookup\LookupInterface;

Expand Down Expand Up @@ -134,7 +134,7 @@ public function testIsEffectiveTLDReturnsFalseForUnknownTLD(array $rules, string
public function testEffectiveLTDDisallowPrivate(array $rules, string $domain): void
{
$lookup = static::compile($rules);
$this->expectException(PrivateETLDException::class);
$this->expectException(PrivateEffectiveTLDException::class);
$lookup->getEffectiveTLD($domain, $lookup::FORBID_PRIVATE);
}

Expand All @@ -150,7 +150,7 @@ public function testEffectiveLTDDisallowUnknown(array $rules, string $domain): v
public function testSplitDisallowPrivate(array $rules, string $domain): void
{
$lookup = static::compile($rules);
$this->expectException(PrivateETLDException::class);
$this->expectException(PrivateEffectiveTLDException::class);
$lookup->split($domain, $lookup::FORBID_PRIVATE);
}

Expand Down

0 comments on commit 350a751

Please sign in to comment.