From abfcba7f4d0336f39f7ac3bc737807000348d765 Mon Sep 17 00:00:00 2001
From: "John Paul E. Balandan, CPA" <paulbalandan@gmail.com>
Date: Sat, 22 Jun 2024 00:10:59 +0800
Subject: [PATCH] fix: return and param types of BaseConnection

---
 phpstan-baseline.php                   | 72 --------------------------
 system/Database/BaseConnection.php     | 15 ++++--
 system/Database/MySQLi/Connection.php  |  2 +
 system/Database/Postgre/Connection.php |  4 ++
 system/Database/SQLSRV/Connection.php  |  2 +
 system/Database/SQLite3/Connection.php |  7 ++-
 system/Test/Mock/MockConnection.php    |  9 +++-
 7 files changed, 32 insertions(+), 79 deletions(-)

diff --git a/phpstan-baseline.php b/phpstan-baseline.php
index 91cb95cc0a44..8b9cee6bf01a 100644
--- a/phpstan-baseline.php
+++ b/phpstan-baseline.php
@@ -2083,24 +2083,12 @@
 	'count' => 1,
 	'path' => __DIR__ . '/system/Database/BaseConnection.php',
 ];
-$ignoreErrors[] = [
-	// identifier: missingType.iterableValue
-	'message' => '#^Method CodeIgniter\\\\Database\\\\BaseConnection\\:\\:_fieldData\\(\\) return type has no value type specified in iterable type array\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/system/Database/BaseConnection.php',
-];
 $ignoreErrors[] = [
 	// identifier: missingType.iterableValue
 	'message' => '#^Method CodeIgniter\\\\Database\\\\BaseConnection\\:\\:callFunction\\(\\) has parameter \\$params with no value type specified in iterable type array\\.$#',
 	'count' => 1,
 	'path' => __DIR__ . '/system/Database/BaseConnection.php',
 ];
-$ignoreErrors[] = [
-	// identifier: missingType.return
-	'message' => '#^Method CodeIgniter\\\\Database\\\\BaseConnection\\:\\:close\\(\\) has no return type specified\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/system/Database/BaseConnection.php',
-];
 $ignoreErrors[] = [
 	// identifier: missingType.iterableValue
 	'message' => '#^Method CodeIgniter\\\\Database\\\\BaseConnection\\:\\:escape\\(\\) has parameter \\$str with no value type specified in iterable type array\\.$#',
@@ -2131,24 +2119,6 @@
 	'count' => 1,
 	'path' => __DIR__ . '/system/Database/BaseConnection.php',
 ];
-$ignoreErrors[] = [
-	// identifier: missingType.iterableValue
-	'message' => '#^Method CodeIgniter\\\\Database\\\\BaseConnection\\:\\:getFieldNames\\(\\) return type has no value type specified in iterable type array\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/system/Database/BaseConnection.php',
-];
-$ignoreErrors[] = [
-	// identifier: missingType.iterableValue
-	'message' => '#^Method CodeIgniter\\\\Database\\\\BaseConnection\\:\\:getForeignKeyData\\(\\) return type has no value type specified in iterable type array\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/system/Database/BaseConnection.php',
-];
-$ignoreErrors[] = [
-	// identifier: missingType.iterableValue
-	'message' => '#^Method CodeIgniter\\\\Database\\\\BaseConnection\\:\\:listTables\\(\\) return type has no value type specified in iterable type array\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/system/Database/BaseConnection.php',
-];
 $ignoreErrors[] = [
 	// identifier: missingType.callable
 	'message' => '#^Method CodeIgniter\\\\Database\\\\BaseConnection\\:\\:prepare\\(\\) has parameter \\$func with no signature specified for Closure\\.$#',
@@ -2191,12 +2161,6 @@
 	'count' => 1,
 	'path' => __DIR__ . '/system/Database/BaseConnection.php',
 ];
-$ignoreErrors[] = [
-	// identifier: missingType.return
-	'message' => '#^Method CodeIgniter\\\\Database\\\\BaseConnection\\:\\:transOff\\(\\) has no return type specified\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/system/Database/BaseConnection.php',
-];
 $ignoreErrors[] = [
 	// identifier: missingType.iterableValue
 	'message' => '#^Property CodeIgniter\\\\Database\\\\BaseConnection\\:\\:\\$aliasedTables type has no value type specified in iterable type array\\.$#',
@@ -3355,12 +3319,6 @@
 	'count' => 1,
 	'path' => __DIR__ . '/system/Database/Postgre/Connection.php',
 ];
