From 506ec33f96a63a2ac7da823a9e4fd3dc2fa6947e Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Mon, 24 Aug 2026 01:32:10 +0800 Subject: [PATCH] refactor: fix phpstan errors in `Config`, `Database`, `Query`, `MigrationRunner`, and `SQLite3\Table` --- system/Database/Config.php | 14 +- system/Database/Database.php | 14 +- system/Database/MigrationRunner.php | 16 +- system/Database/Query.php | 10 +- system/Database/SQLite3/Table.php | 14 +- .../Database/Live/SQLite3/AlterTableTest.php | 11 +- .../Live/SQLite3/GetIndexDataTest.php | 7 +- .../Migrations/MigrationRunnerTest.php | 7 + .../phpstan-baseline/assign.propertyType.neon | 12 +- utils/phpstan-baseline/loader.neon | 2 +- .../missingType.iterableValue.neon | 142 +----------------- .../missingType.parameter.neon | 7 +- utils/phpstan-baseline/property.notFound.neon | 7 +- 13 files changed, 75 insertions(+), 188 deletions(-) diff --git a/system/Database/Config.php b/system/Database/Config.php index 413d0b3b5de8..790c2fb63b12 100644 --- a/system/Database/Config.php +++ b/system/Database/Config.php @@ -26,7 +26,7 @@ class Config extends BaseConfig * Cache for instance of any connections that * have been requested as a "shared" instance. * - * @var array + * @var array */ protected static $instances = []; @@ -41,9 +41,9 @@ class Config extends BaseConfig /** * Returns the database connection * - * @param array|BaseConnection|non-empty-string|null $group The name of the connection group to use, - * or an array of configuration settings. - * @param bool $getShared Whether to return a shared instance of the connection. + * @param array|BaseConnection|non-empty-string|null $group The name of the connection group to use, + * or an array of configuration settings. + * @param bool $getShared Whether to return a shared instance of the connection. * * @return BaseConnection */ @@ -90,6 +90,8 @@ public static function connect($group = null, bool $getShared = true) /** * Returns an array of all db connections currently made. + * + * @return array */ public static function getConnections(): array { @@ -100,7 +102,7 @@ public static function getConnections(): array * Loads and returns an instance of the Forge for the specified * database group, and loads the group if it hasn't been loaded yet. * - * @param array|ConnectionInterface|string|null $group + * @param array|ConnectionInterface|string|null $group * * @return Forge */ @@ -114,7 +116,7 @@ public static function forge($group = null) /** * Returns a new instance of the Database Utilities class. * - * @param array|string|null $group + * @param array|string|null $group * * @return BaseUtils */ diff --git a/system/Database/Database.php b/system/Database/Database.php index 2955ce0d37c5..4cb5dfaf1627 100644 --- a/system/Database/Database.php +++ b/system/Database/Database.php @@ -31,13 +31,15 @@ class Database * Helps to keep track of all open connections for performance * monitoring, logging, etc. * - * @var array + * @var array */ protected $connections = []; /** * Parses the connection binds and creates a Database Connection instance. * + * @param array $params + * * @return BaseConnection * * @throws InvalidArgumentException @@ -94,6 +96,10 @@ public function loadUtils(ConnectionInterface $db): BaseUtils /** * Parses universal DSN string * + * @param array $params + * + * @return array + * * @throws InvalidArgumentException */ protected function parseDSN(array $params): array @@ -132,9 +138,9 @@ protected function parseDSN(array $params): array /** * Creates a database object. * - * @param string $driver Driver name. FQCN can be used. - * @param string $class 'Connection'|'Forge'|'Utils' - * @param array|ConnectionInterface $argument The constructor parameter or DB connection + * @param string $driver Driver name. FQCN can be used. + * @param string $class 'Connection'|'Forge'|'Utils' + * @param array|ConnectionInterface $argument The constructor parameter or DB connection * * @return BaseConnection|BaseUtils|Forge */ diff --git a/system/Database/MigrationRunner.php b/system/Database/MigrationRunner.php index 809fd84a718d..0b3459ba076e 100644 --- a/system/Database/MigrationRunner.php +++ b/system/Database/MigrationRunner.php @@ -90,7 +90,7 @@ class MigrationRunner /** * used to return messages for CLI. * - * @var array + * @var list */ protected $cliMessages = []; @@ -139,7 +139,7 @@ class MigrationRunner * default DB group so that it creates the `migrations` table in the default * DB group. Therefore, passing $db is for testing purposes only. * - * @param array|ConnectionInterface|string|null $db DB group. For testing purposes only. + * @param array|ConnectionInterface|string|null $db DB group. For testing purposes only. * * @throws ConfigException */ @@ -440,7 +440,7 @@ public function force(string $path, string $namespace, ?string $group = null) /** * Retrieves list of available migration scripts * - * @return array List of all located migrations by their UID + * @return array List of all located migrations by their UID */ public function findMigrations(): array { @@ -465,6 +465,8 @@ public function findMigrations(): array /** * Retrieves a list of available migration scripts for one namespace + * + * @return list */ public function findNamespaceMigrations(string $namespace): array { @@ -610,6 +612,8 @@ public function getObjectUid($object): string /** * Retrieves messages formatted for CLI output + * + * @return list */ public function getCliMessages(): array { @@ -693,6 +697,8 @@ protected function removeHistory($history) /** * Grabs the full migration history from the database for a group + * + * @return list */ public function getHistory(string $group = 'default'): array { @@ -719,6 +725,8 @@ public function getHistory(string $group = 'default'): array * Returns the migration history for a single batch. * * @param string $order + * + * @return list */ public function getBatchHistory(int $batch, $order = 'asc'): array { @@ -734,6 +742,8 @@ public function getBatchHistory(int $batch, $order = 'asc'): array /** * Returns all the batches from the database history in order + * + * @return list */ public function getBatches(): array { diff --git a/system/Database/Query.php b/system/Database/Query.php index dbefaff6a09d..294f143e57e8 100644 --- a/system/Database/Query.php +++ b/system/Database/Query.php @@ -44,7 +44,7 @@ class Query implements QueryInterface, Stringable /** * The binds and their values used for binding. * - * @var array + * @var array */ protected $binds = []; @@ -129,6 +129,8 @@ public function setQuery(string $sql, mixed $binds = null, bool $setEscape = tru /** * Will store the variables to bind into the query later. * + * @param array $binds + * * @return $this */ public function setBinds(array $binds, bool $setEscape = true) @@ -266,6 +268,9 @@ protected function compileBinds() } } + /** + * @param array $binds + */ protected function matchNamedBinds(string $sql, array $binds): string { $replacers = []; @@ -287,6 +292,9 @@ protected function matchNamedBinds(string $sql, array $binds): string return strtr($sql, $replacers); } + /** + * @param array $binds + */ 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/SQLite3/Table.php b/system/Database/SQLite3/Table.php index 48e081ed906b..1884d8c3ed44 100644 --- a/system/Database/SQLite3/Table.php +++ b/system/Database/SQLite3/Table.php @@ -35,14 +35,14 @@ class Table /** * All of the unique/primary keys in the table. * - * @var array + * @var array, type: string}> */ protected $keys = []; /** * All of the foreign keys in the table. * - * @var array + * @var array */ protected $foreignKeys = []; @@ -230,6 +230,8 @@ public function dropForeignKey(string $foreignName) /** * Adds primary key + * + * @param array{fields?: list} $fields */ public function addPrimaryKey(array $fields): Table { @@ -254,6 +256,8 @@ public function addPrimaryKey(array $fields): Table /** * Add a foreign key * + * @param list, referenceTable: string, referenceField: list, onDelete: string, onUpdate: string, fkName: string}> $foreignKeys + * * @return $this */ public function addForeignKey(array $foreignKeys) @@ -374,9 +378,9 @@ protected function copyData() * Converts fields retrieved from the database to * the format needed for creating fields with Forge. * - * @param array|bool $fields + * @param bool|list $fields * - * @return ($fields is array ? array : mixed) + * @return ($fields is array ? array> : mixed) */ protected function formatFields($fields) { @@ -452,7 +456,7 @@ private function isNumericType(string $type): bool * * @param array $keys * - * @return array + * @return array, type: string}> */ protected function formatKeys($keys) { diff --git a/tests/system/Database/Live/SQLite3/AlterTableTest.php b/tests/system/Database/Live/SQLite3/AlterTableTest.php index 46806b9618fb..61bf7ad44f6b 100644 --- a/tests/system/Database/Live/SQLite3/AlterTableTest.php +++ b/tests/system/Database/Live/SQLite3/AlterTableTest.php @@ -14,6 +14,7 @@ namespace CodeIgniter\Database\Live\SQLite3; use CodeIgniter\Database\Exceptions\DataException; +use CodeIgniter\Database\SQLite3\Connection; use CodeIgniter\Database\SQLite3\Forge; use CodeIgniter\Database\SQLite3\Table; use CodeIgniter\Test\CIUnitTestCase; @@ -54,8 +55,14 @@ protected function setUp(): void 'database' => ':memory:', 'DBDebug' => true, ]; - $this->db = db_connect($config); - $this->forge = Database::forge($config); + $db = db_connect($config); + $this->assertInstanceOf(Connection::class, $db); + $this->db = $db; + + $forge = Database::forge($config); + $this->assertInstanceOf(Forge::class, $forge); + $this->forge = $forge; + $this->table = new Table($this->db, $this->forge); $this->dropTables(); diff --git a/tests/system/Database/Live/SQLite3/GetIndexDataTest.php b/tests/system/Database/Live/SQLite3/GetIndexDataTest.php index ac5bd5a299d8..854d247daf22 100644 --- a/tests/system/Database/Live/SQLite3/GetIndexDataTest.php +++ b/tests/system/Database/Live/SQLite3/GetIndexDataTest.php @@ -41,8 +41,11 @@ protected function setUp(): void 'database' => 'database.db', 'DBDebug' => true, ]; - $this->db = db_connect($config, false); - $this->forge = Database::forge($config); + $this->db = db_connect($config, false); + + $forge = Database::forge($config); + $this->assertInstanceOf(Forge::class, $forge); + $this->forge = $forge; } public function testGetIndexData(): void diff --git a/tests/system/Database/Migrations/MigrationRunnerTest.php b/tests/system/Database/Migrations/MigrationRunnerTest.php index 510c8169fa34..79ad1cf39330 100644 --- a/tests/system/Database/Migrations/MigrationRunnerTest.php +++ b/tests/system/Database/Migrations/MigrationRunnerTest.php @@ -14,7 +14,9 @@ namespace CodeIgniter\Database\Migrations; use CodeIgniter\Database\BaseConnection; +use CodeIgniter\Database\ConnectionInterface; use CodeIgniter\Database\MigrationRunner; +use CodeIgniter\Database\SQLSRV\Connection as SQLSRVConnection; use CodeIgniter\Events\Events; use CodeIgniter\Exceptions\ConfigException; use CodeIgniter\Test\CIUnitTestCase; @@ -121,6 +123,7 @@ public function testGetHistory(): void ]; if ($this->db->DBDriver === 'SQLSRV') { + $this->assertInstanceOf(SQLSRVConnection::class, $this->db); $this->db->simpleQuery('SET IDENTITY_INSERT ' . $this->db->escapeIdentifiers($this->db->schema) . '.' . $this->db->prefixTable('migrations') . ' ON'); } @@ -138,6 +141,7 @@ public function testGetHistory(): void $this->assertSame($expected, $history); if ($this->db->DBDriver === 'SQLSRV') { + $this->assertInstanceOf(SQLSRVConnection::class, $this->db); $this->db->simpleQuery('SET IDENTITY_INSERT ' . $this->db->escapeIdentifiers($this->db->schema) . '.' . $this->db->prefixTable('migrations') . ' OFF'); $db = $this->getPrivateProperty($runner, 'db'); $db->table('migrations')->delete(['id' => 4]); @@ -477,6 +481,9 @@ public function testMigrationUsesSameConnectionAsMigrationRunner(): void } } + /** + * @param array|ConnectionInterface|string|null $db + */ protected function resetTables($db = null): void { $forge = Database::forge($db); diff --git a/utils/phpstan-baseline/assign.propertyType.neon b/utils/phpstan-baseline/assign.propertyType.neon index 3fb69d14d267..735faa6341d8 100644 --- a/utils/phpstan-baseline/assign.propertyType.neon +++ b/utils/phpstan-baseline/assign.propertyType.neon @@ -1,4 +1,4 @@ -# total 24 errors +# total 22 errors parameters: ignoreErrors: @@ -17,16 +17,6 @@ parameters: count: 1 path: ../../tests/system/Commands/Utilities/Routes/FilterFinderTest.php - - - message: '#^Property CodeIgniter\\Database\\Live\\SQLite3\\AlterTableTest\:\:\$forge \(CodeIgniter\\Database\\SQLite3\\Forge\) does not accept CodeIgniter\\Database\\Forge\.$#' - count: 1 - path: ../../tests/system/Database/Live/SQLite3/AlterTableTest.php - - - - message: '#^Property CodeIgniter\\Database\\Live\\SQLite3\\GetIndexDataTest\:\:\$forge \(CodeIgniter\\Database\\SQLite3\\Forge\) does not accept CodeIgniter\\Database\\Forge\.$#' - count: 1 - path: ../../tests/system/Database/Live/SQLite3/GetIndexDataTest.php - - message: '#^Property CodeIgniter\\Filters\\CSRFTest\:\:\$response \(CodeIgniter\\HTTP\\Response\|null\) does not accept CodeIgniter\\HTTP\\ResponseInterface\.$#' count: 2 diff --git a/utils/phpstan-baseline/loader.neon b/utils/phpstan-baseline/loader.neon index e2eed4892221..947f86255805 100644 --- a/utils/phpstan-baseline/loader.neon +++ b/utils/phpstan-baseline/loader.neon @@ -1,4 +1,4 @@ -# total 572 errors +# total 539 errors includes: - argument.type.neon diff --git a/utils/phpstan-baseline/missingType.iterableValue.neon b/utils/phpstan-baseline/missingType.iterableValue.neon index f8c8995deaa0..51c988f3992a 100644 --- a/utils/phpstan-baseline/missingType.iterableValue.neon +++ b/utils/phpstan-baseline/missingType.iterableValue.neon @@ -1,4 +1,4 @@ -# total 415 errors +# total 387 errors parameters: ignoreErrors: @@ -317,146 +317,6 @@ parameters: count: 1 path: ../../system/DataConverter/DataConverter.php - - - message: '#^Method CodeIgniter\\Database\\Config\:\:connect\(\) has parameter \$group with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/Config.php - - - - message: '#^Method CodeIgniter\\Database\\Config\:\:forge\(\) has parameter \$group with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/Config.php - - - - message: '#^Method CodeIgniter\\Database\\Config\:\:getConnections\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/Config.php - - - - message: '#^Method CodeIgniter\\Database\\Config\:\:utils\(\) has parameter \$group with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/Config.php - - - - message: '#^Property CodeIgniter\\Database\\Config\:\:\$instances type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/Config.php - - - - message: '#^Method CodeIgniter\\Database\\Database\:\:initDriver\(\) has parameter \$argument with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/Database.php - - - - message: '#^Method CodeIgniter\\Database\\Database\:\:load\(\) has parameter \$params with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/Database.php - - - - message: '#^Method CodeIgniter\\Database\\Database\:\:parseDSN\(\) has parameter \$params with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/Database.php - - - - message: '#^Method CodeIgniter\\Database\\Database\:\:parseDSN\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/Database.php - - - - message: '#^Property CodeIgniter\\Database\\Database\:\:\$connections type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/Database.php - - - - message: '#^Method CodeIgniter\\Database\\MigrationRunner\:\:__construct\(\) has parameter \$db with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/MigrationRunner.php - - - - message: '#^Method CodeIgniter\\Database\\MigrationRunner\:\:findMigrations\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/MigrationRunner.php - - - - message: '#^Method CodeIgniter\\Database\\MigrationRunner\:\:findNamespaceMigrations\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/MigrationRunner.php - - - - message: '#^Method CodeIgniter\\Database\\MigrationRunner\:\:getBatchHistory\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/MigrationRunner.php - - - - message: '#^Method CodeIgniter\\Database\\MigrationRunner\:\:getBatches\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/MigrationRunner.php - - - - message: '#^Method CodeIgniter\\Database\\MigrationRunner\:\:getCliMessages\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/MigrationRunner.php - - - - message: '#^Method CodeIgniter\\Database\\MigrationRunner\:\:getHistory\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/MigrationRunner.php - - - - message: '#^Property CodeIgniter\\Database\\MigrationRunner\:\:\$cliMessages type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/MigrationRunner.php - - - - message: '#^Method CodeIgniter\\Database\\Query\:\:matchNamedBinds\(\) has parameter \$binds with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/Query.php - - - - message: '#^Method CodeIgniter\\Database\\Query\:\:matchSimpleBinds\(\) has parameter \$binds with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/Query.php - - - - message: '#^Method CodeIgniter\\Database\\Query\:\:setBinds\(\) has parameter \$binds with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/Query.php - - - - message: '#^Property CodeIgniter\\Database\\Query\:\:\$binds type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/Query.php - - - - message: '#^Method CodeIgniter\\Database\\SQLite3\\Table\:\:addForeignKey\(\) has parameter \$foreignKeys with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/SQLite3/Table.php - - - - message: '#^Method CodeIgniter\\Database\\SQLite3\\Table\:\:addPrimaryKey\(\) has parameter \$fields with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/SQLite3/Table.php - - - - message: '#^Method CodeIgniter\\Database\\SQLite3\\Table\:\:formatFields\(\) has parameter \$fields with no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/SQLite3/Table.php - - - - message: '#^Method CodeIgniter\\Database\\SQLite3\\Table\:\:formatFields\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/SQLite3/Table.php - - - - message: '#^Property CodeIgniter\\Database\\SQLite3\\Table\:\:\$foreignKeys type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/SQLite3/Table.php - - - - message: '#^Property CodeIgniter\\Database\\SQLite3\\Table\:\:\$keys type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Database/SQLite3/Table.php - - message: '#^Method CodeIgniter\\Debug\\BaseExceptionHandler\:\:collectVars\(\) return type has no value type specified in iterable type array\.$#' count: 1 diff --git a/utils/phpstan-baseline/missingType.parameter.neon b/utils/phpstan-baseline/missingType.parameter.neon index baa8330cffd4..54d2e4a72c64 100644 --- a/utils/phpstan-baseline/missingType.parameter.neon +++ b/utils/phpstan-baseline/missingType.parameter.neon @@ -1,4 +1,4 @@ -# total 13 errors +# total 12 errors parameters: ignoreErrors: @@ -27,11 +27,6 @@ parameters: count: 1 path: ../../tests/system/Config/FactoriesTest.php - - - message: '#^Method CodeIgniter\\Database\\Migrations\\MigrationRunnerTest\:\:resetTables\(\) has parameter \$db with no type specified\.$#' - count: 1 - path: ../../tests/system/Database/Migrations/MigrationRunnerTest.php - - message: '#^Method CodeIgniter\\Entity\\EntityTest\:\:getCastEntity\(\) has parameter \$data with no type specified\.$#' count: 1 diff --git a/utils/phpstan-baseline/property.notFound.neon b/utils/phpstan-baseline/property.notFound.neon index 13dd11a84ac9..f55cee7b3de4 100644 --- a/utils/phpstan-baseline/property.notFound.neon +++ b/utils/phpstan-baseline/property.notFound.neon @@ -1,12 +1,7 @@ -# total 21 errors +# total 19 errors parameters: ignoreErrors: - - - message: '#^Access to an undefined property CodeIgniter\\Database\\BaseConnection\:\:\$schema\.$#' - count: 2 - path: ../../tests/system/Database/Migrations/MigrationRunnerTest.php - - message: '#^Access to an undefined property CodeIgniter\\Encryption\\EncrypterInterface\:\:\$key\.$#' count: 1