Add type hints and use class constants where applicable in Rhymix Framework

This commit is contained in:
Kijin Sung 2023-10-03 02:33:24 +09:00
parent 78f7942e40
commit b6e8d41be8
11 changed files with 138 additions and 128 deletions

View file

@ -14,12 +14,12 @@ class Calendar
* @param bool $long_format (optional, default is true) * @param bool $long_format (optional, default is true)
* @return string * @return string
*/ */
public static function getMonthName($month_number, $long_format = true) public static function getMonthName(int $month_number, bool $long_format = true): string
{ {
$month_number = intval($month_number, 10); $month_number = intval($month_number, 10);
if (!is_between($month_number, 1, 12)) if (!is_between($month_number, 1, 12))
{ {
return false; throw new Exception('Invalid month number: ' . $month_number);
} }
return date($long_format ? 'F' : 'M', mktime(0, 0, 0, $month_number, 1)); return date($long_format ? 'F' : 'M', mktime(0, 0, 0, $month_number, 1));
@ -35,12 +35,11 @@ class Calendar
* @param int $year (optional) * @param int $year (optional)
* @return int * @return int
*/ */
public static function getMonthStartDayOfWeek($month_number, $year = null) public static function getMonthStartDayOfWeek(int $month_number, ?int $year = null): int
{ {
$month_number = intval($month_number, 10);
if (!is_between($month_number, 1, 12)) if (!is_between($month_number, 1, 12))
{ {
return false; throw new Exception('Invalid month number: ' . $month_number);
} }
return (int)date('w', mktime(0, 0, 0, $month_number, 1, $year ?: date('Y'))); return (int)date('w', mktime(0, 0, 0, $month_number, 1, $year ?: date('Y')));
@ -56,12 +55,11 @@ class Calendar
* @param int $year (optional) * @param int $year (optional)
* @return int * @return int
*/ */
public static function getMonthDays($month_number, $year = null) public static function getMonthDays(int $month_number, ?int $year = null): int
{ {
$month_number = intval($month_number, 10);
if (!is_between($month_number, 1, 12)) if (!is_between($month_number, 1, 12))
{ {
return false; throw new Exception('Invalid month number: ' . $month_number);
} }
return (int)date('t', mktime(0, 0, 0, $month_number, 1, $year ?: date('Y'))); return (int)date('t', mktime(0, 0, 0, $month_number, 1, $year ?: date('Y')));
@ -81,20 +79,19 @@ class Calendar
* @param int $start_dow (optional) * @param int $start_dow (optional)
* @return array * @return array
*/ */
public static function getMonthCalendar($month_number, $year = null, $start_dow = 0) public static function getMonthCalendar(int $month_number, ?int $year = null, int $start_dow = 0): array
{ {
$month_number = intval($month_number, 10);
if (!is_between($month_number, 1, 12)) if (!is_between($month_number, 1, 12))
{ {
return false; throw new Exception('Invalid month number: ' . $month_number);
} }
if (!is_between($start_dow, 0, 6)) if (!is_between($start_dow, 0, 6))
{ {
return false; throw new Exception('Invalid first day of week: ' . $start_dow);
} }
if (!$year || !is_between($year, 1000, 9999)) if (!$year || !is_between($year, 1000, 9999))
{ {
$year = date('Y'); $year = (int)date('Y');
} }
$start = self::getMonthStartDayOfWeek($month_number, $year); $start = self::getMonthStartDayOfWeek($month_number, $year);

View file

@ -15,23 +15,23 @@ class Config
/** /**
* Location of configuration files. * Location of configuration files.
*/ */
public static $config_filename = 'files/config/config.php'; public const CONFIG_FILENAME = 'files/config/config.php';
public static $old_db_config_filename = 'files/config/db.config.php'; public const OLD_DB_CONFIG_PATH = 'files/config/db.config.php';
public static $old_ftp_config_filename = 'files/config/ftp.config.php'; public const OLD_FTP_CONFIG_PATH = 'files/config/ftp.config.php';
public static $old_lang_config_filename = 'files/config/lang_selected.info'; public const OLD_LANG_CONFIG_PATH = 'files/config/lang_selected.info';
public static $default_config_filename = 'common/defaults/config.php'; public const DEFAULT_CONFIG_PATH = 'common/defaults/config.php';
/** /**
* Load system configuration. * Load system configuration.
* *
* @return void * @return array
*/ */
public static function init() public static function init(): array
{ {
if (file_exists(\RX_BASEDIR . self::$config_filename)) if (file_exists(\RX_BASEDIR . self::CONFIG_FILENAME))
{ {
ob_start(); ob_start();
self::$_config = (include \RX_BASEDIR . self::$config_filename); self::$_config = (include \RX_BASEDIR . self::CONFIG_FILENAME);
ob_end_clean(); ob_end_clean();
} }
else else
@ -47,7 +47,7 @@ class Config
$GLOBALS['RX_NAMESPACES'] = self::$_config['namespaces']; $GLOBALS['RX_NAMESPACES'] = self::$_config['namespaces'];
} }
return self::$_config; return self::$_config ?: [];
} }
/** /**
@ -55,9 +55,9 @@ class Config
* *
* @return array * @return array
*/ */
public static function getAll() public static function getAll(): array
{ {
return self::$_config; return self::$_config ?: [];
} }
/** /**
@ -65,9 +65,9 @@ class Config
* *
* @return array * @return array
*/ */
public static function getDefaults() public static function getDefaults(): array
{ {
return (include \RX_BASEDIR . self::$default_config_filename); return (include \RX_BASEDIR . self::DEFAULT_CONFIG_PATH);
} }
/** /**
@ -76,7 +76,7 @@ class Config
* @param string $key * @param string $key
* @return mixed * @return mixed
*/ */
public static function get($key) public static function get(string $key)
{ {
if (!count(self::$_config)) if (!count(self::$_config))
{ {
@ -102,7 +102,7 @@ class Config
* @param mixed $value * @param mixed $value
* @return void * @return void
*/ */
public static function set($key, $value) public static function set(string $key, $value): void
{ {
if (!count(self::$_config)) if (!count(self::$_config))
{ {
@ -123,7 +123,7 @@ class Config
* @param array $config * @param array $config
* @return void * @return void
*/ */
public static function setAll($config) public static function setAll(array $config): void
{ {
self::$_config = $config; self::$_config = $config;
} }
@ -134,7 +134,7 @@ class Config
* @param array $config (optional) * @param array $config (optional)
* @return bool * @return bool
*/ */
public static function save($config = null) public static function save(?array $config = null): bool
{ {
if ($config) if ($config)
{ {
@ -142,10 +142,10 @@ class Config
} }
// Backup the main config file. // Backup the main config file.
$config_filename = \RX_BASEDIR . self::$config_filename; $config_filename = \RX_BASEDIR . self::CONFIG_FILENAME;
if (Storage::exists($config_filename)) if (Storage::exists($config_filename))
{ {
$backup_filename = \RX_BASEDIR . self::$config_filename . '.backup.' . time() . '.php'; $backup_filename = \RX_BASEDIR . self::CONFIG_FILENAME . '.backup.' . time() . '.php';
$result = Storage::copy($config_filename, $backup_filename); $result = Storage::copy($config_filename, $backup_filename);
clearstatcache(true, $backup_filename); clearstatcache(true, $backup_filename);
if (!$result || filesize($config_filename) !== filesize($backup_filename)) if (!$result || filesize($config_filename) !== filesize($backup_filename))
@ -169,8 +169,8 @@ class Config
// Save XE-compatible config files. // Save XE-compatible config files.
$warning = '// THIS FILE IS NOT USED IN RHYMIX.' . "\n" . '// TO MODIFY SYSTEM CONFIGURATION, EDIT config.php INSTEAD.'; $warning = '// THIS FILE IS NOT USED IN RHYMIX.' . "\n" . '// TO MODIFY SYSTEM CONFIGURATION, EDIT config.php INSTEAD.';
$buff = '<?php' . "\n" . $warning . "\n"; $buff = '<?php' . "\n" . $warning . "\n";
Storage::write(\RX_BASEDIR . self::$old_db_config_filename, $buff); Storage::write(\RX_BASEDIR . self::OLD_DB_CONFIG_PATH, $buff);
Storage::write(\RX_BASEDIR . self::$old_ftp_config_filename, $buff); Storage::write(\RX_BASEDIR . self::OLD_FTP_CONFIG_PATH, $buff);
return true; return true;
} }
@ -180,7 +180,7 @@ class Config
* @param mixed $value * @param mixed $value
* @return string * @return string
*/ */
public static function serialize($value) public static function serialize($value): string
{ {
if (is_object($value)) if (is_object($value))
{ {

View file

@ -12,6 +12,11 @@ class DateTime
*/ */
protected static $_timezones = array(); protected static $_timezones = array();
/**
* Define the relative format constant.
*/
public const FORMAT_RELATIVE = 'relative';
/** /**
* Format a Unix timestamp using the internal timezone. * Format a Unix timestamp using the internal timezone.
* *
@ -19,9 +24,9 @@ class DateTime
* @param int $timestamp Unix timestamp (optional, default is now) * @param int $timestamp Unix timestamp (optional, default is now)
* @return string * @return string
*/ */
public static function formatTimestamp($format, $timestamp = null) public static function formatTimestamp(string $format, ?int $timestamp = null): string
{ {
if ($format === 'relative') if ($format === self::FORMAT_RELATIVE)
{ {
return self::getRelativeTimestamp($timestamp ?: time()); return self::getRelativeTimestamp($timestamp ?: time());
} }
@ -37,9 +42,9 @@ class DateTime
* @param int $timestamp Unix timestamp (optional, default is now) * @param int $timestamp Unix timestamp (optional, default is now)
* @return string * @return string
*/ */
public static function formatTimestampForCurrentUser($format, $timestamp = null) public static function formatTimestampForCurrentUser(string $format, ?int $timestamp = null): string
{ {
if ($format === 'relative') if ($format === self::FORMAT_RELATIVE)
{ {
return self::getRelativeTimestamp($timestamp ?: time()); return self::getRelativeTimestamp($timestamp ?: time());
} }
@ -60,7 +65,7 @@ class DateTime
* *
* @return string * @return string
*/ */
public static function getTimezoneForCurrentUser() public static function getTimezoneForCurrentUser(): string
{ {
if (isset($_SESSION['RHYMIX']['timezone']) && $_SESSION['RHYMIX']['timezone']) if (isset($_SESSION['RHYMIX']['timezone']) && $_SESSION['RHYMIX']['timezone'])
{ {
@ -68,11 +73,11 @@ class DateTime
} }
elseif ($default = \Context::get('_default_timezone')) elseif ($default = \Context::get('_default_timezone'))
{ {
return $default; return (string)$default;
} }
elseif ($default = Config::get('locale.default_timezone')) elseif ($default = Config::get('locale.default_timezone'))
{ {
return $default; return (string)$default;
} }
else else
{ {
@ -85,7 +90,7 @@ class DateTime
* *
* @return array * @return array
*/ */
public static function getTimezoneList() public static function getTimezoneList(): array
{ {
$result = array(); $result = array();
$tzlist = \DateTimeZone::listIdentifiers(); $tzlist = \DateTimeZone::listIdentifiers();
@ -111,7 +116,7 @@ class DateTime
* @param int $timestamp Unix timestamp (optional, default is now) * @param int $timestamp Unix timestamp (optional, default is now)
* @return int * @return int
*/ */
public static function getTimezoneOffset($timezone, $timestamp = null) public static function getTimezoneOffset(string $timezone, ?int $timestamp = null): int
{ {
if (!isset(self::$_timezones[$timezone])) if (!isset(self::$_timezones[$timezone]))
{ {
@ -130,9 +135,9 @@ class DateTime
* @param int $timestamp Unix timestamp (optional, default is now) * @param int $timestamp Unix timestamp (optional, default is now)
* @return int * @return int
*/ */
public static function getTimezoneOffsetFromInternal($timezone, $timestamp = null) public static function getTimezoneOffsetFromInternal(string $timezone, ?int $timestamp = null): int
{ {
return self::getTimezoneOffset($timezone, $timestamp) - Config::get('locale.internal_timezone'); return self::getTimezoneOffset($timezone, $timestamp ?: time()) - Config::get('locale.internal_timezone');
} }
/** /**
@ -141,7 +146,7 @@ class DateTime
* @param string $timezone * @param string $timezone
* @return int * @return int
*/ */
public static function getTimezoneOffsetByLegacyFormat($timezone) public static function getTimezoneOffsetByLegacyFormat(string $timezone): int
{ {
$multiplier = ($timezone[0] === '-') ? -60 : 60; $multiplier = ($timezone[0] === '-') ? -60 : 60;
$timezone = preg_replace('/[^0-9]/', '', $timezone); $timezone = preg_replace('/[^0-9]/', '', $timezone);
@ -159,9 +164,9 @@ class DateTime
* so this should never be a problem in practice. * so this should never be a problem in practice.
* *
* @param int $offset * @param int $offset
* @return bool * @return string
*/ */
public static function getTimezoneNameByOffset($offset) public static function getTimezoneNameByOffset(int $offset): string
{ {
switch ($offset) switch ($offset)
{ {
@ -185,9 +190,9 @@ class DateTime
* @param int $timestamp * @param int $timestamp
* @return string * @return string
*/ */
public static function getRelativeTimestamp($timestamp) public static function getRelativeTimestamp(?int $timestamp = null): string
{ {
$diff = \RX_TIME - $timestamp; $diff = \RX_TIME - intval($timestamp ?: time());
$langs = lang('common.time_gap'); $langs = lang('common.time_gap');
if ($diff < 3) if ($diff < 3)

View file

@ -26,7 +26,7 @@ class Formatter
* @param int $options (optional) * @param int $options (optional)
* @return string * @return string
*/ */
public static function text2html($text, $options = 0) public static function text2html(string $text, int $options = 0): string
{ {
// This option uses <p> instead of <br> to separate lines. // This option uses <p> instead of <br> to separate lines.
if ($options & self::TEXT_NEWLINE_AS_P) if ($options & self::TEXT_NEWLINE_AS_P)
@ -63,7 +63,7 @@ class Formatter
* @param string $html * @param string $html
* @return string * @return string
*/ */
public static function html2text($html) public static function html2text(string $html): string
{ {
// Add line breaks after <br> and <p> tags. // Add line breaks after <br> and <p> tags.
$html = preg_replace('!<br[^>]*>\s*!i', "\n", $html); $html = preg_replace('!<br[^>]*>\s*!i', "\n", $html);
@ -97,7 +97,7 @@ class Formatter
* @param int $options (optional) * @param int $options (optional)
* @return string * @return string
*/ */
public static function markdown2html($markdown, $options = 0) public static function markdown2html(string $markdown, int $options = 0): string
{ {
if ($options & self::MD_ENABLE_EXTRA) if ($options & self::MD_ENABLE_EXTRA)
{ {
@ -126,7 +126,7 @@ class Formatter
* @param string $html * @param string $html
* @return string * @return string
*/ */
public static function html2markdown($html) public static function html2markdown(string $html): string
{ {
$converter = new \League\HTMLToMarkdown\HtmlConverter(); $converter = new \League\HTMLToMarkdown\HtmlConverter();
$converter->getConfig()->setOption('bold_style', '**'); $converter->getConfig()->setOption('bold_style', '**');
@ -140,7 +140,7 @@ class Formatter
* @param string $bbcode * @param string $bbcode
* @return string * @return string
*/ */
public static function bbcode($bbcode) public static function bbcode(string $bbcode): string
{ {
$parser = new \JBBCode\Parser; $parser = new \JBBCode\Parser;
$parser->addCodeDefinitionSet(new \JBBCode\DefaultCodeDefinitionSet()); $parser->addCodeDefinitionSet(new \JBBCode\DefaultCodeDefinitionSet());
@ -162,7 +162,7 @@ class Formatter
* @param string $html * @param string $html
* @return string * @return string
*/ */
public static function applySmartQuotes($html) public static function applySmartQuotes(string $html): string
{ {
return \Michelf\SmartyPants::defaultTransform($html, 'qbBdDiew'); return \Michelf\SmartyPants::defaultTransform($html, 'qbBdDiew');
} }
@ -173,10 +173,10 @@ class Formatter
* @param string|array $source_filename * @param string|array $source_filename
* @param string $target_filename * @param string $target_filename
* @param array $variables (optional) * @param array $variables (optional)
* @parsm bool $minify (optional) * @param bool $minify (optional)
* @return bool * @return bool
*/ */
public static function compileLESS($source_filename, $target_filename, $variables = array(), $minify = false) public static function compileLESS($source_filename, string $target_filename, array $variables = [], bool $minify = false): bool
{ {
// Get the cleaned and concatenated content. // Get the cleaned and concatenated content.
$imported_list = []; $imported_list = [];
@ -221,10 +221,10 @@ class Formatter
* @param string|array $source_filename * @param string|array $source_filename
* @param string $target_filename * @param string $target_filename
* @param array $variables (optional) * @param array $variables (optional)
* @parsm bool $minify (optional) * @param bool $minify (optional)
* @return bool * @return bool
*/ */
public static function compileSCSS($source_filename, $target_filename, $variables = array(), $minify = false) public static function compileSCSS($source_filename, string $target_filename, array $variables = [], bool $minify = false): bool
{ {
// Get the cleaned and concatenated content. // Get the cleaned and concatenated content.
$imported_list = []; $imported_list = [];
@ -301,7 +301,7 @@ class Formatter
* @param string $target_filename * @param string $target_filename
* @return bool * @return bool
*/ */
public static function minifyCSS($source_filename, $target_filename) public static function minifyCSS($source_filename, string $target_filename): bool
{ {
$minifier = new \MatthiasMullie\Minify\CSS(); $minifier = new \MatthiasMullie\Minify\CSS();
$minifier->setMaxImportSize(5); $minifier->setMaxImportSize(5);
@ -329,7 +329,7 @@ class Formatter
* @param string $target_filename * @param string $target_filename
* @return bool * @return bool
*/ */
public static function minifyJS($source_filename, $target_filename) public static function minifyJS($source_filename, string $target_filename): bool
{ {
$minifier = new \MatthiasMullie\Minify\JS(); $minifier = new \MatthiasMullie\Minify\JS();
if (is_array($source_filename)) if (is_array($source_filename))
@ -357,7 +357,7 @@ class Formatter
* @param array &$imported_list * @param array &$imported_list
* @return string * @return string
*/ */
public static function concatCSS($source_filename, $target_filename, $add_comment = true, &$imported_list = []) public static function concatCSS($source_filename, string $target_filename, bool $add_comment = true, array &$imported_list = []): string
{ {
$charsets = []; $charsets = [];
$imported_urls = []; $imported_urls = [];
@ -523,10 +523,9 @@ class Formatter
* JS concatenation subroutine. * JS concatenation subroutine.
* *
* @param string|array $source_filename * @param string|array $source_filename
* @param string $target_filename
* @return string * @return string
*/ */
public static function concatJS($source_filename, $target_filename) public static function concatJS($source_filename): string
{ {
$result = ''; $result = '';

View file

@ -13,7 +13,7 @@ class Image
* @param string $filename * @param string $filename
* @return bool * @return bool
*/ */
public static function isImage($filename) public static function isImage(string $filename): bool
{ {
return array_shift(explode('/', MIME::getContentType($filename))) === 'image'; return array_shift(explode('/', MIME::getContentType($filename))) === 'image';
} }
@ -24,7 +24,7 @@ class Image
* @param string $filename * @param string $filename
* @return bool * @return bool
*/ */
public static function isAnimatedGIF($filename) public static function isAnimatedGIF(string $filename): bool
{ {
if (MIME::getContentType($filename) !== 'image/gif') if (MIME::getContentType($filename) !== 'image/gif')
{ {
@ -51,17 +51,17 @@ class Image
* Get image information * Get image information
* *
* @param string $filename * @param string $filename
* @return array|false * @return ?array
*/ */
public static function getImageInfo($filename) public static function getImageInfo(string $filename): ?array
{ {
if (!self::isImage($filename)) if (!self::isImage($filename))
{ {
return false; return null;
} }
if (!$image_info = @getimagesize($filename)) if (!$image_info = @getimagesize($filename))
{ {
return false; return null;
} }
$img_type = [ $img_type = [
IMAGETYPE_GIF => 'gif', IMAGETYPE_GIF => 'gif',

View file

@ -13,7 +13,7 @@ class Korea
* @param string $num * @param string $num
* @return string * @return string
*/ */
public static function formatPhoneNumber($num) public static function formatPhoneNumber(string $num): string
{ {
// Remove all non-numbers. // Remove all non-numbers.
$num = preg_replace('/[^0-9]/', '', $num); $num = preg_replace('/[^0-9]/', '', $num);
@ -69,7 +69,7 @@ class Korea
* @param string $num * @param string $num
* @return bool * @return bool
*/ */
public static function isValidPhoneNumber($num) public static function isValidPhoneNumber(string $num): bool
{ {
$num = str_replace('-', '', self::formatPhoneNumber($num)); $num = str_replace('-', '', self::formatPhoneNumber($num));
if (preg_match('/^1[0-9]{7}$/', $num)) if (preg_match('/^1[0-9]{7}$/', $num))
@ -97,7 +97,7 @@ class Korea
* @param string $num * @param string $num
* @return bool * @return bool
*/ */
public static function isValidMobilePhoneNumber($num) public static function isValidMobilePhoneNumber(string $num): bool
{ {
$num = str_replace('-', '', self::formatPhoneNumber($num)); $num = str_replace('-', '', self::formatPhoneNumber($num));
$len = strlen($num); $len = strlen($num);
@ -114,7 +114,7 @@ class Korea
* @param string $code * @param string $code
* @return bool * @return bool
*/ */
public static function isValidJuminNumber($code) public static function isValidJuminNumber(string $code): bool
{ {
// Return false if the format is obviously wrong. // Return false if the format is obviously wrong.
if (!preg_match('/^[0-9]{6}-?[0-9]{7}$/', $code)) if (!preg_match('/^[0-9]{6}-?[0-9]{7}$/', $code))
@ -164,7 +164,7 @@ class Korea
* @param string $code * @param string $code
* @return bool * @return bool
*/ */
public static function isValidCorporationNumber($code) public static function isValidCorporationNumber(string $code): bool
{ {
// Return false if the format is obviously wrong. // Return false if the format is obviously wrong.
if (!preg_match('/^[0-9]{6}-?[0-9]{7}$/', $code)) if (!preg_match('/^[0-9]{6}-?[0-9]{7}$/', $code))
@ -200,7 +200,7 @@ class Korea
* @param string $code * @param string $code
* @return bool * @return bool
*/ */
public static function isValidBusinessNumber($code) public static function isValidBusinessNumber(string $code): bool
{ {
// Return false if the format is obviously wrong. // Return false if the format is obviously wrong.
if (!preg_match('/^[0-9]{3}-?[0-9]{2}-?[0-9]{5}$/', $code)) if (!preg_match('/^[0-9]{3}-?[0-9]{2}-?[0-9]{5}$/', $code))
@ -232,7 +232,7 @@ class Korea
* @param string $ip * @param string $ip
* @return bool * @return bool
*/ */
public static function isKoreanIP($ip) public static function isKoreanIP(string $ip): bool
{ {
// Extract the IPv4 address from an "IPv4-mapped IPv6" address. // Extract the IPv4 address from an "IPv4-mapped IPv6" address.
if (preg_match('/::ffff:(?:0+:)?([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)$/', $ip, $matches)) $ip = $matches[1]; if (preg_match('/::ffff:(?:0+:)?([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)$/', $ip, $matches)) $ip = $matches[1];
@ -293,7 +293,7 @@ class Korea
* @param bool $clear_cache (optional) * @param bool $clear_cache (optional)
* @return bool * @return bool
*/ */
public static function isKoreanEmailAddress($email_address, $clear_cache = false) public static function isKoreanEmailAddress(string $email_address, bool $clear_cache = false): bool
{ {
// Clear the cache if requested. // Clear the cache if requested.
if ($clear_cache) if ($clear_cache)
@ -369,7 +369,7 @@ class Korea
* @param int $type * @param int $type
* @return array * @return array
*/ */
protected static function _getDNSRecords($domain, $type) protected static function _getDNSRecords(string $domain, int $type): array
{ {
$records = dns_get_record($domain, $type); $records = dns_get_record($domain, $type);
if (!$records) if (!$records)
@ -414,6 +414,7 @@ class Korea
'hanmail.net', 'hanmail.net',
'hanmail2.net', 'hanmail2.net',
'daum.net', 'daum.net',
'kakao.com',
'paran.com', 'paran.com',
'tistory.com', 'tistory.com',
'naver.com', 'naver.com',

View file

@ -24,9 +24,9 @@ class Lang
* This method returns the cached instance of a language. * This method returns the cached instance of a language.
* *
* @param string $language * @param string $language
* @return object * @return self
*/ */
public static function getInstance($language) public static function getInstance(string $language): self
{ {
if ($language === 'jp') if ($language === 'jp')
{ {
@ -44,7 +44,7 @@ class Lang
* *
* @param string $language * @param string $language
*/ */
protected function __construct($language) protected function __construct(string $language)
{ {
$this->_language = preg_replace('/[^a-z0-9_-]/i', '', $language); $this->_language = preg_replace('/[^a-z0-9_-]/i', '', $language);
$this->_loaded_plugins['_custom_'] = new \stdClass(); $this->_loaded_plugins['_custom_'] = new \stdClass();
@ -55,7 +55,7 @@ class Lang
* *
* @return string * @return string
*/ */
public function langType() public function langType(): string
{ {
return $this->_language; return $this->_language;
} }
@ -66,7 +66,7 @@ class Lang
* @param string $name * @param string $name
* @return bool * @return bool
*/ */
public function loadPlugin($name) public function loadPlugin(string $name): bool
{ {
if (isset($this->_loaded_plugins[$name])) if (isset($this->_loaded_plugins[$name]))
{ {
@ -89,6 +89,12 @@ class Lang
{ {
$this->loadDirectory(\RX_BASEDIR . "addons/$name/lang", $name); $this->loadDirectory(\RX_BASEDIR . "addons/$name/lang", $name);
} }
else
{
return false;
}
return true;
} }
/** /**
@ -98,7 +104,7 @@ class Lang
* @param string $plugin_name * @param string $plugin_name
* @return bool * @return bool
*/ */
public function loadDirectory($dir, $plugin_name = null) public function loadDirectory(string $dir, string $plugin_name = ''): bool
{ {
// Do not load the same directory twice. // Do not load the same directory twice.
$dir = rtrim($dir, '/'); $dir = rtrim($dir, '/');
@ -161,7 +167,7 @@ class Lang
* *
* @return array * @return array
*/ */
public static function getSupportedList() public static function getSupportedList(): array
{ {
static $list = null; static $list = null;
if ($list === null) if ($list === null)
@ -175,9 +181,9 @@ class Lang
* Generic getter. * Generic getter.
* *
* @param string $key * @param string $key
* @return string * @return string|\ArrayObject
*/ */
public function get($key) public function get(string $key)
{ {
$args = func_get_args(); $args = func_get_args();
array_shift($args); array_shift($args);
@ -200,10 +206,10 @@ class Lang
* Generic setter. * Generic setter.
* *
* @param string $key * @param string $key
* @param string $value * @param mixed $value
* @return void * @return void
*/ */
public function set($key, $value) public function set(string $key, $value): void
{ {
$this->__set($key, $value); $this->__set($key, $value);
} }
@ -212,9 +218,9 @@ class Lang
* Fallback method for getting the default translation. * Fallback method for getting the default translation.
* *
* @param string $key * @param string $key
* @return string * @return string|\ArrayObject
*/ */
public function getFromDefaultLang($key) public function getFromDefaultLang(string $key)
{ {
if ($this->_language === 'en') if ($this->_language === 'en')
{ {
@ -230,9 +236,9 @@ class Lang
* Magic method for translations without arguments. * Magic method for translations without arguments.
* *
* @param string $key * @param string $key
* @return string * @return string|\ArrayObject
*/ */
public function __get($key) public function __get(string $key)
{ {
// Load a dot-separated key (prefixed by plugin name). // Load a dot-separated key (prefixed by plugin name).
if (preg_match('/^[a-z0-9_.-]+$/i', $key) && ($keys = explode('.', $key)) && count($keys) >= 2) if (preg_match('/^[a-z0-9_.-]+$/i', $key) && ($keys = explode('.', $key)) && count($keys) >= 2)
@ -293,10 +299,10 @@ class Lang
* Magic method for setting a new custom translation. * Magic method for setting a new custom translation.
* *
* @param string $key * @param string $key
* @param string $value * @param mixed $value
* @return void * @return void
*/ */
public function __set($key, $value) public function __set(string $key, $value): void
{ {
// Set a dot-separated key (prefixed by plugin name). // Set a dot-separated key (prefixed by plugin name).
if (preg_match('/^[a-z0-9_.-]+$/i', $key) && ($keys = explode('.', $key)) && count($keys) >= 2) if (preg_match('/^[a-z0-9_.-]+$/i', $key) && ($keys = explode('.', $key)) && count($keys) >= 2)
@ -309,7 +315,7 @@ class Lang
} }
if (!isset($this->_loaded_plugins[$plugin_name])) if (!isset($this->_loaded_plugins[$plugin_name]))
{ {
return false; return;
} }
// Set the given key. // Set the given key.
@ -330,7 +336,7 @@ class Lang
} }
else else
{ {
return false; return;
} }
} }
elseif (is_array($lang) && isset($lang[$subkey])) elseif (is_array($lang) && isset($lang[$subkey]))
@ -346,7 +352,7 @@ class Lang
} }
else else
{ {
return false; return;
} }
} }
else else
@ -374,7 +380,7 @@ class Lang
* @param string $key * @param string $key
* @return bool * @return bool
*/ */
public function __isset($key) public function __isset(string $key): bool
{ {
foreach ($this->_loaded_plugins as $plugin_name => $translations) foreach ($this->_loaded_plugins as $plugin_name => $translations)
{ {
@ -392,7 +398,7 @@ class Lang
* @param string $key * @param string $key
* @return void * @return void
*/ */
public function __unset($key) public function __unset(string $key): void
{ {
$this->set($key, null); $this->set($key, null);
} }
@ -402,9 +408,9 @@ class Lang
* *
* @param string $key * @param string $key
* @param mixed $args * @param mixed $args
* @return string|null * @return mixed
*/ */
public function __call($key, $args = array()) public function __call(string $key, $args = array())
{ {
return $this->get($key, $args); return $this->get($key, $args);
} }

View file

@ -29,7 +29,7 @@ class i18n
* @param int $sort_by * @param int $sort_by
* @return array * @return array
*/ */
public static function listCountries($sort_by = self::SORT_NAME_ENGLISH) public static function listCountries(int $sort_by = self::SORT_NAME_ENGLISH): array
{ {
if (isset(self::$_countries[$sort_by])) if (isset(self::$_countries[$sort_by]))
{ {
@ -92,10 +92,10 @@ class i18n
* *
* This function returns null if a matching country is not found. * This function returns null if a matching country is not found.
* *
* @param $code Country code * @param string $code Country code
* @return string|null * @return ?string
*/ */
public static function getCallingCodeByCountryCode($code) public static function getCallingCodeByCountryCode(string $code): ?string
{ {
$countries = self::listCountries(); $countries = self::listCountries();
if (strlen($code) === 3) if (strlen($code) === 3)
@ -122,10 +122,11 @@ class i18n
* This function may return the wrong country if two or more countries share a calling code. * This function may return the wrong country if two or more countries share a calling code.
* This function returns null if a matching country is not found. * This function returns null if a matching country is not found.
* *
* @param $code Calling code * @param string $code Calling code
* @return string|null * @param int $type (2 or 3)
* @return ?string
*/ */
public static function getCountryCodeByCallingCode($code, $type = 3) public static function getCountryCodeByCallingCode(string $code, $type = 3): ?string
{ {
$countries = self::listCountries(); $countries = self::listCountries();
$code = preg_replace('/[^0-9]/', '', $code); $code = preg_replace('/[^0-9]/', '', $code);
@ -133,12 +134,13 @@ class i18n
{ {
if (preg_replace('/[^0-9]/', '', $country->calling_code) === $code) if (preg_replace('/[^0-9]/', '', $country->calling_code) === $code)
{ {
return $type == 3 ? $country->iso_3166_1_alpha3 : $country->iso_3166_1_alpha2; return strval($type == 3 ? $country->iso_3166_1_alpha3 : $country->iso_3166_1_alpha2);
} }
} }
return null; return null;
} }
/** /**
* Format a phone number with country code. * Format a phone number with country code.
* *
@ -147,7 +149,7 @@ class i18n
* @param bool $pretty (optional) * @param bool $pretty (optional)
* @return string * @return string
*/ */
public static function formatPhoneNumber($phone_number, $phone_country, $pretty = true) public static function formatPhoneNumber(string $phone_number, string $phone_country, bool $pretty = true): string
{ {
if (!is_numeric($phone_country)) if (!is_numeric($phone_country))
{ {
@ -156,11 +158,11 @@ class i18n
if ($pretty) if ($pretty)
{ {
if ($phone_country == 82) if ($phone_country === '82')
{ {
$pretty_phone_number = Korea::formatPhoneNumber($phone_number); $pretty_phone_number = Korea::formatPhoneNumber($phone_number);
} }
elseif ($phone_country == 1) elseif ($phone_country === '1')
{ {
$digits = preg_replace('/[^0-9]/', '', $phone_number); $digits = preg_replace('/[^0-9]/', '', $phone_number);
$pretty_phone_number = substr($digits, 0, 3) . '-' . substr($digits, 3, 3) . '-' . substr($digits, 6); $pretty_phone_number = substr($digits, 0, 3) . '-' . substr($digits, 3, 3) . '-' . substr($digits, 6);
@ -173,7 +175,7 @@ class i18n
} }
else else
{ {
if (!in_array(strval($phone_country), array('39', '378', '379'))) if (!in_array($phone_country, array('39', '378', '379')))
{ {
$phone_number = preg_replace('/^0/', '', $phone_number); $phone_number = preg_replace('/^0/', '', $phone_number);
} }

View file

@ -14,7 +14,7 @@ abstract class BaseParser
* @param bool $normalize * @param bool $normalize
* @return array * @return array
*/ */
protected static function _getAttributes(\SimpleXMLElement $element, $normalize = true): array protected static function _getAttributes(\SimpleXMLElement $element, bool $normalize = true): array
{ {
$result = array(); $result = array();
foreach ($element->attributes() as $key => $val) foreach ($element->attributes() as $key => $val)

View file

@ -17,13 +17,13 @@ class ConfigParser
* *
* @return array * @return array
*/ */
public static function convert() public static function convert(): array
{ {
// Load DB info file. // Load DB info file.
if (file_exists(\RX_BASEDIR . Config::$old_db_config_filename)) if (file_exists(\RX_BASEDIR . Config::OLD_DB_CONFIG_PATH))
{ {
ob_start(); ob_start();
include \RX_BASEDIR . Config::$old_db_config_filename; include \RX_BASEDIR . Config::OLD_DB_CONFIG_PATH;
ob_end_clean(); ob_end_clean();
} }
else else
@ -32,18 +32,18 @@ class ConfigParser
} }
// Load FTP info file. // Load FTP info file.
if (file_exists(\RX_BASEDIR . Config::$old_ftp_config_filename)) if (file_exists(\RX_BASEDIR . Config::OLD_FTP_CONFIG_PATH))
{ {
ob_start(); ob_start();
include \RX_BASEDIR . Config::$old_ftp_config_filename; include \RX_BASEDIR . Config::OLD_FTP_CONFIG_PATH;
ob_end_clean(); ob_end_clean();
} }
// Load selected language file. // Load selected language file.
if (file_exists(\RX_BASEDIR . Config::$old_lang_config_filename)) if (file_exists(\RX_BASEDIR . Config::OLD_LANG_CONFIG_PATH))
{ {
$lang_selected = array(); $lang_selected = array();
$lang_selected_raw = file_get_contents(\RX_BASEDIR . Config::$old_lang_config_filename); $lang_selected_raw = file_get_contents(\RX_BASEDIR . Config::OLD_LANG_CONFIG_PATH);
$lang_selected_raw = array_map('trim', explode("\n", $lang_selected_raw)); $lang_selected_raw = array_map('trim', explode("\n", $lang_selected_raw));
foreach ($lang_selected_raw as $lang_selected_item) foreach ($lang_selected_raw as $lang_selected_item)
{ {
@ -64,7 +64,7 @@ class ConfigParser
} }
// Load defaults for the new configuration. // Load defaults for the new configuration.
$config = (include \RX_BASEDIR . Config::$default_config_filename); $config = (include \RX_BASEDIR . Config::DEFAULT_CONFIG_PATH);
// Convert database configuration. // Convert database configuration.
if (!isset($db_info->master_db)) if (!isset($db_info->master_db))

View file

@ -30,7 +30,7 @@ function config(string $key, $value = null)
* *
* @param string $code Lang variable name * @param string $code Lang variable name
* @param string $value `$code`s value * @param string $value `$code`s value
* @return mixed * @return string|null|\ArrayObject
*/ */
function lang(string $code, $value = null) function lang(string $code, $value = null)
{ {