-$ignoreErrors[] = [
-	// identifier: missingType.return
-	'message' => '#^Method CodeIgniter\\\\Database\\\\Postgre\\\\Connection\\:\\:convertDSN\\(\\) has no return type specified\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/system/Database/Postgre/Connection.php',
-];
 $ignoreErrors[] = [
 	// identifier: missingType.iterableValue
 	'message' => '#^Method CodeIgniter\\\\Database\\\\Postgre\\\\Connection\\:\\:escape\\(\\) has parameter \\$str with no value type specified in iterable type array\\.$#',
@@ -3967,12 +3925,6 @@
 	'count' => 1,
 	'path' => __DIR__ . '/system/Database/SQLite3/Connection.php',
 ];
-$ignoreErrors[] = [
-	// identifier: missingType.iterableValue
-	'message' => '#^Method CodeIgniter\\\\Database\\\\SQLite3\\\\Connection\\:\\:getFieldNames\\(\\) return type has no value type specified in iterable type array\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/system/Database/SQLite3/Connection.php',
-];
 $ignoreErrors[] = [
 	// identifier: booleanNot.exprNotBoolean
 	'message' => '#^Only booleans are allowed in a negated boolean, string given\\.$#',
@@ -9613,24 +9565,6 @@
 	'count' => 1,
 	'path' => __DIR__ . '/system/Test/Mock/MockConnection.php',
 ];
-$ignoreErrors[] = [
-	// identifier: missingType.iterableValue
-	'message' => '#^Method CodeIgniter\\\\Test\\\\Mock\\\\MockConnection\\:\\:_fieldData\\(\\) return type has no value type specified in iterable type array\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/system/Test/Mock/MockConnection.php',
-];
-$ignoreErrors[] = [
-	// identifier: missingType.return
-	'message' => '#^Method CodeIgniter\\\\Test\\\\Mock\\\\MockConnection\\:\\:shouldReturn\\(\\) has no return type specified\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/system/Test/Mock/MockConnection.php',
-];
-$ignoreErrors[] = [
-	// identifier: missingType.parameter
-	'message' => '#^Method CodeIgniter\\\\Test\\\\Mock\\\\MockConnection\\:\\:shouldReturn\\(\\) has parameter \\$return with no type specified\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/system/Test/Mock/MockConnection.php',
-];
 $ignoreErrors[] = [
 	// identifier: method.childReturnType
 	'message' => '#^Return type \\(array\\{code\\: int\\|string\\|null, message\\: string\\|null\\}\\) of method CodeIgniter\\\\Test\\\\Mock\\\\MockConnection\\:\\:error\\(\\) should be covariant with return type \\(array\\<string, int\\|string\\>\\) of method CodeIgniter\\\\Database\\\\ConnectionInterface\\<object\\|resource,object\\|resource\\>\\:\\:error\\(\\)$#',
@@ -9661,12 +9595,6 @@
 	'count' => 2,
 	'path' => __DIR__ . '/system/Test/Mock/MockConnection.php',
 ];
