Fix incorrect or missing type declarations in Cache and DB classes

This commit is contained in:
Kijin Sung 2023-09-26 11:09:55 +09:00
parent 23c5f66479
commit a1537bafe3
3 changed files with 46 additions and 44 deletions

View file

@ -72,7 +72,7 @@ class CacheHandler extends Handler
*/ */
public function get($key, $modified_time = 0) 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; return $value === null ? false : $value;
} }
@ -88,7 +88,7 @@ class CacheHandler extends Handler
*/ */
public function put($key, $obj, $valid_time = 0) 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) 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) 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) public function invalidateGroupKey($keyGroupName)
{ {
return Rhymix\Framework\Cache::clearGroup($keyGroupName); return Rhymix\Framework\Cache::clearGroup(strval($keyGroupName));
} }
} }

View file

@ -16,7 +16,7 @@ class Cache
/** /**
* The cache prefix. * The cache prefix.
*/ */
protected static $_prefix = null; protected static $_prefix = '';
/** /**
* The default TTL. * The default TTL.
@ -32,9 +32,9 @@ class Cache
* Initialize the cache system. * Initialize the cache system.
* *
* @param array $config * @param array $config
* @return void * @return Drivers\CacheInterface
*/ */
public static function init($config) public static function init($config): Drivers\CacheInterface
{ {
if (!is_array($config)) if (!is_array($config))
{ {
@ -90,7 +90,7 @@ class Cache
* *
* @return array * @return array
*/ */
public static function getSupportedDrivers() public static function getSupportedDrivers(): array
{ {
$result = array(); $result = array();
foreach (Storage::readDirectory(__DIR__ . '/drivers/cache', false) as $filename) foreach (Storage::readDirectory(__DIR__ . '/drivers/cache', false) as $filename)
@ -108,9 +108,9 @@ class Cache
/** /**
* Get the name of the currently enabled cache driver. * 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; return self::$_driver_name;
} }
@ -120,9 +120,9 @@ class Cache
* *
* @param string $name (optional) * @param string $name (optional)
* @param array $config (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) if ($name === null)
{ {
@ -145,9 +145,9 @@ class Cache
/** /**
* Get the automatically generated cache prefix for this installation of Rhymix. * 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; return self::$_prefix;
} }
@ -157,7 +157,7 @@ class Cache
* *
* @return int * @return int
*/ */
public static function getDefaultTTL() public static function getDefaultTTL(): int
{ {
return self::$_ttl; return self::$_ttl;
} }
@ -168,7 +168,7 @@ class Cache
* @param int $ttl * @param int $ttl
* @return void * @return void
*/ */
public static function setDefaultTTL($ttl) public static function setDefaultTTL(int $ttl): void
{ {
self::$_ttl = $ttl; self::$_ttl = $ttl;
} }
@ -181,7 +181,7 @@ class Cache
* @param string $key * @param string $key
* @return mixed * @return mixed
*/ */
public static function get($key) public static function get(string $key)
{ {
if (self::$_driver !== null) if (self::$_driver !== null)
{ {
@ -206,11 +206,10 @@ class Cache
* @param bool $force (optional) * @param bool $force (optional)
* @return bool * @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) if (self::$_driver !== null)
{ {
$ttl = intval($ttl);
if ($ttl >= (3600 * 24 * 30)) if ($ttl >= (3600 * 24 * 30))
{ {
$ttl = min(3600 * 24 * 30, max(0, $ttl - time())); $ttl = min(3600 * 24 * 30, max(0, $ttl - time()));

View file

@ -175,7 +175,7 @@ class DB
* @param array $driver_options * @param array $driver_options
* @return Helpers\DBStmtHelper * @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. // Add table prefixes to the query string.
$statement = $this->addPrefixes($statement); $statement = $this->addPrefixes($statement);
@ -207,7 +207,7 @@ class DB
* @param mixed ...$args * @param mixed ...$args
* @return Helpers\DBStmtHelper * @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 query parameters are given as a single array, unpack it.
if (count($args) === 1 && is_array($args[0])) if (count($args) === 1 && is_array($args[0]))
@ -242,13 +242,13 @@ class DB
* Execute an XML-defined query. * Execute an XML-defined query.
* *
* @param string $query_id * @param string $query_id
* @param array $args * @param array|object $args
* @param array $columns * @param array $columns
* @param string $result_type * @param string $result_type
* @param string $result_class * @param string $result_class
* @return Helpers\DBResultHelper * @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. // Validate the args.
if (is_object($args)) if (is_object($args))
@ -506,9 +506,9 @@ class DB
* @param int $last_index * @param int $last_index
* @param string $result_type * @param string $result_type
* @param string $result_class * @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)) if (!($stmt instanceof \PDOStatement))
{ {
@ -701,8 +701,10 @@ class DB
/** /**
* Get the next global sequence value. * 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'))); $this->_handle->exec(sprintf('INSERT INTO `%s` (seq) VALUES (NULL)', $this->addQuotes($this->_prefix . 'sequence')));
$sequence = $this->getInsertID(); $sequence = $this->getInsertID();
@ -717,7 +719,7 @@ class DB
} }
$this->clearError(); $this->clearError();
return $sequence; return (int)$sequence;
} }
/** /**
@ -942,16 +944,16 @@ class DB
* *
* @param string $table_name * @param string $table_name
* @param string $column_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. // 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))); $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); $column_info = $this->fetch($stmt);
if (!$column_info) if (!$column_info)
{ {
return false; return null;
} }
// Reorganize the type information. // Reorganize the type information.
@ -1054,7 +1056,7 @@ class DB
* @param string $query_string * @param string $query_string
* @return string * @return string
*/ */
public function addPrefixes($query_string): string public function addPrefixes(string $query_string): string
{ {
if (!$this->_prefix) if (!$this->_prefix)
{ {
@ -1084,7 +1086,7 @@ class DB
/** /**
* Escape a string according to current DB settings. * Escape a string according to current DB settings.
* *
* @param string $str * @param int|float|string $str
* @return string * @return string
*/ */
public function addQuotes($str): string public function addQuotes($str): string
@ -1095,7 +1097,7 @@ class DB
} }
else 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 * @return void
*/ */
public function clearError() public function clearError(): void
{ {
$this->_errno = 0; $this->_errno = 0;
$this->_errstr = 'success'; $this->_errstr = 'success';
@ -1218,7 +1220,7 @@ class DB
* @param array $log * @param array $log
* @return void * @return void
*/ */
public function setQueryLog(array $log) public function setQueryLog(array $log): void
{ {
Debug::addQuery($log); Debug::addQuery($log);
} }
@ -1229,7 +1231,7 @@ class DB
* @param float $elapsed_time * @param float $elapsed_time
* @return void * @return void
*/ */
public function addElapsedTime(float $elapsed_time) public function addElapsedTime(float $elapsed_time): void
{ {
$this->_query_time += $elapsed_time; $this->_query_time += $elapsed_time;
} }
@ -1258,8 +1260,9 @@ class DB
* Enable or disable debug comments. * Enable or disable debug comments.
* *
* @param bool $enabled * @param bool $enabled
* @return void
*/ */
public function setDebugComment(bool $enabled) public function setDebugComment(bool $enabled): void
{ {
$this->_debug_comment = $enabled; $this->_debug_comment = $enabled;
} }
@ -1270,7 +1273,7 @@ class DB
* @param string $key * @param string $key
* @return mixed * @return mixed
*/ */
public function __get($key) public function __get(string $key)
{ {
switch ($key) switch ($key)
{ {
@ -1296,7 +1299,7 @@ class DB
* @param string $query_string * @param string $query_string
* @return Helpers\DBStmtHelper * @return Helpers\DBStmtHelper
*/ */
public function _query($query_string) public function _query($query_string): Helpers\DBStmtHelper
{ {
if ($this->_debug_comment) if ($this->_debug_comment)
{ {
@ -1304,7 +1307,7 @@ class DB
} }
$this->_last_stmt = null; $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; return $this->_last_stmt;
} }
@ -1318,7 +1321,7 @@ class DB
* @param int $last_index * @param int $last_index
* @return mixed * @return mixed
*/ */
public function _fetch($stmt, $last_index = 0) public function _fetch($stmt, int $last_index = 0)
{ {
return $this->fetch($stmt, $last_index); return $this->fetch($stmt, $last_index);
} }
@ -1448,9 +1451,9 @@ class DB
* Methods related to table creation. * Methods related to table creation.
* *
* @deprecated * @deprecated
* @return void * @return bool
*/ */
public function createTableByXmlFile($filename) public function createTableByXmlFile($filename): bool
{ {
$output = $this->createTable($filename); $output = $this->createTable($filename);
return $output->toBool(); return $output->toBool();