diff --git a/src/PublicSuffixList.php b/src/PublicSuffixList.php index cac6eb6..d2f04a6 100644 --- a/src/PublicSuffixList.php +++ b/src/PublicSuffixList.php @@ -51,7 +51,7 @@ private function setFallbackURL(): void { $this->setLocalPSLName($this->url); if (null === $this->url) { - $this->url = file_exists(__DIR__ . $this->localPSL) ? $this->localPSL : $this->sourceURL; + $this->url = \file_exists(__DIR__ . $this->localPSL) ? $this->localPSL : $this->sourceURL; } } @@ -89,7 +89,7 @@ private function loadTree(): void */ private function parsePSL(string $fileData): void { - $lines = explode("\n", $fileData); + $lines = \explode("\n", $fileData); foreach ($lines as $line) { if ('' === $line || $this->startsWith($line, '//')) { @@ -102,7 +102,7 @@ private function parsePSL(string $fileData): void } // This line should be a TLD - $tldParts = explode('.', $line); + $tldParts = \explode('.', $line); $this->buildSubDomain($this->tree, $tldParts); } @@ -117,7 +117,7 @@ private function parsePSL(string $fileData): void */ private function startsWith(string $search, string $startString): bool { - return (0 === strpos($search, $startString)); + return (0 === \strpos($search, $startString)); } /** @@ -129,15 +129,15 @@ private function startsWith(string $search, string $startString): bool */ private function buildSubDomain(array &$node, array $tldParts): void { - $dom = trim(array_pop($tldParts)); + $dom = \trim(\array_pop($tldParts)); $isNotDomain = false; if ($this->startsWith($dom, '!')) { - $dom = substr($dom, 1); + $dom = \substr($dom, 1); $isNotDomain = true; } - if (!array_key_exists($dom, $node)) { + if (!\array_key_exists($dom, $node)) { if ($isNotDomain) { $node[$dom] = ['!' => '']; } else { @@ -145,7 +145,7 @@ private function buildSubDomain(array &$node, array $tldParts): void } } - if (!$isNotDomain && 0 < count($tldParts)) { + if (!$isNotDomain && 0 < \count($tldParts)) { $this->buildSubDomain($node[$dom], $tldParts); } } @@ -172,10 +172,10 @@ public function getTree(): array */ private function readPSL() { - $parts = parse_url($this->url); + $parts = \parse_url($this->url); $remote = isset($parts['scheme']) || isset($parts['host']); // try to read with file_get_contents - $newPSL = file_get_contents(($remote ? '' : __DIR__) . $this->url); + $newPSL = \file_get_contents(($remote ? '' : __DIR__) . $this->url); if (false !== $newPSL) { if ($remote) { $this->saveLocalPSL($newPSL); @@ -184,14 +184,14 @@ private function readPSL() } // try again with curl if file_get_contents failed - if (function_exists('curl_init') && false !== ($curlHandle = curl_init())) { - curl_setopt($curlHandle, CURLOPT_URL, $this->url); - curl_setopt($curlHandle, CURLOPT_FAILONERROR, true); - curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($curlHandle, CURLOPT_CONNECTTIMEOUT, 5); - $curlReturn = curl_exec($curlHandle); - curl_close($curlHandle); - if (false !== $curlReturn && is_string($curlReturn)) { + if (\function_exists('curl_init') && false !== ($curlHandle = \curl_init())) { + \curl_setopt($curlHandle, \CURLOPT_URL, $this->url); + \curl_setopt($curlHandle, \CURLOPT_FAILONERROR, true); + \curl_setopt($curlHandle, \CURLOPT_RETURNTRANSFER, 1); + \curl_setopt($curlHandle, \CURLOPT_CONNECTTIMEOUT, 5); + $curlReturn = \curl_exec($curlHandle); + \curl_close($curlHandle); + if (false !== $curlReturn && \is_string($curlReturn)) { if ($remote) { $this->saveLocalPSL($curlReturn); } @@ -209,7 +209,7 @@ private function readPSL() */ private function getCacheFileName(string $url): string { - return __DIR__ . $this->dataDir . $this->cachedPrefix . md5($url); + return __DIR__ . $this->dataDir . $this->cachedPrefix . \md5($url); } /** @@ -221,8 +221,8 @@ private function getCacheFileName(string $url): string private function readCachedPSL(string $url): ?array { $cacheFile = $this->getCacheFileName($url); - return file_exists($cacheFile) - ? unserialize(file_get_contents($cacheFile), ['allowed_classes' => false]) + return \file_exists($cacheFile) + ? \unserialize(\file_get_contents($cacheFile), ['allowed_classes' => false]) : null; } @@ -234,7 +234,7 @@ private function readCachedPSL(string $url): ?array */ private function cachePSL(string $url) { - return file_put_contents($this->getCacheFileName($url), serialize($this->tree)); + return \file_put_contents($this->getCacheFileName($url), \serialize($this->tree)); } /** @@ -245,7 +245,7 @@ private function cachePSL(string $url) */ private function saveLocalPSL(string $fileContents) { - return file_put_contents(__DIR__ . $this->localPSL, $fileContents); + return \file_put_contents(__DIR__ . $this->localPSL, $fileContents); } /** @@ -259,8 +259,8 @@ private function setLocalPSLName(?string $url): void if (null === $url) { $url = $this->sourceURL; } - $parts = parse_url($url); - $fileName = basename($parts['path']); + $parts = \parse_url($url); + $fileName = \basename($parts['path']); $this->localPSL = $this->dataDir . $fileName; } @@ -273,15 +273,15 @@ private function setLocalPSLName(?string $url): void public function clearDataDirectory(bool $cacheOnly = false): void { $dir = __DIR__ . $this->dataDir; - if (is_dir($dir)) { - if (false !== ($dirHandle = opendir($dir))) { - while (false !== ($file = readdir($dirHandle))) { - if ('file' === filetype($dir . $file) + if (\is_dir($dir)) { + if (false !== ($dirHandle = \opendir($dir))) { + while (false !== ($file = \readdir($dirHandle))) { + if ('file' === \filetype($dir . $file) && (!$cacheOnly || $this->startsWith($file, $this->cachedPrefix))) { - unlink($dir . $file); + \unlink($dir . $file); } } - closedir($dirHandle); + \closedir($dirHandle); } } } diff --git a/src/RegisteredDomain.php b/src/RegisteredDomain.php index a405ece..6075dd6 100644 --- a/src/RegisteredDomain.php +++ b/src/RegisteredDomain.php @@ -42,10 +42,10 @@ protected function normalizeHost(string $url): string if (empty($url)) { return ''; } - $host = (false !== strpos($url, '/')) ? parse_url($url, PHP_URL_HOST) : $url; + $host = (false !== \strpos($url, '/')) ? \parse_url($url, \PHP_URL_HOST) : $url; $host = $host ?: ''; // Ensure $host is a string, even if parse_url returns false - $parts = explode('.', $host); + $parts = \explode('.', $host); $utf8Host = ''; $separator = ''; @@ -54,7 +54,7 @@ protected function normalizeHost(string $url): string $separator = '.'; } - return mb_strtolower($utf8Host); + return \mb_strtolower($utf8Host); } /** @@ -66,9 +66,9 @@ protected function normalizeHost(string $url): string */ protected function convertPunycode(string $part): string { - if (0 === strpos($part, 'xn--')) { - if (function_exists('idn_to_utf8')) { - return defined('INTL_IDNA_VARIANT_UTS46') ? idn_to_utf8($part, 0, INTL_IDNA_VARIANT_UTS46) : idn_to_utf8($part); + if (0 === \strpos($part, 'xn--')) { + if (\function_exists('idn_to_utf8')) { + return \defined('INTL_IDNA_VARIANT_UTS46') ? \idn_to_utf8($part, 0, \INTL_IDNA_VARIANT_UTS46) : \idn_to_utf8($part); } return $this->decodePunycode($part); } @@ -93,11 +93,11 @@ protected function decodePunycode(string $encoded): string $skew = 38; $damp = 700; - if (0 !== strpos($encoded, $prefix)) { + if (0 !== \strpos($encoded, $prefix)) { return $encoded; } - $trimmed = trim(str_replace($prefix, '', $encoded)); + $trimmed = \trim(\str_replace($prefix, '', $encoded)); if ('' === $trimmed) { return $encoded; } @@ -109,18 +109,18 @@ protected function decodePunycode(string $encoded): string $decoded = []; $output = ''; - $delim_pos = strrpos($encoded, '-'); - if ($delim_pos > strlen($prefix)) { - for ($k = strlen($prefix); $k < $delim_pos; ++$k) { - $decoded[] = ord($encoded[$k]); + $delim_pos = \strrpos($encoded, '-'); + if ($delim_pos > \strlen($prefix)) { + for ($k = \strlen($prefix); $k < $delim_pos; ++$k) { + $decoded[] = \ord($encoded[$k]); } } - $deco_len = count($decoded); - $enco_len = strlen($encoded); + $deco_len = \count($decoded); + $enco_len = \strlen($encoded); for ($enco_idx = $delim_pos ? ($delim_pos + 1) : 0; $enco_idx < $enco_len; ++$deco_len) { for ($old_idx = $idx, $w = 1, $k = $base; 1; $k += $base) { - $cp = ord($encoded[$enco_idx++]); + $cp = \ord($encoded[$enco_idx++]); $digit = ($cp - 48 < 10) ? $cp - 22 : (($cp - 65 < 26) ? $cp - 65 : (($cp - 97 < 26) ? $cp - 97 : $base)); $idx += $digit * $w; $t = ($k <= $bias) ? $tmin : (($k >= $bias + $tmax) ? $tmax : ($k - $bias)); @@ -153,16 +153,16 @@ protected function decodePunycode(string $encoded): string foreach ($decoded as $k => $v) { if ($v < 128) { - $output .= chr($v); + $output .= \chr($v); } // 7bit are transferred literally elseif ($v < (1 << 11)) { - $output .= chr(192 + ($v >> 6)) . chr(128 + ($v & 63)); + $output .= \chr(192 + ($v >> 6)) . \chr(128 + ($v & 63)); } // 2 bytes elseif ($v < (1 << 16)) { - $output .= chr(224 + ($v >> 12)) . chr(128 + (($v >> 6) & 63)) . chr(128 + ($v & 63)); + $output .= \chr(224 + ($v >> 12)) . \chr(128 + (($v >> 6) & 63)) . \chr(128 + ($v & 63)); } // 3 bytes elseif ($v < (1 << 21)) { - $output .= chr(240 + ($v >> 18)) . chr(128 + (($v >> 12) & 63)) . chr(128 + (($v >> 6) & 63)) . chr(128 + ($v & 63)); + $output .= \chr(240 + ($v >> 18)) . \chr(128 + (($v >> 12) & 63)) . \chr(128 + (($v >> 6) & 63)) . \chr(128 + ($v & 63)); } // 4 bytes else { $output .= $safe_char; @@ -183,7 +183,7 @@ public function getRegisteredDomain(string $host): ?string $this->tree = $this->psl->getTree(); $signingDomain = $this->normalizeHost($host); - $signingDomainParts = explode('.', $signingDomain); + $signingDomainParts = \explode('.', $signingDomain); $result = $this->findRegisteredDomain($signingDomainParts, $this->tree); @@ -193,8 +193,8 @@ public function getRegisteredDomain(string $host): ?string } // assure there is at least 1 TLD in the stripped signing domain - if (!strpos($result, '.')) { - $cnt = count($signingDomainParts); + if (!\strpos($result, '.')) { + $cnt = \count($signingDomainParts); if (1 === $cnt || '' === $signingDomainParts[$cnt-2]) { return null; } @@ -213,14 +213,14 @@ public function getRegisteredDomain(string $host): ?string */ protected function findRegisteredDomain(array $remainingSigningDomainParts, array &$treeNode): ?string { - $sub = array_pop($remainingSigningDomainParts); + $sub = \array_pop($remainingSigningDomainParts); $result = null; if (isset($treeNode['!'])) { return ''; - } elseif (array_key_exists($sub, $treeNode)) { + } elseif (\array_key_exists($sub, $treeNode)) { $result = $this->findRegisteredDomain($remainingSigningDomainParts, $treeNode[$sub]); - } elseif (array_key_exists('*', $treeNode)) { + } elseif (\array_key_exists('*', $treeNode)) { $result = $this->findRegisteredDomain($remainingSigningDomainParts, $treeNode['*']); } else { return $sub; @@ -228,7 +228,7 @@ protected function findRegisteredDomain(array $remainingSigningDomainParts, arra if ('' === $result) { return $sub; - } elseif (null !== $result && strlen($result) > 0) { + } elseif (null !== $result && \strlen($result) > 0) { return $result . '.' . $sub; } return null; diff --git a/tests/unit/PublicSuffixListTest.php b/tests/unit/PublicSuffixListTest.php index 88fbd4a..ee15f59 100644 --- a/tests/unit/PublicSuffixListTest.php +++ b/tests/unit/PublicSuffixListTest.php @@ -88,7 +88,7 @@ public function testFallbackURL() $property->setAccessible(true); // $expectedUrl = file_exists(dirname(__DIR__, 2) . '/data/public_suffix_list.dat') ? dirname(__DIR__, 2) . '/data/public_suffix_list.dat' : 'https://publicsuffix.org/list/public_suffix_list.dat'; - $expectedUrl = file_exists(dirname(__DIR__, 2) . '/data/public_suffix_list.dat') ? '/../data/public_suffix_list.dat' : 'https://publicsuffix.org/list/public_suffix_list.dat'; + $expectedUrl = \\file_exists(\\dirname(__DIR__, 2) . '/data/public_suffix_list.dat') ? '/../data/public_suffix_list.dat' : 'https://publicsuffix.org/list/public_suffix_list.dat'; $this->assertSame($expectedUrl, $property->getValue($this->object)); } }