Skip to content

Commit d9ee179

Browse files
authored
refactor: fix phpstan errors in Config, Database, Query, MigrationRunner, and SQLite3\Table (#10477)
1 parent 68883ae commit d9ee179

13 files changed

Lines changed: 75 additions & 188 deletions

File tree

system/Database/Config.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Config extends BaseConfig
2626
* Cache for instance of any connections that
2727
* have been requested as a "shared" instance.
2828
*
29-
* @var array
29+
* @var array<string, BaseConnection>
3030
*/
3131
protected static $instances = [];
3232

@@ -41,9 +41,9 @@ class Config extends BaseConfig
4141
/**
4242
* Returns the database connection
4343
*
44-
* @param array|BaseConnection|non-empty-string|null $group The name of the connection group to use,
45-
* or an array of configuration settings.
46-
* @param bool $getShared Whether to return a shared instance of the connection.
44+
* @param array<string, mixed>|BaseConnection|non-empty-string|null $group The name of the connection group to use,
45+
* or an array of configuration settings.
46+
* @param bool $getShared Whether to return a shared instance of the connection.
4747
*
4848
* @return BaseConnection
4949
*/
@@ -90,6 +90,8 @@ public static function connect($group = null, bool $getShared = true)
9090

9191
/**
9292
* Returns an array of all db connections currently made.
93+
*
94+
* @return array<string, BaseConnection>
9395
*/
9496
public static function getConnections(): array
9597
{
@@ -100,7 +102,7 @@ public static function getConnections(): array
100102
* Loads and returns an instance of the Forge for the specified
101103
* database group, and loads the group if it hasn't been loaded yet.
102104
*
103-
* @param array|ConnectionInterface|string|null $group
105+
* @param array<string, mixed>|ConnectionInterface|string|null $group
104106
*
105107
* @return Forge
106108
*/
@@ -114,7 +116,7 @@ public static function forge($group = null)
114116
/**
115117
* Returns a new instance of the Database Utilities class.
116118
*
117-
* @param array|string|null $group
119+
* @param array<string, mixed>|string|null $group
118120
*
119121
* @return BaseUtils
120122
*/

system/Database/Database.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ class Database
3131
* Helps to keep track of all open connections for performance
3232
* monitoring, logging, etc.
3333
*
34-
* @var array
34+
* @var array<string, BaseConnection>
3535
*/
3636
protected $connections = [];
3737

3838
/**
3939
* Parses the connection binds and creates a Database Connection instance.
4040
*
41+
* @param array<string, mixed> $params
42+
*
4143
* @return BaseConnection
4244
*
4345
* @throws InvalidArgumentException
@@ -94,6 +96,10 @@ public function loadUtils(ConnectionInterface $db): BaseUtils
9496
/**
9597
* Parses universal DSN string
9698
*
99+
* @param array<string, mixed> $params
100+
*
101+
* @return array<string, mixed>
102+
*
97103
* @throws InvalidArgumentException
98104
*/
99105
protected function parseDSN(array $params): array
@@ -132,9 +138,9 @@ protected function parseDSN(array $params): array
132138
/**
133139
* Creates a database object.
134140
*
135-
* @param string $driver Driver name. FQCN can be used.
136-
* @param string $class 'Connection'|'Forge'|'Utils'
137-
* @param array|ConnectionInterface $argument The constructor parameter or DB connection
141+
* @param string $driver Driver name. FQCN can be used.
142+
* @param string $class 'Connection'|'Forge'|'Utils'
143+
* @param array<string, mixed>|ConnectionInterface $argument The constructor parameter or DB connection
138144
*
139145
* @return BaseConnection|BaseUtils|Forge
140146
*/

system/Database/MigrationRunner.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class MigrationRunner
9090
/**
9191
* used to return messages for CLI.
9292
*
93-
* @var array
93+
* @var list<string>
9494
*/
9595
protected $cliMessages = [];
9696

@@ -139,7 +139,7 @@ class MigrationRunner
139139
* default DB group so that it creates the `migrations` table in the default
140140
* DB group. Therefore, passing $db is for testing purposes only.
141141
*
142-
* @param array|ConnectionInterface|string|null $db DB group. For testing purposes only.
142+
* @param array<string, mixed>|ConnectionInterface|string|null $db DB group. For testing purposes only.
143143
*
144144
* @throws ConfigException
145145
*/
@@ -440,7 +440,7 @@ public function force(string $path, string $namespace, ?string $group = null)
440440
/**
441441
* Retrieves list of available migration scripts
442442
*
443-
* @return array List of all located migrations by their UID
443+
* @return array<string, stdClass> List of all located migrations by their UID
444444
*/
445445
public function findMigrations(): array
446446
{
@@ -465,6 +465,8 @@ public function findMigrations(): array
465465

466466
/**
467467
* Retrieves a list of available migration scripts for one namespace
468+
*
469+
* @return list<stdClass>
468470
*/
469471
public function findNamespaceMigrations(string $namespace): array
470472
{
@@ -610,6 +612,8 @@ public function getObjectUid($object): string
610612

611613
/**
612614
* Retrieves messages formatted for CLI output
615+
*
616+
* @return list<string>
613617
*/
614618
public function getCliMessages(): array
615619
{
@@ -693,6 +697,8 @@ protected function removeHistory($history)
693697

694698
/**
695699
* Grabs the full migration history from the database for a group
700+
*
701+
* @return list<stdClass>
696702
*/
697703
public function getHistory(string $group = 'default'): array
698704
{
@@ -719,6 +725,8 @@ public function getHistory(string $group = 'default'): array
719725
* Returns the migration history for a single batch.
720726
*
721727
* @param string $order
728+
*
729+
* @return list<stdClass>
722730
*/
723731
public function getBatchHistory(int $batch, $order = 'asc'): array
724732
{
@@ -734,6 +742,8 @@ public function getBatchHistory(int $batch, $order = 'asc'): array
734742

735743
/**
736744
* Returns all the batches from the database history in order
745+
*
746+
* @return list<int>
737747
*/
738748
public function getBatches(): array
739749
{

system/Database/Query.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Query implements QueryInterface, Stringable
4444
/**
4545
* The binds and their values used for binding.
4646
*
47-
* @var array
47+
* @var array<array-key, array{0: mixed, 1: bool}>
4848
*/
4949
protected $binds = [];
5050

@@ -129,6 +129,8 @@ public function setQuery(string $sql, mixed $binds = null, bool $setEscape = tru
129129
/**
130130
* Will store the variables to bind into the query later.
131131
*
132+
* @param array<array-key, mixed> $binds
133+
*
132134
* @return $this
133135
*/
134136
public function setBinds(array $binds, bool $setEscape = true)
@@ -266,6 +268,9 @@ protected function compileBinds()
266268
}
267269
}
268270

271+
/**
272+
* @param array<array-key, array{0: mixed, 1: bool}> $binds
273+
*/
269274
protected function matchNamedBinds(string $sql, array $binds): string
270275
{
271276
$replacers = [];
@@ -287,6 +292,9 @@ protected function matchNamedBinds(string $sql, array $binds): string
287292
return strtr($sql, $replacers);
288293
}
289294

295+
/**
296+
* @param array<array-key, array{0: mixed, 1: bool}> $binds
297+
*/
290298
protected function matchSimpleBinds(string $sql, array $binds, int $bindCount, int $ml): string
291299
{
292300
if ($c = preg_match_all("/'[^']*'/", $sql, $matches) >= 1) {

system/Database/SQLite3/Table.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ class Table
3535
/**
3636
* All of the unique/primary keys in the table.
3737
*
38-
* @var array
38+
* @var array<string, array{fields: list<string>, type: string}>
3939
*/
4040
protected $keys = [];
4141

4242
/**
4343
* All of the foreign keys in the table.
4444
*
45-
* @var array
45+
* @var array<array-key, stdClass>
4646
*/
4747
protected $foreignKeys = [];
4848

@@ -230,6 +230,8 @@ public function dropForeignKey(string $foreignName)
230230

231231
/**
232232
* Adds primary key
233+
*
234+
* @param array{fields?: list<string>} $fields
233235
*/
234236
public function addPrimaryKey(array $fields): Table
235237
{
@@ -254,6 +256,8 @@ public function addPrimaryKey(array $fields): Table
254256
/**
255257
* Add a foreign key
256258
*
259+
* @param list<array{field: list<string>, referenceTable: string, referenceField: list<string>, onDelete: string, onUpdate: string, fkName: string}> $foreignKeys
260+
*
257261
* @return $this
258262
*/
259263
public function addForeignKey(array $foreignKeys)
@@ -374,9 +378,9 @@ protected function copyData()
374378
* Converts fields retrieved from the database to
375379
* the format needed for creating fields with Forge.
376380
*
377-
* @param array|bool $fields
381+
* @param bool|list<stdClass> $fields
378382
*
379-
* @return ($fields is array ? array : mixed)
383+
* @return ($fields is array ? array<string, array<string, bool|int|string|null>> : mixed)
380384
*/
381385
protected function formatFields($fields)
382386
{
@@ -452,7 +456,7 @@ private function isNumericType(string $type): bool
452456
*
453457
* @param array<string, stdClass> $keys
454458
*
455-
* @return array<string, array{fields: string, type: string}>
459+
* @return array<string, array{fields: list<string>, type: string}>
456460
*/
457461
protected function formatKeys($keys)
458462
{

tests/system/Database/Live/SQLite3/AlterTableTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace CodeIgniter\Database\Live\SQLite3;
1515

1616
use CodeIgniter\Database\Exceptions\DataException;
17+
use CodeIgniter\Database\SQLite3\Connection;
1718
use CodeIgniter\Database\SQLite3\Forge;
1819
use CodeIgniter\Database\SQLite3\Table;
1920
use CodeIgniter\Test\CIUnitTestCase;
@@ -54,8 +55,14 @@ protected function setUp(): void
5455
'database' => ':memory:',
5556
'DBDebug' => true,
5657
];
57-
$this->db = db_connect($config);
58-
$this->forge = Database::forge($config);
58+
$db = db_connect($config);
59+
$this->assertInstanceOf(Connection::class, $db);
60+
$this->db = $db;
61+
62+
$forge = Database::forge($config);
63+
$this->assertInstanceOf(Forge::class, $forge);
64+
$this->forge = $forge;
65+
5966
$this->table = new Table($this->db, $this->forge);
6067

6168
$this->dropTables();

tests/system/Database/Live/SQLite3/GetIndexDataTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ protected function setUp(): void
4141
'database' => 'database.db',
4242
'DBDebug' => true,
4343
];
44-
$this->db = db_connect($config, false);
45-
$this->forge = Database::forge($config);
44+
$this->db = db_connect($config, false);
45+
46+
$forge = Database::forge($config);
47+
$this->assertInstanceOf(Forge::class, $forge);
48+
$this->forge = $forge;
4649
}
4750

4851
public function testGetIndexData(): void

tests/system/Database/Migrations/MigrationRunnerTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
namespace CodeIgniter\Database\Migrations;
1515

1616
use CodeIgniter\Database\BaseConnection;
17+
use CodeIgniter\Database\ConnectionInterface;
1718
use CodeIgniter\Database\MigrationRunner;
19+
use CodeIgniter\Database\SQLSRV\Connection as SQLSRVConnection;
1820
use CodeIgniter\Events\Events;
1921
use CodeIgniter\Exceptions\ConfigException;
2022
use CodeIgniter\Test\CIUnitTestCase;
@@ -121,6 +123,7 @@ public function testGetHistory(): void
121123
];
122124

123125
if ($this->db->DBDriver === 'SQLSRV') {
126+
$this->assertInstanceOf(SQLSRVConnection::class, $this->db);
124127
$this->db->simpleQuery('SET IDENTITY_INSERT ' . $this->db->escapeIdentifiers($this->db->schema) . '.' . $this->db->prefixTable('migrations') . ' ON');
125128
}
126129

@@ -138,6 +141,7 @@ public function testGetHistory(): void
138141
$this->assertSame($expected, $history);
139142

140143
if ($this->db->DBDriver === 'SQLSRV') {
144+
$this->assertInstanceOf(SQLSRVConnection::class, $this->db);
141145
$this->db->simpleQuery('SET IDENTITY_INSERT ' . $this->db->escapeIdentifiers($this->db->schema) . '.' . $this->db->prefixTable('migrations') . ' OFF');
142146
$db = $this->getPrivateProperty($runner, 'db');
143147
$db->table('migrations')->delete(['id' => 4]);
@@ -477,6 +481,9 @@ public function testMigrationUsesSameConnectionAsMigrationRunner(): void
477481
}
478482
}
479483

484+
/**
485+
* @param array<string, mixed>|ConnectionInterface|string|null $db
486+
*/
480487
protected function resetTables($db = null): void
481488
{
482489
$forge = Database::forge($db);

utils/phpstan-baseline/assign.propertyType.neon

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 24 errors
1+
# total 22 errors
22

33
parameters:
44
ignoreErrors:
@@ -17,16 +17,6 @@ parameters:
1717
count: 1
1818
path: ../../tests/system/Commands/Utilities/Routes/FilterFinderTest.php
1919

20-
-
21-
message: '#^Property CodeIgniter\\Database\\Live\\SQLite3\\AlterTableTest\:\:\$forge \(CodeIgniter\\Database\\SQLite3\\Forge\) does not accept CodeIgniter\\Database\\Forge\.$#'
22-
count: 1
23-
path: ../../tests/system/Database/Live/SQLite3/AlterTableTest.php
24-
25-
-
26-
message: '#^Property CodeIgniter\\Database\\Live\\SQLite3\\GetIndexDataTest\:\:\$forge \(CodeIgniter\\Database\\SQLite3\\Forge\) does not accept CodeIgniter\\Database\\Forge\.$#'
27-
count: 1
28-
path: ../../tests/system/Database/Live/SQLite3/GetIndexDataTest.php
29-
3020
-
3121
message: '#^Property CodeIgniter\\Filters\\CSRFTest\:\:\$response \(CodeIgniter\\HTTP\\Response\|null\) does not accept CodeIgniter\\HTTP\\ResponseInterface\.$#'
3222
count: 2

utils/phpstan-baseline/loader.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 572 errors
1+
# total 539 errors
22

33
includes:
44
- argument.type.neon

0 commit comments

Comments
 (0)