From ab2235a2fc3ecc87ce9242320b78ca174830d0e2 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Sun, 18 Jan 2026 17:37:14 +0800 Subject: [PATCH] refactor: complete `QueryInterface` --- system/Database/Query.php | 63 ++------------------- system/Database/QueryInterface.php | 29 ++++------ user_guide_src/source/changelogs/v4.7.0.rst | 13 ++++- utils/phpstan-baseline/loader.neon | 2 +- utils/phpstan-baseline/method.notFound.neon | 7 +-- 5 files changed, 27 insertions(+), 87 deletions(-) diff --git a/system/Database/Query.php b/system/Database/Query.php index c74ba6a687ab..8040f54b666c 100644 --- a/system/Database/Query.php +++ b/system/Database/Query.php @@ -100,14 +100,7 @@ public function __construct(ConnectionInterface $db) $this->db = $db; } - /** - * Sets the raw query string to use for this statement. - * - * @param mixed $binds - * - * @return $this - */ - public function setQuery(string $sql, $binds = null, bool $setEscape = true) + public function setQuery(string $sql, mixed $binds = null, bool $setEscape = true): self { $this->originalQueryString = $sql; unset($this->swappedQueryString); @@ -153,10 +146,6 @@ public function setBinds(array $binds, bool $setEscape = true) return $this; } - /** - * Returns the final, processed query string after binding, etal - * has been performed. - */ public function getQuery(): string { if (empty($this->finalQueryString)) { @@ -166,14 +155,7 @@ public function getQuery(): string return $this->finalQueryString; } - /** - * Records the execution time of the statement using microtime(true) - * for it's start and end values. If no end value is present, will - * use the current time to determine total duration. - * - * @return $this - */ - public function setDuration(float $start, ?float $end = null) + public function setDuration(float $start, ?float $end = null): self { $this->startTime = $start; @@ -200,23 +182,12 @@ public function getStartTime(bool $returnRaw = false, int $decimals = 6) return number_format($this->startTime, $decimals); } - /** - * Returns the duration of this query during execution, or null if - * the query has not been executed yet. - * - * @param int $decimals The accuracy of the returned time. - */ public function getDuration(int $decimals = 6): string { return number_format(($this->endTime - $this->startTime), $decimals); } - /** - * Stores the error description that happened for this query. - * - * @return $this - */ - public function setError(int $code, string $error) + public function setError(int $code, string $error): self { $this->errorCode = $code; $this->errorString = $error; @@ -224,44 +195,27 @@ public function setError(int $code, string $error) return $this; } - /** - * Reports whether this statement created an error not. - */ public function hasError(): bool { return ! empty($this->errorString); } - /** - * Returns the error code created while executing this statement. - */ public function getErrorCode(): int { return $this->errorCode; } - /** - * Returns the error message created while executing this statement. - */ public function getErrorMessage(): string { return $this->errorString; } - /** - * Determines if the statement is a write-type query or not. - */ public function isWriteType(): bool { return $this->db->isWriteType($this->originalQueryString); } - /** - * Swaps out one table prefix for a new one. - * - * @return $this - */ - public function swapPrefix(string $orig, string $swap) + public function swapPrefix(string $orig, string $swap): self { $sql = $this->swappedQueryString ?? $this->originalQueryString; @@ -275,9 +229,6 @@ public function swapPrefix(string $orig, string $swap) return $this; } - /** - * Returns the original SQL that was passed into the system. - */ public function getOriginalQuery(): string { return $this->originalQueryString; @@ -315,9 +266,6 @@ protected function compileBinds() } } - /** - * Match bindings - */ protected function matchNamedBinds(string $sql, array $binds): string { $replacers = []; @@ -339,9 +287,6 @@ protected function matchNamedBinds(string $sql, array $binds): string return strtr($sql, $replacers); } - /** - * Match bindings - */ protected function matchSimpleBinds(string $sql, array $binds, int $bindCount, int $ml): string { if ($c = preg_match_all("/'[^']*'/", $sql, $matches) >= 1) { diff --git a/system/Database/QueryInterface.php b/system/Database/QueryInterface.php index 86eb01f4e169..841ff2cae871 100644 --- a/system/Database/QueryInterface.php +++ b/system/Database/QueryInterface.php @@ -14,8 +14,6 @@ namespace CodeIgniter\Database; /** - * Interface QueryInterface - * * Represents a single statement that can be executed against the database. * Statements are platform-specific and can handle binding of binds. */ @@ -23,29 +21,21 @@ interface QueryInterface { /** * Sets the raw query string to use for this statement. - * - * @param mixed $binds - * - * @return $this */ - public function setQuery(string $sql, $binds = null, bool $setEscape = true); + public function setQuery(string $sql, mixed $binds = null, bool $setEscape = true): self; /** * Returns the final, processed query string after binding, etal * has been performed. - * - * @return string */ - public function getQuery(); + public function getQuery(): string; /** * Records the execution time of the statement using microtime(true) * for it's start and end values. If no end value is present, will * use the current time to determine total duration. - * - * @return $this */ - public function setDuration(float $start, ?float $end = null); + public function setDuration(float $start, ?float $end = null): self; /** * Returns the duration of this query during execution, or null if @@ -57,10 +47,8 @@ public function getDuration(int $decimals = 6): string; /** * Stores the error description that happened for this query. - * - * @return $this */ - public function setError(int $code, string $error); + public function setError(int $code, string $error): self; /** * Reports whether this statement created an error not. @@ -84,8 +72,11 @@ public function isWriteType(): bool; /** * Swaps out one table prefix for a new one. - * - * @return $this */ - public function swapPrefix(string $orig, string $swap); + public function swapPrefix(string $orig, string $swap): self; + + /** + * Returns the original SQL that was passed into the system. + */ + public function getOriginalQuery(): string; } diff --git a/user_guide_src/source/changelogs/v4.7.0.rst b/user_guide_src/source/changelogs/v4.7.0.rst index d907b3caac45..e8124847efc2 100644 --- a/user_guide_src/source/changelogs/v4.7.0.rst +++ b/user_guide_src/source/changelogs/v4.7.0.rst @@ -177,8 +177,11 @@ To use a different encryption key permanently, pass a custom config when creatin Interface Changes ================= -- **Cache:** The ``CacheInterface`` now includes the ``deleteMatching()`` method. If you've implemented your own caching driver from scratch, you will need to provide an implementation for this method to ensure compatibility. -- **Images:** The ``ImageHandlerInterface`` now includes a new method: ``clearMetadata()``. If you've implemented your own handler from scratch, you will need to provide an implementation for this method to ensure compatibility. +**NOTE:** If you've implemented your own classes that implement these interfaces from scratch, you will need to update your implementations to include the new methods to ensure compatibility. + +- **Cache:** The ``CacheInterface`` now includes the ``deleteMatching()`` method. +- **Database:** The ``QueryInterface`` now includes the ``getOriginalQuery()`` method. +- **Images:** The ``ImageHandlerInterface`` now includes a new method: ``clearMetadata()``. Method Signature Changes ======================== @@ -204,6 +207,12 @@ Method Signature Changes - ``clean()`` - ``getCacheInfo()`` - ``getMetaData()`` +- Added native parameter and return types to ``CodeIgniter\Database\QueryInterface`` methods: + - ``setQuery(string $sql, mixed $binds = null, bool $setEscape = true): self`` + - ``getQuery(): string`` + - ``setDuration(float $start, ?float $end = null): self`` + - ``setError(int $code, string $error): self`` + - ``swapPrefix(string $orig, string $swap): self`` - Added native return types to ``CodeIgniter\Debug\Toolbar`` methods: - ``prepare(): void`` - ``respond(): void`` diff --git a/utils/phpstan-baseline/loader.neon b/utils/phpstan-baseline/loader.neon index b8c387c27d31..12a532cbb4fb 100644 --- a/utils/phpstan-baseline/loader.neon +++ b/utils/phpstan-baseline/loader.neon @@ -1,4 +1,4 @@ -# total 2166 errors +# total 2165 errors includes: - argument.type.neon diff --git a/utils/phpstan-baseline/method.notFound.neon b/utils/phpstan-baseline/method.notFound.neon index 1018f0fc8119..34144944c976 100644 --- a/utils/phpstan-baseline/method.notFound.neon +++ b/utils/phpstan-baseline/method.notFound.neon @@ -1,12 +1,7 @@ -# total 81 errors +# total 80 errors parameters: ignoreErrors: - - - message: '#^Call to an undefined method CodeIgniter\\Database\\QueryInterface\:\:getOriginalQuery\(\)\.$#' - count: 1 - path: ../../system/Database/BaseConnection.php - - message: '#^Call to an undefined method CodeIgniter\\View\\RendererInterface\:\:getData\(\)\.$#' count: 1