1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BenTools\HostnameExtractor\SuffixProvider; |
4
|
|
|
|
5
|
|
|
use function BenTools\Violin\string; |
6
|
|
|
use CallbackFilterIterator; |
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
|
9
|
|
|
final class PublicSuffixProvider implements SuffixProviderInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Client |
13
|
|
|
*/ |
14
|
|
|
private $client; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $listUrl; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $cache; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* PublicSuffixProvider constructor. |
28
|
|
|
* |
29
|
|
|
* @param Client|null $client |
30
|
|
|
* @param string $listUrl |
31
|
|
|
*/ |
32
|
|
|
public function __construct( |
33
|
|
|
Client $client = null, |
34
|
|
|
string $listUrl = 'https://publicsuffix.org/list/public_suffix_list.dat' |
35
|
|
|
) { |
36
|
|
|
|
37
|
|
|
$this->client = $client ?? new Client(); |
38
|
|
|
$this->listUrl = $listUrl; |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param bool $force |
43
|
|
|
*/ |
44
|
|
|
public function fetchSuffixes(bool $force = false): void |
45
|
|
|
{ |
46
|
|
|
if (null === $this->cache || true === $force) { |
47
|
|
|
$content = $this->client->get($this->listUrl)->getBody(); |
48
|
|
|
// Create an iterator for the document |
49
|
|
|
$iterator = (function (string $content) { |
50
|
|
|
$tok = \strtok($content, "\r\n"); |
51
|
|
|
while (false !== $tok) { |
52
|
|
|
$line = $tok; |
53
|
|
|
$tok = \strtok("\r\n"); |
54
|
|
|
|
55
|
|
|
// Remove "*." prefixes |
56
|
|
|
if (0 === \strpos($line, '*.')) { |
57
|
|
|
$line = \substr($line, 2, mb_strlen($line) - 2); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
yield $line; |
61
|
|
|
} |
62
|
|
|
})($content); |
63
|
|
|
|
64
|
|
|
// Ignore commented lines |
65
|
|
|
$valid = function (string $string) { |
66
|
|
|
return 0 !== \strpos($string, '//'); |
67
|
|
|
}; |
68
|
|
|
$suffixes = \iterator_to_array(new CallbackFilterIterator($iterator, $valid)); |
69
|
|
|
|
70
|
|
|
// Sort by suffix length |
71
|
|
|
\usort($suffixes, function (string $a, string $b) { |
72
|
|
|
return \count(string($b)) <=> \count(string($a)); |
73
|
|
|
}); |
74
|
|
|
|
75
|
|
|
$this->cache = $suffixes; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritDoc |
81
|
|
|
*/ |
82
|
|
|
public function getSuffixes(): iterable |
83
|
|
|
{ |
84
|
|
|
$this->fetchSuffixes(); |
85
|
|
|
yield from ($this->cache ?? []); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.