Skip to content

Commit 751ff1e

Browse files
committed
yoda style
1 parent 68b6461 commit 751ff1e

File tree

7 files changed

+34
-36
lines changed

7 files changed

+34
-36
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ use Pdp\Converter;
7474

7575
$content = file_get_contents('https://raw.githubusercontent.com/publicsuffix/list/master/public_suffix_list.dat');
7676
$converter = new Converter();
77-
$arr_rules = $converter->convert($raw);
77+
$arr_rules = $converter->convert($content);
7878
~~~
7979

8080
Once the PSL has been converted we can feed its data to a `Pdp\Rules` object which is responsible for resolving a given domain name against the PSL rules.

src/Cache.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function get($key, $default = null)
7272
{
7373
$path = $this->getPath($key);
7474
$expires_at = @filemtime($path);
75-
if ($expires_at === false) {
75+
if (false === $expires_at) {
7676
return $default; // file not found
7777
}
7878

@@ -83,16 +83,16 @@ public function get($key, $default = null)
8383
}
8484

8585
$data = @file_get_contents($path);
86-
if ($data === false) {
86+
if (false === $data) {
8787
return $default; // race condition: file not found
8888
}
8989

90-
if ($data === 'b:0;') {
90+
if ('b:0;' === $data) {
9191
return false; // because we can't otherwise distinguish a FALSE return-value from unserialize()
9292
}
9393

9494
$value = @unserialize($data);
95-
if ($value === false) {
95+
if (false === $value) {
9696
return $default; // unserialize() failed
9797
}
9898

@@ -152,7 +152,7 @@ private function getExpireAt($ttl): int
152152
return date_create_immutable('@'.time())->add($ttl)->getTimestamp();
153153
}
154154

155-
if ($ttl === null) {
155+
if (null === $ttl) {
156156
return time() + self::CACHE_TTL;
157157
}
158158

src/Converter.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,19 @@ public function convert(string $content): array
5555
*/
5656
private function getSection(string $section, string $line): string
5757
{
58-
if ($section == '' && strpos($line, '// ===BEGIN ICANN DOMAINS===') === 0) {
58+
if ('' === $section && 0 === strpos($line, '// ===BEGIN ICANN DOMAINS===')) {
5959
return Rules::ICANN_DOMAINS;
6060
}
6161

62-
if ($section == Rules::ICANN_DOMAINS && strpos($line, '// ===END ICANN DOMAINS===') === 0) {
62+
if (Rules::ICANN_DOMAINS === $section && 0 === strpos($line, '// ===END ICANN DOMAINS===')) {
6363
return '';
6464
}
6565

66-
if ($section == '' && strpos($line, '// ===BEGIN PRIVATE DOMAINS===') === 0) {
66+
if ('' === $section && 0 === strpos($line, '// ===BEGIN PRIVATE DOMAINS===')) {
6767
return Rules::PRIVATE_DOMAINS;
6868
}
6969

70-
if ($section == Rules::PRIVATE_DOMAINS && strpos($line, '// ===END PRIVATE DOMAINS===') === 0) {
70+
if (Rules::PRIVATE_DOMAINS === $section && 0 === strpos($line, '// ===END PRIVATE DOMAINS===')) {
7171
return '';
7272
}
7373

@@ -101,7 +101,7 @@ private function addRule(array $list, array $rule_parts): array
101101

102102
$part = idn_to_ascii($part, 0, INTL_IDNA_VARIANT_UTS46);
103103
$isDomain = true;
104-
if (strpos($part, '!') === 0) {
104+
if (0 === strpos($part, '!')) {
105105
$part = substr($part, 1);
106106
$isDomain = false;
107107
}

src/CurlHttpClient.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ public function getContent(string $url): string
3838
CURLOPT_URL => $url,
3939
]);
4040
$content = curl_exec($curl);
41-
if (CURLE_OK !== ($code = curl_errno($curl))) {
42-
$message = curl_error($curl);
43-
curl_close($curl);
44-
throw new HttpClientException($message, $code);
45-
}
41+
$code = curl_errno($curl);
42+
$message = curl_error($curl);
4643
curl_close($curl);
44+
if (CURLE_OK === $code) {
45+
return $content;
46+
}
4747

48-
return $content;
48+
throw new HttpClientException($message, $code);
4949
}
5050
}

src/Domain.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function __construct($domain = null, PublicSuffix $publicSuffix = null)
7070
*/
7171
private function setRegistrableDomain()
7272
{
73-
if (strpos((string) $this->domain, '.') === false) {
73+
if (false === strpos((string) $this->domain, '.')) {
7474
return null;
7575
}
7676

@@ -95,7 +95,7 @@ private function setRegistrableDomain()
9595
private function normalize(string $domain)
9696
{
9797
$func = 'idn_to_utf8';
98-
if (strpos($domain, 'xn--') !== false) {
98+
if (false !== strpos($domain, 'xn--')) {
9999
$func = 'idn_to_ascii';
100100
}
101101

src/Manager.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,8 @@ private function getCacheKey(string $str): string
9696
*/
9797
public function refreshRules(string $source_url = self::PSL_URL): bool
9898
{
99-
static $converter;
100-
$converter = $converter ?? new Converter();
10199
$content = $this->http->getContent($source_url);
102-
$rules = $converter->convert($content);
100+
$rules = (new Converter())->convert($content);
103101
if (empty($rules[Rules::ICANN_DOMAINS]) || empty($rules[Rules::PRIVATE_DOMAINS])) {
104102
return false;
105103
}

src/Rules.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function resolve(string $domain = null, string $type = self::ALL_DOMAINS)
5858
return new Domain();
5959
}
6060

61-
$publicSuffix = $this->findPublicSuffix($type, $domain);
61+
$publicSuffix = $this->findPublicSuffix($domain, $type);
6262
if (null === $publicSuffix->getContent()) {
6363
return new Domain($domain, $this->handleNoMatches($domain));
6464
}
@@ -75,7 +75,7 @@ public function resolve(string $domain = null, string $type = self::ALL_DOMAINS)
7575
*/
7676
private function isMatchable($domain): bool
7777
{
78-
return $domain !== null
78+
return null !== $domain
7979
&& strpos($domain, '.') > 0
8080
&& strlen($domain) === strcspn($domain, '][')
8181
&& !filter_var($domain, FILTER_VALIDATE_IP);
@@ -109,21 +109,21 @@ private function normalize(string $domain): string
109109
/**
110110
* Returns the matched public suffix.
111111
*
112-
* @param string $type
113112
* @param string $domain
113+
* @param string $type
114114
*
115115
* @return PublicSuffix
116116
*/
117-
private function findPublicSuffix(string $type, string $domain): PublicSuffix
117+
private function findPublicSuffix(string $domain, string $type): PublicSuffix
118118
{
119119
$normalizedDomain = $this->normalize($domain);
120120
$reverseLabels = array_reverse(explode('.', $normalizedDomain));
121-
$resultIcann = $this->findPublicSuffixFromSection(self::ICANN_DOMAINS, $reverseLabels);
121+
$resultIcann = $this->findPublicSuffixFromSection($reverseLabels, self::ICANN_DOMAINS);
122122
if (self::ICANN_DOMAINS === $type) {
123123
return $resultIcann;
124124
}
125125

126-
$resultPrivate = $this->findPublicSuffixFromSection(self::PRIVATE_DOMAINS, $reverseLabels);
126+
$resultPrivate = $this->findPublicSuffixFromSection($reverseLabels, self::PRIVATE_DOMAINS);
127127
if (count($resultPrivate) > count($resultIcann)) {
128128
return $resultPrivate;
129129
}
@@ -138,12 +138,12 @@ private function findPublicSuffix(string $type, string $domain): PublicSuffix
138138
/**
139139
* Returns the public suffix matched against a given PSL section.
140140
*
141-
* @param string $type
142141
* @param array $labels
142+
* @param string $type
143143
*
144144
* @return PublicSuffix
145145
*/
146-
private function findPublicSuffixFromSection(string $type, array $labels): PublicSuffix
146+
private function findPublicSuffixFromSection(array $labels, string $type): PublicSuffix
147147
{
148148
$rules = $this->rules[$type];
149149
$matches = [];
@@ -155,7 +155,7 @@ private function findPublicSuffixFromSection(string $type, array $labels): Publi
155155

156156
//match wildcard rule
157157
if (isset($rules['*'])) {
158-
array_unshift($matches, $label);
158+
$matches[] = $label;
159159
break;
160160
}
161161

@@ -169,16 +169,16 @@ private function findPublicSuffixFromSection(string $type, array $labels): Publi
169169
break;
170170
}
171171

172-
array_unshift($matches, $label);
172+
$matches[] = $label;
173173
$rules = $rules[$label];
174174
}
175175

176-
$found = array_filter($matches, 'strlen');
177-
if (empty($found)) {
176+
$foundLabels = array_reverse(array_filter($matches, 'strlen'));
177+
if (empty($foundLabels)) {
178178
return new PublicSuffix();
179179
}
180180

181-
return new PublicSuffix(implode('.', $found), $type);
181+
return new PublicSuffix(implode('.', $foundLabels), $type);
182182
}
183183

184184
/**
@@ -213,7 +213,7 @@ private function handleNoMatches(string $domain): PublicSuffix
213213
*/
214214
private function isPunycoded(string $domain): bool
215215
{
216-
return strpos($domain, 'xn--') !== false;
216+
return false !== strpos($domain, 'xn--');
217217
}
218218

219219
/**

0 commit comments

Comments
 (0)