From 19a4eb0dc4927e33824cb592f2dea0a040485dbc Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Fri, 28 Aug 2026 12:36:15 +0800 Subject: [PATCH] refactor: fix remaining phpstan errors in `Database` --- system/Database/BaseBuilder.php | 6 +- tests/system/Database/BaseConnectionTest.php | 10 +++ tests/system/Database/BaseQueryTest.php | 18 +++-- tests/system/Database/Builder/InsertTest.php | 9 ++- tests/system/Database/Builder/UpdateTest.php | 7 +- tests/system/Database/ConfigTest.php | 18 +++++ tests/system/Database/Live/MetadataTest.php | 6 +- .../Database/Live/SQLite3/AlterTableTest.php | 2 +- tests/system/Database/Live/UpdateTest.php | 6 +- utils/phpstan-baseline/argument.type.neon | 22 +----- utils/phpstan-baseline/loader.neon | 2 +- .../missingType.iterableValue.neon | 67 +------------------ .../phpstan-baseline/property.phpDocType.neon | 12 +--- 13 files changed, 60 insertions(+), 125 deletions(-) diff --git a/system/Database/BaseBuilder.php b/system/Database/BaseBuilder.php index 5b03b0510b5f..1d2055d9ca24 100644 --- a/system/Database/BaseBuilder.php +++ b/system/Database/BaseBuilder.php @@ -2072,9 +2072,9 @@ private function setAlias(string $alias): BaseBuilder /** * Sets update fields for upsert, update * - * @param list|list|string $set - * @param bool $addToDefault adds update fields to the default ones - * @param list|null $ignore ignores items in set + * @param array|string $set + * @param bool $addToDefault Adds update fields to the default ones + * @param list|null $ignore Ignores items in set * * @return $this */ diff --git a/tests/system/Database/BaseConnectionTest.php b/tests/system/Database/BaseConnectionTest.php index 4d6d1f76173d..1b3a9841f58e 100644 --- a/tests/system/Database/BaseConnectionTest.php +++ b/tests/system/Database/BaseConnectionTest.php @@ -27,6 +27,9 @@ #[Group('Others')] final class BaseConnectionTest extends CIUnitTestCase { + /** + * @var array + */ private array $options = [ 'DSN' => '', 'hostname' => 'localhost', @@ -50,6 +53,10 @@ final class BaseConnectionTest extends CIUnitTestCase 'time' => 'H:i:s', ], ]; + + /** + * @var array + */ private array $failoverOptions = [ 'DSN' => '', 'hostname' => 'localhost', @@ -337,6 +344,9 @@ public function testProtectIdentifiers( $this->assertSame($expected, $return); } + /** + * @return iterable + */ public static function provideProtectIdentifiers(): iterable { yield from [ diff --git a/tests/system/Database/BaseQueryTest.php b/tests/system/Database/BaseQueryTest.php index c5265b85dfa7..b50e41b7ece8 100644 --- a/tests/system/Database/BaseQueryTest.php +++ b/tests/system/Database/BaseQueryTest.php @@ -107,12 +107,8 @@ public function testSwapPrefix(): void $this->assertSame($newSQL, $query->getQuery()); } - /** - * @param mixed $expected - * @param mixed $sql - */ #[DataProvider('provideIsWriteType')] - public function testIsWriteType($expected, $sql): void + public function testIsWriteType(bool $expected, string $sql): void { $query = new Query($this->db); @@ -120,6 +116,9 @@ public function testIsWriteType($expected, $sql): void $this->assertSame($expected, $query->isWriteType()); } + /** + * @return iterable + */ public static function provideIsWriteType(): iterable { return [ @@ -579,12 +578,8 @@ public function testSwapPrefixAfterGetQuery(): void $this->assertSame($expected, $query->getQuery()); } - /** - * @param mixed $expected - * @param mixed $sql - */ #[DataProvider('provideHighlightQueryKeywords')] - public function testHighlightQueryKeywords($expected, $sql): void + public function testHighlightQueryKeywords(string $expected, string $sql): void { $query = new Query($this->db); $query->setQuery($sql); @@ -592,6 +587,9 @@ public function testHighlightQueryKeywords($expected, $sql): void $this->assertSame($expected, $query->debugToolbarDisplay()); } + /** + * @return iterable + */ public static function provideHighlightQueryKeywords(): iterable { return [ diff --git a/tests/system/Database/Builder/InsertTest.php b/tests/system/Database/Builder/InsertTest.php index e8761b9de52b..062cf9786b34 100644 --- a/tests/system/Database/Builder/InsertTest.php +++ b/tests/system/Database/Builder/InsertTest.php @@ -27,11 +27,6 @@ #[Group('Others')] final class InsertTest extends CIUnitTestCase { - /** - * @var MockConnection - */ - protected $db; - protected function setUp(): void { parent::setUp(); @@ -162,6 +157,7 @@ public function testInsertBatch(): void ], ]; + $this->assertInstanceOf(MockConnection::class, $this->db); $this->db->shouldReturn('execute', new class () {}); $builder->insertBatch($insertData, true); @@ -197,6 +193,7 @@ public function testInsertBatchIgnore(): void ], ]; + $this->assertInstanceOf(MockConnection::class, $this->db); $this->db->shouldReturn('execute', new class () {}); $builder->ignore()->insertBatch($insertData, true, 1); @@ -231,6 +228,7 @@ public function testInsertBatchWithoutEscape(): void ], ]; + $this->assertInstanceOf(MockConnection::class, $this->db); $this->db->shouldReturn('execute', new class () {}); $builder->insertBatch($insertData, false); @@ -255,6 +253,7 @@ public function testInsertBatchWithFieldsEndingInNumbers(): void ['ip' => '4.4.4.0', 'ip2' => '4.4.4.2'], ]; + $this->assertInstanceOf(MockConnection::class, $this->db); $this->db->shouldReturn('execute', new class () {}); $builder->insertBatch($data, true); diff --git a/tests/system/Database/Builder/UpdateTest.php b/tests/system/Database/Builder/UpdateTest.php index 5bb93ad2a598..5a4ccd0be9d3 100644 --- a/tests/system/Database/Builder/UpdateTest.php +++ b/tests/system/Database/Builder/UpdateTest.php @@ -26,11 +26,6 @@ #[Group('Others')] final class UpdateTest extends CIUnitTestCase { - /** - * @var MockConnection - */ - protected $db; - protected function setUp(): void { parent::setUp(); @@ -229,6 +224,7 @@ public function testUpdateBatch(): void ], ]; + $this->assertInstanceOf(MockConnection::class, $this->db); $this->db->shouldReturn('execute', new class () {}); $builder->updateBatch($updateData, 'id'); @@ -268,6 +264,7 @@ public function testSetUpdateBatchWithoutEscape(): void ], ], 'id', $escape); + $this->assertInstanceOf(MockConnection::class, $this->db); $this->db->shouldReturn('execute', new class () {}); $builder->updateBatch(null, 'id'); diff --git a/tests/system/Database/ConfigTest.php b/tests/system/Database/ConfigTest.php index 1d2482e0ad87..980404ea9f3d 100644 --- a/tests/system/Database/ConfigTest.php +++ b/tests/system/Database/ConfigTest.php @@ -27,6 +27,9 @@ final class ConfigTest extends CIUnitTestCase { use ReflectionHelper; + /** + * @var array + */ private array $group = [ 'DSN' => '', 'hostname' => 'localhost', @@ -46,6 +49,10 @@ final class ConfigTest extends CIUnitTestCase 'failover' => [], 'port' => 3306, ]; + + /** + * @var array + */ private array $dsnGroup = [ 'DSN' => 'MySQLi://user:pass@localhost:3306/dbname?DBPrefix=test_&pConnect=true&charset=latin1&DBCollat=latin1_swedish_ci', 'hostname' => '', @@ -65,6 +72,10 @@ final class ConfigTest extends CIUnitTestCase 'failover' => [], 'port' => 3306, ]; + + /** + * @var array + */ private array $dsnGroupPostgre = [ 'DSN' => 'Postgre://user:pass@localhost:5432/dbname?DBPrefix=test_&connect_timeout=5&sslmode=1', 'hostname' => '', @@ -84,6 +95,10 @@ final class ConfigTest extends CIUnitTestCase 'failover' => [], 'port' => 5432, ]; + + /** + * @var array + */ private array $dsnGroupPostgreNative = [ 'DSN' => 'pgsql:host=localhost;port=5432;dbname=database_name', 'hostname' => '', @@ -214,6 +229,9 @@ public function testConvertDSN(string $input, string $expected): void $this->assertSame($expected, $this->getPrivateProperty($conn, 'DSN')); } + /** + * @return iterable + */ public static function provideConvertDSN(): iterable { yield from [ diff --git a/tests/system/Database/Live/MetadataTest.php b/tests/system/Database/Live/MetadataTest.php index 5030a6544231..62f96f443acc 100644 --- a/tests/system/Database/Live/MetadataTest.php +++ b/tests/system/Database/Live/MetadataTest.php @@ -27,7 +27,11 @@ final class MetadataTest extends CIUnitTestCase { use DatabaseTestTrait; - protected $seed = CITestSeeder::class; + protected $seed = CITestSeeder::class; + + /** + * @var list + */ private array $expectedTables = []; protected function setUp(): void diff --git a/tests/system/Database/Live/SQLite3/AlterTableTest.php b/tests/system/Database/Live/SQLite3/AlterTableTest.php index 61bf7ad44f6b..a68d42ca1440 100644 --- a/tests/system/Database/Live/SQLite3/AlterTableTest.php +++ b/tests/system/Database/Live/SQLite3/AlterTableTest.php @@ -63,7 +63,7 @@ protected function setUp(): void $this->assertInstanceOf(Forge::class, $forge); $this->forge = $forge; - $this->table = new Table($this->db, $this->forge); + $this->table = new Table($db, $this->forge); $this->dropTables(); } diff --git a/tests/system/Database/Live/UpdateTest.php b/tests/system/Database/Live/UpdateTest.php index 7f9ce90944ae..8961e7b05664 100644 --- a/tests/system/Database/Live/UpdateTest.php +++ b/tests/system/Database/Live/UpdateTest.php @@ -115,7 +115,8 @@ public function testUpdateWithWhereAndLimit(): void } /** - * @param array $expected + * @param list> $data + * @param list> $expected */ #[DataProvider('provideUpdateBatch')] public function testUpdateBatch(string $constraints, array $data, array $expected): void @@ -155,6 +156,9 @@ public function testUpdateBatch(string $constraints, array $data, array $expecte $this->seeInDatabase($table, $expected[1]); } + /** + * @return iterable>, list>}> + */ public static function provideUpdateBatch(): iterable { yield from [ diff --git a/utils/phpstan-baseline/argument.type.neon b/utils/phpstan-baseline/argument.type.neon index f5e14ec6a08f..b9f5906a8ac3 100644 --- a/utils/phpstan-baseline/argument.type.neon +++ b/utils/phpstan-baseline/argument.type.neon @@ -1,4 +1,4 @@ -# total 12 errors +# total 6 errors parameters: ignoreErrors: @@ -22,26 +22,6 @@ parameters: count: 1 path: ../../tests/system/CodeIgniterTest.php - - - message: '#^Parameter \#1 \$db of class CodeIgniter\\Database\\SQLite3\\Table constructor expects CodeIgniter\\Database\\SQLite3\\Connection, CodeIgniter\\Database\\BaseConnection given\.$#' - count: 1 - path: ../../tests/system/Database/Live/SQLite3/AlterTableTest.php - - - - message: '#^Parameter \#1 \$set of method CodeIgniter\\Database\\BaseBuilder\\:\:updateFields\(\) expects list\\|string, array\{0\: ''country'', updated_at\: CodeIgniter\\Database\\RawSql\} given\.$#' - count: 2 - path: ../../tests/system/Database/Live/UpdateTest.php - - - - message: '#^Parameter \#1 \$set of method CodeIgniter\\Database\\BaseBuilder\\:\:updateFields\(\) expects list\\|string, array\{0\: ''name'', updated_at\: CodeIgniter\\Database\\RawSql\} given\.$#' - count: 1 - path: ../../tests/system/Database/Live/UpdateTest.php - - - - message: '#^Parameter \#1 \$set of method CodeIgniter\\Database\\BaseBuilder\\:\:updateFields\(\) expects list\\|string, array\{updated_at\: CodeIgniter\\Database\\RawSql\} given\.$#' - count: 2 - path: ../../tests/system/Database/Live/UpsertTest.php - - message: '#^Parameter \#2 \$message of method CodeIgniter\\Log\\Handlers\\ChromeLoggerHandler\:\:handle\(\) expects string, stdClass given\.$#' count: 1 diff --git a/utils/phpstan-baseline/loader.neon b/utils/phpstan-baseline/loader.neon index a77d7c3f2728..37af12f96222 100644 --- a/utils/phpstan-baseline/loader.neon +++ b/utils/phpstan-baseline/loader.neon @@ -1,4 +1,4 @@ -# total 153 errors +# total 132 errors includes: - argument.type.neon diff --git a/utils/phpstan-baseline/missingType.iterableValue.neon b/utils/phpstan-baseline/missingType.iterableValue.neon index 7a94bec26d15..e3ce34dc009d 100644 --- a/utils/phpstan-baseline/missingType.iterableValue.neon +++ b/utils/phpstan-baseline/missingType.iterableValue.neon @@ -1,4 +1,4 @@ -# total 95 errors +# total 82 errors parameters: ignoreErrors: @@ -357,71 +357,6 @@ parameters: count: 1 path: ../../tests/system/DataConverter/DataConverterTest.php - - - message: '#^Method CodeIgniter\\Database\\BaseConnectionTest\:\:provideProtectIdentifiers\(\) return type has no value type specified in iterable type iterable\.$#' - count: 1 - path: ../../tests/system/Database/BaseConnectionTest.php - - - - message: '#^Property CodeIgniter\\Database\\BaseConnectionTest\:\:\$failoverOptions type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../tests/system/Database/BaseConnectionTest.php - - - - message: '#^Property CodeIgniter\\Database\\BaseConnectionTest\:\:\$options type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../tests/system/Database/BaseConnectionTest.php - - - - message: '#^Method CodeIgniter\\Database\\BaseQueryTest\:\:provideHighlightQueryKeywords\(\) return type has no value type specified in iterable type iterable\.$#' - count: 1 - path: ../../tests/system/Database/BaseQueryTest.php - - - - message: '#^Method CodeIgniter\\Database\\BaseQueryTest\:\:provideIsWriteType\(\) return type has no value type specified in iterable type iterable\.$#' - count: 1 - path: ../../tests/system/Database/BaseQueryTest.php - - - - message: '#^Method CodeIgniter\\Database\\ConfigTest\:\:provideConvertDSN\(\) return type has no value type specified in iterable type iterable\.$#' - count: 1 - path: ../../tests/system/Database/ConfigTest.php - - - - message: '#^Property CodeIgniter\\Database\\ConfigTest\:\:\$dsnGroup type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../tests/system/Database/ConfigTest.php - - - - message: '#^Property CodeIgniter\\Database\\ConfigTest\:\:\$dsnGroupPostgre type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../tests/system/Database/ConfigTest.php - - - - message: '#^Property CodeIgniter\\Database\\ConfigTest\:\:\$dsnGroupPostgreNative type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../tests/system/Database/ConfigTest.php - - - - message: '#^Property CodeIgniter\\Database\\ConfigTest\:\:\$group type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../tests/system/Database/ConfigTest.php - - - - message: '#^Property CodeIgniter\\Database\\Live\\MetadataTest\:\:\$expectedTables type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../tests/system/Database/Live/MetadataTest.php - - - - message: '#^Method CodeIgniter\\Database\\Live\\UpdateTest\:\:provideUpdateBatch\(\) return type has no value type specified in iterable type iterable\.$#' - count: 1 - path: ../../tests/system/Database/Live/UpdateTest.php - - - - message: '#^Method CodeIgniter\\Database\\Live\\UpdateTest\:\:testUpdateBatch\(\) has parameter \$data with no value type specified in iterable type array\.$#' - count: 1 - path: ../../tests/system/Database/Live/UpdateTest.php - - message: '#^Method CodeIgniter\\I18n\\TimeLegacyTest\:\:provideToStringDoesNotDependOnLocale\(\) return type has no value type specified in iterable type iterable\.$#' count: 1 diff --git a/utils/phpstan-baseline/property.phpDocType.neon b/utils/phpstan-baseline/property.phpDocType.neon index fbbec53d281e..00ab948073fd 100644 --- a/utils/phpstan-baseline/property.phpDocType.neon +++ b/utils/phpstan-baseline/property.phpDocType.neon @@ -1,4 +1,4 @@ -# total 9 errors +# total 7 errors parameters: ignoreErrors: @@ -22,16 +22,6 @@ parameters: count: 1 path: ../../system/Session/Handlers/FileHandler.php - - - message: '#^PHPDoc type CodeIgniter\\Test\\Mock\\MockConnection of property CodeIgniter\\Database\\Builder\\InsertTest\:\:\$db is not the same as PHPDoc type CodeIgniter\\Database\\BaseConnection of overridden property CodeIgniter\\Test\\CIUnitTestCase\:\:\$db\.$#' - count: 1 - path: ../../tests/system/Database/Builder/InsertTest.php - - - - message: '#^PHPDoc type CodeIgniter\\Test\\Mock\\MockConnection of property CodeIgniter\\Database\\Builder\\UpdateTest\:\:\$db is not the same as PHPDoc type CodeIgniter\\Database\\BaseConnection of overridden property CodeIgniter\\Test\\CIUnitTestCase\:\:\$db\.$#' - count: 1 - path: ../../tests/system/Database/Builder/UpdateTest.php - - message: '#^PHPDoc type Tests\\Support\\Models\\EventModel of property CodeIgniter\\Models\\EventsModelTest\:\:\$model is not the same as PHPDoc type CodeIgniter\\Model of overridden property CodeIgniter\\Models\\LiveModelTestCase\:\:\$model\.$#' count: 1