-$ignoreErrors[] = [
-	// identifier: method.childReturnType
-	'message' => '#^Return type \\(mixed\\) of method CodeIgniter\\\\Test\\\\Mock\\\\MockConnection\\:\\:setDatabase\\(\\) should be covariant with return type \\(bool\\) of method CodeIgniter\\\\Database\\\\ConnectionInterface\\<object\\|resource,object\\|resource\\>\\:\\:setDatabase\\(\\)$#',
-	'count' => 2,
-	'path' => __DIR__ . '/system/Test/Mock/MockConnection.php',
-];
 $ignoreErrors[] = [
 	// identifier: missingType.return
 	'message' => '#^Method CodeIgniter\\\\Test\\\\Mock\\\\MockEvents\\:\\:getEventsFile\\(\\) has no return type specified\\.$#',
diff --git a/system/Database/BaseConnection.php b/system/Database/BaseConnection.php
index 57c37d8a0a6c..6581dc7c5ac0 100644
--- a/system/Database/BaseConnection.php
+++ b/system/Database/BaseConnection.php
@@ -477,6 +477,8 @@ public function initialize()
 
     /**
      * Close the database connection.
+     *
+     * @return void
      */
     public function close()
     {
@@ -736,6 +738,8 @@ public function simpleQuery(string $sql)
      * Disable Transactions
      *
      * This permits transactions to be disabled at run-time.
+     *
+     * @return void
      */
     public function transOff()
     {
@@ -1454,7 +1458,7 @@ protected function getDriverFunctionPrefix(): string
     /**
      * Returns an array of table names
      *
-     * @return array|false
+     * @return false|list<string>
      *
      * @throws DatabaseException
      */
@@ -1481,6 +1485,7 @@ public function listTables(bool $constrainByPrefix = false)
         $query = $this->query($sql);
 
         foreach ($query->getResultArray() as $row) {
+            /** @var string $table */
             $table = $row['table_name'] ?? $row['TABLE_NAME'] ?? $row[array_key_first($row)];
 
             $this->dataCache['table_names'][] = $table;
@@ -1531,7 +1536,7 @@ public function tableExists(string $tableName, bool $cached = true): bool
     /**
      * Fetch Field Names
      *
-     * @return array|false
+     * @return false|list<string>
      *
      * @throws DatabaseException
      */
@@ -1608,7 +1613,7 @@ public function getIndexData(string $table)
     /**
      * Returns an object with foreign key data
      *
-     * @return array
+     * @return array<string, stdClass>
      */
     public function getForeignKeyData(string $table)
     {
@@ -1769,7 +1774,9 @@ abstract protected function _listColumns(string $table = '');
     /**
      * Platform-specific field data information.
      *
-     * @see    getFieldData()
+     * @see getFieldData()
+     *
+     * @return list<stdClass>
      */
     abstract protected function _fieldData(string $table): array;
 
diff --git a/system/Database/MySQLi/Connection.php b/system/Database/MySQLi/Connection.php
index b25f2e1af390..068dfba5b9e9 100644
--- a/system/Database/MySQLi/Connection.php
+++ b/system/Database/MySQLi/Connection.php
@@ -237,6 +237,8 @@ public function connect(bool $persistent = false)
     /**
      * Keep or establish the connection if no queries have been sent for
      * a length of time exceeding the server's idle timeout.
+     *
+     * @return void
      */
     public function reconnect()
     {
diff --git a/system/Database/Postgre/Connection.php b/system/Database/Postgre/Connection.php
index c22dc8605f56..d3af2380156e 100644
--- a/system/Database/Postgre/Connection.php
+++ b/system/Database/Postgre/Connection.php
@@ -104,6 +104,8 @@ public function connect(bool $persistent = false)
 
     /**
      * Converts the DSN with semicolon syntax.
+     *
+     * @return void
      */
     private function convertDSN()
     {
@@ -143,6 +145,8 @@ private function convertDSN()
     /**
      * Keep or establish the connection if no queries have been sent for
      * a length of time exceeding the server's idle timeout.
+     *
+     * @return void
      */
     public function reconnect()
     {
diff --git a/system/Database/SQLSRV/Connection.php b/system/Database/SQLSRV/Connection.php
index 0626a63c998b..7e008e27c451 100644
--- a/system/Database/SQLSRV/Connection.php
+++ b/system/Database/SQLSRV/Connection.php
@@ -164,6 +164,8 @@ public function getAllErrorMessages(): string
     /**
      * Keep or establish the connection if no queries have been sent for
      * a length of time exceeding the server's idle timeout.
+     *
+     * @return void
      */
     public function reconnect()
     {
diff --git a/system/Database/SQLite3/Connection.php b/system/Database/SQLite3/Connection.php
index 945434184f33..9945d41de11b 100644
--- a/system/Database/SQLite3/Connection.php
+++ b/system/Database/SQLite3/Connection.php
@@ -55,6 +55,9 @@ class Connection extends BaseConnection
      */
     protected $busyTimeout;
 
+    /**
+     * @return void
+     */
     public function initialize()
     {
         parent::initialize();
@@ -101,6 +104,8 @@ public function connect(bool $persistent = false)
     /**
      * Keep or establish the connection if no queries have been sent for
      * a length of time exceeding the server's idle timeout.
+     *
+     * @return void
      */
     public function reconnect()
     {
@@ -211,7 +216,7 @@ protected function _listColumns(string $table = ''): string
     }
 
     /**
-     * @return array|false
+     * @return false|list<string>
      *
      * @throws DatabaseException
      */
diff --git a/system/Test/Mock/MockConnection.php b/system/Test/Mock/MockConnection.php
index 020d6e3dd340..ed814240bad3 100644
--- a/system/Test/Mock/MockConnection.php
+++ b/system/Test/Mock/MockConnection.php
@@ -38,6 +38,11 @@ class MockConnection extends BaseConnection
     public $database;
     public $lastQuery;
 
+    /**
+     * @param mixed $return
+     *
+     * @return $this
+     */
     public function shouldReturn(string $method, $return)
     {
         $this->returnValues[$method] = $return;
@@ -127,13 +132,13 @@ public function reconnect(): bool
     /**
      * Select a specific database table to use.
      *
-     * @return mixed
+     * @return bool
      */
     public function setDatabase(string $databaseName)
     {
         $this->database = $databaseName;
 
-        return $this;
+        return true;
     }
 
     /**