From a1537bafe3a7a7fc626e27320d5900b7e4bb80d2 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Tue, 26 Sep 2023 11:09:55 +0900 Subject: [PATCH] Fix incorrect or missing type declarations in Cache and DB classes --- classes/cache/CacheHandler.class.php | 10 +++--- common/framework/Cache.php | 29 ++++++++-------- common/framework/DB.php | 51 +++++++++++++++------------- 3 files changed, 46 insertions(+), 44 deletions(-) diff --git a/classes/cache/CacheHandler.class.php b/classes/cache/CacheHandler.class.php index 08d972504..01c149c6a 100644 --- a/classes/cache/CacheHandler.class.php +++ b/classes/cache/CacheHandler.class.php @@ -72,7 +72,7 @@ class CacheHandler extends Handler */ public function get($key, $modified_time = 0) { - $value = Rhymix\Framework\Cache::get($key); + $value = Rhymix\Framework\Cache::get(strval($key)); return $value === null ? false : $value; } @@ -88,7 +88,7 @@ class CacheHandler extends Handler */ public function put($key, $obj, $valid_time = 0) { - return Rhymix\Framework\Cache::set($key, $obj, $valid_time, $this->_always_use_file); + return Rhymix\Framework\Cache::set(strval($key), $obj, intval($valid_time), $this->_always_use_file); } /** @@ -99,7 +99,7 @@ class CacheHandler extends Handler */ public function delete($key) { - return Rhymix\Framework\Cache::delete($key); + return Rhymix\Framework\Cache::delete(strval($key)); } /** @@ -112,7 +112,7 @@ class CacheHandler extends Handler */ public function isValid($key, $modified_time = 0) { - return Rhymix\Framework\Cache::exists($key); + return Rhymix\Framework\Cache::exists(strval($key)); } /** @@ -154,6 +154,6 @@ class CacheHandler extends Handler */ public function invalidateGroupKey($keyGroupName) { - return Rhymix\Framework\Cache::clearGroup($keyGroupName); + return Rhymix\Framework\Cache::clearGroup(strval($keyGroupName)); } } diff --git a/common/framework/Cache.php b/common/framework/Cache.php index 45d646ff5..f1e2dbe5b 100644 --- a/common/framework/Cache.php +++ b/common/framework/Cache.php @@ -16,7 +16,7 @@ class Cache /** * The cache prefix. */ - protected static $_prefix = null; + protected static $_prefix = ''; /** * The default TTL. @@ -32,9 +32,9 @@ class Cache * Initialize the cache system. * * @param array $config - * @return void + * @return Drivers\CacheInterface */ - public static function init($config) + public static function init($config): Drivers\CacheInterface { if (!is_array($config)) { @@ -90,7 +90,7 @@ class Cache * * @return array */ - public static function getSupportedDrivers() + public static function getSupportedDrivers(): array { $result = array(); foreach (Storage::readDirectory(__DIR__ . '/drivers/cache', false) as $filename) @@ -108,9 +108,9 @@ class Cache /** * Get the name of the currently enabled cache driver. * - * @return string|null + * @return ?string */ - public static function getDriverName() + public static function getDriverName(): ?string { return self::$_driver_name; } @@ -120,9 +120,9 @@ class Cache * * @param string $name (optional) * @param array $config (optional) - * @return object|null + * @return ?Drivers\CacheInterface */ - public static function getDriverInstance($name = null, array $config = []) + public static function getDriverInstance($name = null, array $config = []): ?Drivers\CacheInterface { if ($name === null) { @@ -145,9 +145,9 @@ class Cache /** * Get the automatically generated cache prefix for this installation of Rhymix. * - * @return object|null + * @return string */ - public static function getPrefix() + public static function getPrefix(): string { return self::$_prefix; } @@ -157,7 +157,7 @@ class Cache * * @return int */ - public static function getDefaultTTL() + public static function getDefaultTTL(): int { return self::$_ttl; } @@ -168,7 +168,7 @@ class Cache * @param int $ttl * @return void */ - public static function setDefaultTTL($ttl) + public static function setDefaultTTL(int $ttl): void { self::$_ttl = $ttl; } @@ -181,7 +181,7 @@ class Cache * @param string $key * @return mixed */ - public static function get($key) + public static function get(string $key) { if (self::$_driver !== null) { @@ -206,11 +206,10 @@ class Cache * @param bool $force (optional) * @return bool */ - public static function set($key, $value, $ttl = 0, $force = false) + public static function set(string $key, $value, int $ttl = 0, bool $force = false): bool { if (self::$_driver !== null) { - $ttl = intval($ttl); if ($ttl >= (3600 * 24 * 30)) { $ttl = min(3600 * 24 * 30, max(0, $ttl - time())); diff --git a/common/framework/DB.php b/common/framework/DB.php index 6fa818016..a76e0ead4 100644 --- a/common/framework/DB.php +++ b/common/framework/DB.php @@ -175,7 +175,7 @@ class DB * @param array $driver_options * @return Helpers\DBStmtHelper */ - public function prepare(string $statement, array $driver_options = []) + public function prepare(string $statement, array $driver_options = []): Helpers\DBStmtHelper { // Add table prefixes to the query string. $statement = $this->addPrefixes($statement); @@ -207,7 +207,7 @@ class DB * @param mixed ...$args * @return Helpers\DBStmtHelper */ - public function query(string $query_string, ...$args) + public function query(string $query_string, ...$args): Helpers\DBStmtHelper { // If query parameters are given as a single array, unpack it. if (count($args) === 1 && is_array($args[0])) @@ -242,13 +242,13 @@ class DB * Execute an XML-defined query. * * @param string $query_id - * @param array $args + * @param array|object $args * @param array $columns * @param string $result_type * @param string $result_class * @return Helpers\DBResultHelper */ - public function executeQuery(string $query_id, $args = [], $column_list = [], $result_type = 'auto', $result_class = ''): Helpers\DBResultHelper + public function executeQuery(string $query_id, $args = [], array $column_list = [], string $result_type = 'auto', string $result_class = ''): Helpers\DBResultHelper { // Validate the args. if (is_object($args)) @@ -506,9 +506,9 @@ class DB * @param int $last_index * @param string $result_type * @param string $result_class - * @return mixed + * @return array|object|null|\PDOStatement */ - public function fetch($stmt, $last_index = 0, $result_type = 'auto', $result_class = '') + public function fetch($stmt, int $last_index = 0, string $result_type = 'auto', string $result_class = '') { if (!($stmt instanceof \PDOStatement)) { @@ -701,8 +701,10 @@ class DB /** * Get the next global sequence value. + * + * @return int */ - public function getNextSequence() + public function getNextSequence(): int { $this->_handle->exec(sprintf('INSERT INTO `%s` (seq) VALUES (NULL)', $this->addQuotes($this->_prefix . 'sequence'))); $sequence = $this->getInsertID(); @@ -717,7 +719,7 @@ class DB } $this->clearError(); - return $sequence; + return (int)$sequence; } /** @@ -942,16 +944,16 @@ class DB * * @param string $table_name * @param string $column_name - * @return object + * @return ?object */ - public function getColumnInfo(string $table_name, string $column_name) + public function getColumnInfo(string $table_name, string $column_name): ?object { // If column information is not found, return false. $stmt = $this->_handle->query(sprintf("SHOW FIELDS FROM `%s` WHERE Field = '%s'", $this->addQuotes($this->_prefix . $table_name), $this->addQuotes($column_name))); $column_info = $this->fetch($stmt); if (!$column_info) { - return false; + return null; } // Reorganize the type information. @@ -1054,7 +1056,7 @@ class DB * @param string $query_string * @return string */ - public function addPrefixes($query_string): string + public function addPrefixes(string $query_string): string { if (!$this->_prefix) { @@ -1084,7 +1086,7 @@ class DB /** * Escape a string according to current DB settings. * - * @param string $str + * @param int|float|string $str * @return string */ public function addQuotes($str): string @@ -1095,7 +1097,7 @@ class DB } else { - return preg_replace("/^'(.*)'$/s", '$1', $this->_handle->quote($str)); + return preg_replace("/^'(.*)'$/s", '$1', $this->_handle->quote(strval($str))); } } @@ -1153,7 +1155,7 @@ class DB * * @return void */ - public function clearError() + public function clearError(): void { $this->_errno = 0; $this->_errstr = 'success'; @@ -1218,7 +1220,7 @@ class DB * @param array $log * @return void */ - public function setQueryLog(array $log) + public function setQueryLog(array $log): void { Debug::addQuery($log); } @@ -1229,7 +1231,7 @@ class DB * @param float $elapsed_time * @return void */ - public function addElapsedTime(float $elapsed_time) + public function addElapsedTime(float $elapsed_time): void { $this->_query_time += $elapsed_time; } @@ -1258,8 +1260,9 @@ class DB * Enable or disable debug comments. * * @param bool $enabled + * @return void */ - public function setDebugComment(bool $enabled) + public function setDebugComment(bool $enabled): void { $this->_debug_comment = $enabled; } @@ -1270,7 +1273,7 @@ class DB * @param string $key * @return mixed */ - public function __get($key) + public function __get(string $key) { switch ($key) { @@ -1296,7 +1299,7 @@ class DB * @param string $query_string * @return Helpers\DBStmtHelper */ - public function _query($query_string) + public function _query($query_string): Helpers\DBStmtHelper { if ($this->_debug_comment) { @@ -1304,7 +1307,7 @@ class DB } $this->_last_stmt = null; - $this->_last_stmt = $this->_handle->query($query_string); + $this->_last_stmt = $this->_handle->query(strval($query_string)); return $this->_last_stmt; } @@ -1318,7 +1321,7 @@ class DB * @param int $last_index * @return mixed */ - public function _fetch($stmt, $last_index = 0) + public function _fetch($stmt, int $last_index = 0) { return $this->fetch($stmt, $last_index); } @@ -1448,9 +1451,9 @@ class DB * Methods related to table creation. * * @deprecated - * @return void + * @return bool */ - public function createTableByXmlFile($filename) + public function createTableByXmlFile($filename): bool { $output = $this->createTable($filename); return $output->toBool();