Clean up missing or inconsistent types in Timer, UA, URL classes

This commit is contained in:
Kijin Sung 2023-10-03 14:42:38 +09:00
parent 433103bf92
commit c93f04f9c1
3 changed files with 39 additions and 38 deletions

View file

@ -20,11 +20,11 @@ class Timer
* @param string $name (optional)
* @return float
*/
public static function start($name = null)
public static function start(string $name = ''): float
{
$timestamp = microtime(true);
if ($name === null)
if ($name === '')
{
$name = 'anon-timer-' . $timestamp;
}
@ -37,17 +37,17 @@ class Timer
* Stop a timer and return the elapsed time.
*
* If the name is not given, the most recently started timer will be stopped.
* If no timer has been started, this method returns false.
* If no timer has been started, this method returns zero.
*
* @param string $name (optional)
* @return float|false
* @return float
*/
public static function stop($name = null)
public static function stop(string $name = ''): float
{
$timestamp = microtime(true);
$started_timestamp = 0;
if ($name === null)
if ($name === '')
{
if (count(self::$_timestamps))
{
@ -55,7 +55,7 @@ class Timer
}
else
{
return false;
return 0;
}
}
elseif (array_key_exists($name, self::$_timestamps))
@ -65,7 +65,7 @@ class Timer
}
else
{
return false;
return 0;
}
return $timestamp - $started_timestamp;
@ -75,15 +75,15 @@ class Timer
* Stop a timer and return the elapsed time in a human-readable format.
*
* If the name is not given, the most recently started timer will be stopped.
* If no timer has been started, this method returns false.
* If no timer has been started, this method returns '0'.
*
* @param string $name (optional)
* @return string|false
* @return string
*/
public static function stopFormat($name = null)
public static function stopFormat(string $name = ''): string
{
$result = self::stop($name);
if ($result === false) return $result;
if ($result === 0) return '0';
return number_format($result * 1000, 1, '.', ',') . 'ms';
}
@ -92,7 +92,7 @@ class Timer
*
* @return float
*/
public static function sinceStartup()
public static function sinceStartup(): float
{
return microtime(true) - \RX_MICROTIME;
}
@ -102,7 +102,7 @@ class Timer
*
* @return string
*/
public static function sinceStartupFormat()
public static function sinceStartupFormat(): string
{
return number_format((microtime(true) - \RX_MICROTIME) * 1000, 1, '.', ',') . 'ms';
}

View file

@ -32,7 +32,7 @@ class UA
* @param string $ua (optional)
* @return bool
*/
public static function isMobile($ua = null)
public static function isMobile(?string $ua = null): bool
{
// Get the User-Agent header if the caller did not specify $ua.
if ($ua === null)
@ -95,10 +95,10 @@ class UA
* @param string $ua (optional)
* @return bool
*/
public static function isTablet($ua = null)
public static function isTablet(?string $ua = null): bool
{
// Get the User-Agent header if the caller did not specify $ua.
$ua = $ua ?: (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null);
$ua = $ua ?: ($_SERVER['HTTP_USER_AGENT'] ?? null);
// If the User-Agent header is missing, it's probably not a tablet.
if (is_null($ua))
@ -140,10 +140,10 @@ class UA
* @param string $ua (optional)
* @return bool
*/
public static function isRobot($ua = null)
public static function isRobot(?string $ua = null): bool
{
// Get the User-Agent header if the caller did not specify $ua.
$ua = $ua ?: (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null);
$ua = $ua ?: ($_SERVER['HTTP_USER_AGENT'] ?? null);
// If the User-Agent header is missing, it's probably not a robot.
if (is_null($ua))
@ -183,10 +183,10 @@ class UA
* @param string $header (optional)
* @return string
*/
public static function getLocale($header = null)
public static function getLocale(?string $header = null): string
{
// Get the Accept-Language header if the caller did not specify $header.
$header = $header ?: (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : 'en-US');
$header = $header ?: ($_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? 'en-US');
// Return the first locale name found.
return preg_match('/^([a-z0-9_-]+)/i', $header, $matches) ? $matches[1] : 'en-US';
@ -198,10 +198,10 @@ class UA
* @param string $ua (optional)
* @return object
*/
public static function getBrowserInfo($ua = null)
public static function getBrowserInfo(?string $ua = null): object
{
// Get the User-Agent header if the caller did not specify $ua.
$ua = $ua ?: (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null);
$ua = $ua ?: ($_SERVER['HTTP_USER_AGENT'] ?? null);
// Initialize the result.
$result = (object)array(
@ -374,10 +374,10 @@ class UA
* @param string $ua (optional)
* @return string
*/
public static function encodeFilenameForDownload($filename, $ua = null)
public static function encodeFilenameForDownload(string $filename, ?string $ua = null): string
{
// Get the User-Agent header if the caller did not specify $ua.
$ua = $ua ?: (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null);
$ua = $ua ?: ($_SERVER['HTTP_USER_AGENT'] ?? null);
// Get the browser name and version.
$browser = self::getBrowserInfo($ua);
@ -463,7 +463,7 @@ class UA
* @param string $color_scheme
* @return void
*/
public static function setColorScheme(string $color_scheme)
public static function setColorScheme(string $color_scheme): void
{
if (in_array($color_scheme, ['light', 'dark']))
{

View file

@ -16,7 +16,7 @@ class URL
* @param array $changes
* @return string
*/
public static function getCurrentURL(array $changes = array())
public static function getCurrentURL(array $changes = []): string
{
$request_uri = preg_replace('/[<>"]/', '', isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/');
$url = self::getCurrentDomainURL($request_uri);
@ -36,7 +36,7 @@ class URL
* @param string $path
* @return string
*/
public static function getCurrentDomainURL($path = '/')
public static function getCurrentDomainURL(string $path = '/'): string
{
$proto = \RX_SSL ? 'https://' : 'http://';
$host = isset($_SERVER['HTTP_HOST']) ? self::decodeIdna($_SERVER['HTTP_HOST']) : 'localhost';
@ -49,7 +49,7 @@ class URL
* @param string $url
* @return string
*/
public static function getCanonicalURL($url)
public static function getCanonicalURL(string $url): string
{
if (preg_match('#^\.?/([^/]|$)#', $url) || !preg_match('#^(https?:|/)#', $url))
{
@ -67,7 +67,7 @@ class URL
* @param string $url
* @return string|false
*/
public static function getDomainFromURL($url)
public static function getDomainFromURL(string $url)
{
$domain = @parse_url($url, \PHP_URL_HOST);
if ($domain === false || $domain === null)
@ -86,7 +86,7 @@ class URL
* @param string $url
* @return bool
*/
public static function isInternalURL($url)
public static function isInternalURL(string $url): bool
{
$domain = self::getDomainFromURL($url);
if ($domain === false)
@ -117,7 +117,7 @@ class URL
* @param array $changes
* @return string
*/
public static function modifyURL($url, array $changes = array())
public static function modifyURL(string $url, array $changes = []): string
{
$url = parse_url(self::getCanonicalURL($url));
$prefix = sprintf('%s://%s%s%s', $url['scheme'], $url['host'], (($url['port'] ?? '') ? (':' . $url['port']) : ''), $url['path']);
@ -143,7 +143,7 @@ class URL
* @param string $path
* @return string|false
*/
public static function fromServerPath($path)
public static function fromServerPath(string $path)
{
$cleanpath = Filters\FilenameFilter::cleanPath($path);
if (substr($path, -1) === '/')
@ -169,16 +169,17 @@ class URL
* e.g. if the URL belongs to an external domain.
*
* @param string $url
* @return string
* @return string|false
*/
public static function toServerPath($url)
public static function toServerPath(string $url)
{
$url = self::getCanonicalURL($url);
if (!self::isInternalURL($url))
{
return false;
}
return Filters\FilenameFilter::cleanPath($_SERVER['DOCUMENT_ROOT'] . parse_url($url, \PHP_URL_PATH));
$root = $_SERVER['DOCUMENT_ROOT'] ?? rtrim(\RX_BASEDIR, '/');
return Filters\FilenameFilter::cleanPath($root . parse_url($url, \PHP_URL_PATH));
}
/**
@ -187,7 +188,7 @@ class URL
* @param string $url
* @return string
*/
public static function encodeIdna($url)
public static function encodeIdna(string $url): string
{
if (preg_match('@[:/#]@', $url))
{
@ -223,7 +224,7 @@ class URL
* @param string $url
* @return string
*/
public static function decodeIdna($url)
public static function decodeIdna(string $url): string
{
if (preg_match('@[:/#]@', $url))
{