From d6ad1423b8d2ca5062f0ce7ae68390dc53225cf1 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Fri, 4 Sep 2026 22:23:28 +0800 Subject: [PATCH] refactor: fix minor type inaccuracies found via PHPStan bleeding edge --- system/Database/Database.php | 32 +++++++++++++--------------- system/HTTP/Files/FileCollection.php | 2 +- system/HTTP/SiteURIFactory.php | 2 +- system/Helpers/Array/ArrayHelper.php | 2 +- system/Router/RouteCollection.php | 2 +- 5 files changed, 19 insertions(+), 21 deletions(-) diff --git a/system/Database/Database.php b/system/Database/Database.php index 4cb5dfaf1627..a6f0e9cb73ef 100644 --- a/system/Database/Database.php +++ b/system/Database/Database.php @@ -58,7 +58,7 @@ public function load(array $params = [], string $alias = '') throw new InvalidArgumentException('You have not selected a database type to connect to.'); } - assert($this->checkDbExtension($params['DBDriver'])); + $this->checkDbExtension($params['DBDriver']); $this->connections[$alias] = $this->initDriver($params['DBDriver'], 'Connection', $params); @@ -154,15 +154,17 @@ protected function initDriver(string $driver, string $class, $argument): object } /** - * Check the PHP database extension is loaded. + * Check if the PHP database extension is loaded. * * @param string $driver DB driver or FQCN for custom driver + * + * @throws ConfigException if the driver is invalid + * @throws CriticalError if the required PHP extension is not loaded */ - private function checkDbExtension(string $driver): bool + private function checkDbExtension(string $driver): void { if (str_contains($driver, '\\')) { - // Cannot check a fully qualified classname for a custom driver. - return true; + return; // Cannot check a fully qualified classname for a custom driver. } $extensionMap = [ @@ -174,21 +176,17 @@ private function checkDbExtension(string $driver): bool 'OCI8' => 'oci8', ]; - $extension = $extensionMap[$driver] ?? ''; - - if ($extension === '') { - $message = 'Invalid DBDriver name: "' . $driver . '"'; - - throw new ConfigException($message); - } + $extension = $extensionMap[$driver] + ?? throw new ConfigException(sprintf('Invalid DBDriver name: "%s".', $driver)); if (extension_loaded($extension)) { - return true; + return; } - $message = 'The required PHP extension "' . $extension . '" is not loaded.' - . ' Install and enable it to use "' . $driver . '" driver.'; - - throw new CriticalError($message); + throw new CriticalError(sprintf( + 'The required PHP extension "%s" is not loaded. Install and enable it to use "%s" driver.', + $extension, + $driver, + )); } } diff --git a/system/HTTP/Files/FileCollection.php b/system/HTTP/Files/FileCollection.php index 058e86c3a4aa..c1c0582f6ff5 100644 --- a/system/HTTP/Files/FileCollection.php +++ b/system/HTTP/Files/FileCollection.php @@ -189,7 +189,7 @@ protected function createFileObject(array $array) return new UploadedFile( $array['tmp_name'] ?? null, - $array['name'] ?? null, + $array['name'], $array['type'] ?? null, ($array['size'] ?? null) === null ? null : (int) $array['size'], $array['error'] ?? null, diff --git a/system/HTTP/SiteURIFactory.php b/system/HTTP/SiteURIFactory.php index 73cc9fe84ee4..11dccce6c540 100644 --- a/system/HTTP/SiteURIFactory.php +++ b/system/HTTP/SiteURIFactory.php @@ -227,7 +227,7 @@ private function createURIFromRoutePath(string $routePath): SiteURI */ private function getHost(): ?string { - $httpHostPort = $this->superglobals->server('HTTP_HOST') ?? null; + $httpHostPort = $this->superglobals->server('HTTP_HOST'); if ($httpHostPort !== null) { [$httpHost] = explode(':', $httpHostPort, 2); diff --git a/system/Helpers/Array/ArrayHelper.php b/system/Helpers/Array/ArrayHelper.php index f70f6905ca5d..d370e1f1112f 100644 --- a/system/Helpers/Array/ArrayHelper.php +++ b/system/Helpers/Array/ArrayHelper.php @@ -324,7 +324,7 @@ public static function recursiveCount(array $array, int $counter = 0): int * @param list|string> $array * @param int|string|null $sortByIndex */ - public static function sortValuesByNatural(array &$array, $sortByIndex = null): bool + public static function sortValuesByNatural(array &$array, $sortByIndex = null): true { return usort($array, static function ($currentValue, $nextValue) use ($sortByIndex): int { if ($sortByIndex !== null) { diff --git a/system/Router/RouteCollection.php b/system/Router/RouteCollection.php index 6ade80e6f9fc..c08c01530b79 100644 --- a/system/Router/RouteCollection.php +++ b/system/Router/RouteCollection.php @@ -1520,7 +1520,7 @@ private function checkSubdomains($subdomains): bool // Routes can be limited to any sub-domain. In that case, though, // it does require a sub-domain to be present. - if (! in_array($this->currentSubdomain, [null, ''], true) && in_array('*', $subdomains, true)) { + if ($this->currentSubdomain !== '' && in_array('*', $subdomains, true)) { return true; }