Skip to content

Commit

Permalink
Unqualified function/constant reference
Browse files Browse the repository at this point in the history
  • Loading branch information
mambax7 committed Jul 1, 2024
1 parent 1ce07f7 commit e04fbb9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 58 deletions.
62 changes: 31 additions & 31 deletions src/PublicSuffixList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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, '//')) {
Expand All @@ -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);
}
Expand All @@ -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));
}

/**
Expand All @@ -129,23 +129,23 @@ 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 {
$node[$dom] = [];
}
}

if (!$isNotDomain && 0 < count($tldParts)) {
if (!$isNotDomain && 0 < \count($tldParts)) {
$this->buildSubDomain($node[$dom], $tldParts);
}
}
Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -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);
}

/**
Expand All @@ -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;
}

Expand All @@ -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));
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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;
}

Expand All @@ -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);
}
}
}
Expand Down
52 changes: 26 additions & 26 deletions src/RegisteredDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand All @@ -54,7 +54,7 @@ protected function normalizeHost(string $url): string
$separator = '.';
}

return mb_strtolower($utf8Host);
return \mb_strtolower($utf8Host);
}

/**
Expand All @@ -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);
}
Expand All @@ -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;
}
Expand All @@ -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));
Expand Down Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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;
}
Expand All @@ -213,22 +213,22 @@ 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;
}

if ('' === $result) {
return $sub;
} elseif (null !== $result && strlen($result) > 0) {
} elseif (null !== $result && \strlen($result) > 0) {
return $result . '.' . $sub;
}
return null;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/PublicSuffixListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit e04fbb9

Please sign in to comment.