Skip to content

Commit 9a4acaa

Browse files
authored
refactor: fix phpstan errors in Connection (#10475)
1 parent 22a7e4e commit 9a4acaa

19 files changed

Lines changed: 103 additions & 496 deletions

system/Database/BaseConnection.php

Lines changed: 61 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,35 @@
2525
use Throwable;
2626

2727
/**
28-
* @property-read array $aliasedTables
29-
* @property-read string $charset
30-
* @property-read bool $compress
31-
* @property-read float $connectDuration
32-
* @property-read float $connectTime
33-
* @property-read string $database
34-
* @property-read array $dateFormat
35-
* @property-read string $DBCollat
36-
* @property-read bool $DBDebug
37-
* @property-read string $DBDriver
38-
* @property-read string $DBPrefix
39-
* @property-read string $DSN
40-
* @property-read array|bool $encrypt
41-
* @property-read array $failover
42-
* @property-read string $hostname
43-
* @property-read Query $lastQuery
44-
* @property-read string $password
45-
* @property-read bool $pConnect
46-
* @property-read int|string $port
47-
* @property-read bool $pretend
48-
* @property-read string $queryClass
49-
* @property-read array $reservedIdentifiers
50-
* @property-read bool $strictOn
51-
* @property-read string $subdriver
52-
* @property-read string $swapPre
53-
* @property-read int $transDepth
54-
* @property-read bool $transFailure
55-
* @property-read bool $transStatus
56-
* @property-read string $username
28+
* @property-read list<string> $aliasedTables
29+
* @property-read string $charset
30+
* @property-read bool $compress
31+
* @property-read float $connectDuration
32+
* @property-read float $connectTime
33+
* @property-read string $database
34+
* @property-read array<string, string> $dateFormat
35+
* @property-read string $DBCollat
36+
* @property-read bool $DBDebug
37+
* @property-read string $DBDriver
38+
* @property-read string $DBPrefix
39+
* @property-read string $DSN
40+
* @property-read array<string, bool|string>|bool $encrypt
41+
* @property-read list<array<string, mixed>> $failover
42+
* @property-read string $hostname
43+
* @property-read Query $lastQuery
44+
* @property-read string $password
45+
* @property-read bool $pConnect
46+
* @property-read int|string $port
47+
* @property-read bool $pretend
48+
* @property-read string $queryClass
49+
* @property-read list<string> $reservedIdentifiers
50+
* @property-read bool $strictOn
51+
* @property-read string $subdriver
52+
* @property-read string $swapPre
53+
* @property-read int $transDepth
54+
* @property-read bool $transFailure
55+
* @property-read bool $transStatus
56+
* @property-read string $username
5757
*
5858
* @template TConnection
5959
* @template TResult
@@ -177,7 +177,7 @@ abstract class BaseConnection implements ConnectionInterface
177177
/**
178178
* Encryption flag/data
179179
*
180-
* @var array|bool
180+
* @var array<string, bool|string>|bool
181181
*/
182182
protected $encrypt = false;
183183

@@ -202,7 +202,7 @@ abstract class BaseConnection implements ConnectionInterface
202202
/**
203203
* Settings for a failover connection.
204204
*
205-
* @var array
205+
* @var list<array<string, mixed>>
206206
*/
207207
protected $failover = [];
208208

@@ -240,14 +240,14 @@ abstract class BaseConnection implements ConnectionInterface
240240
*
241241
* Identifiers that must NOT be escaped.
242242
*
243-
* @var array
243+
* @var list<string>
244244
*/
245245
protected $reservedIdentifiers = ['*'];
246246

247247
/**
248248
* Identifier escape character
249249
*
250-
* @var array|string
250+
* @var list<string>|string
251251
*/
252252
public $escapeChar = '"';
253253

@@ -268,15 +268,12 @@ abstract class BaseConnection implements ConnectionInterface
268268
/**
269269
* RegExp used to escape identifiers
270270
*
271-
* @var array
271+
* @var list<string>
272272
*/
273273
protected $pregEscapeChar = [];
274274

275275
/**
276-
* Holds previously looked up data
277-
* for performance reasons.
278-
*
279-
* @var array
276+
* @var array<string, mixed>
280277
*/
281278
public $dataCache = [];
282279

@@ -374,7 +371,7 @@ abstract class BaseConnection implements ConnectionInterface
374371
];
375372

376373
/**
377-
* Saves our connection settings.
374+
* @param array<string, mixed> $params
378375
*/
379376
public function __construct(array $params)
380377
{
@@ -739,6 +736,8 @@ public function getPlatform(): string
739736
* collected during use of the Builder, and set here
740737
* so queries are built correctly.
741738
*
739+
* @param list<string> $aliases
740+
*
742741
* @return $this
743742
*/
744743
public function setAliasedTables(array $aliases)
@@ -1111,7 +1110,7 @@ abstract protected function _transRollback(): bool;
11111110
/**
11121111
* Returns a non-shared new instance of the query builder for this connection.
11131112
*
1114-
* @param array|string|TableName $tableName
1113+
* @param array<array-key, string>|string|TableName $tableName
11151114
*
11161115
* @return BaseBuilder
11171116
*
@@ -1156,6 +1155,7 @@ public function newQuery(): BaseBuilder
11561155
* })
11571156
*
11581157
* @param Closure(BaseConnection): mixed $func
1158+
* @param array<string, mixed> $options
11591159
*
11601160
* @return BasePreparedQuery|null
11611161
*/
@@ -1242,12 +1242,12 @@ public function getConnectDuration(int $decimals = 6): string
12421242
* insert the table prefix (if it exists) in the proper position, and escape only
12431243
* the correct identifiers.
12441244
*
1245-
* @param array|int|string|TableName $item
1246-
* @param bool $prefixSingle Prefix a table name with no segments?
1247-
* @param bool $protectIdentifiers Protect table or column names?
1248-
* @param bool $fieldExists Supplied $item contains a column name?
1245+
* @param array<array-key, mixed>|int|string|TableName $item
1246+
* @param bool $prefixSingle Prefix a table name with no segments?
1247+
* @param bool $protectIdentifiers Protect table or column names?
1248+
* @param bool $fieldExists Supplied $item contains a column name?
12491249
*
1250-
* @return ($item is array ? array : string)
1250+
* @return ($item is array ? array<array-key, mixed> : string)
12511251
*/
12521252
public function protectIdentifiers($item, bool $prefixSingle = false, ?bool $protectIdentifiers = null, bool $fieldExists = true)
12531253
{
@@ -1448,9 +1448,9 @@ private function escapeTableName(TableName $tableName): string
14481448
*
14491449
* This function escapes column and table names
14501450
*
1451-
* @param array|string $item
1451+
* @param array<array-key, mixed>|string $item
14521452
*
1453-
* @return ($item is array ? array : string)
1453+
* @return ($item is array ? array<array-key, mixed> : string)
14541454
*/
14551455
public function escapeIdentifiers($item)
14561456
{
@@ -1534,7 +1534,7 @@ abstract public function affectedRows(): int;
15341534
*
15351535
* @param mixed $str
15361536
*
1537-
* @return ($str is array ? array : float|int|string)
1537+
* @return ($str is array ? array<array-key, mixed> : float|int|string)
15381538
*/
15391539
public function escape($str)
15401540
{
@@ -1638,7 +1638,7 @@ protected function _escapeString(string $str): string
16381638
* This function enables you to call PHP database functions that are not natively included
16391639
* in CodeIgniter, in a platform independent manner.
16401640
*
1641-
* @param array ...$params
1641+
* @param mixed ...$params
16421642
*
16431643
* @throws DatabaseException
16441644
*/
@@ -1682,7 +1682,7 @@ protected function getDriverFunctionPrefix(): string
16821682
*/
16831683
public function listTables(bool $constrainByPrefix = false)
16841684
{
1685-
if (isset($this->dataCache['table_names']) && $this->dataCache['table_names']) {
1685+
if (($this->dataCache['table_names'] ?? []) !== []) {
16861686
$tables = $constrainByPrefix
16871687
? preg_grep("/^{$this->DBPrefix}/", $this->dataCache['table_names'])
16881688
: $this->dataCache['table_names'];
@@ -1847,21 +1847,18 @@ public function getForeignKeyData(string $table)
18471847
/**
18481848
* Converts array of arrays generated by _foreignKeyData() to array of objects
18491849
*
1850-
* @return array<string, stdClass>
1850+
* @param array<array-key, array{
1851+
* constraint_name: string|null,
1852+
* table_name: string,
1853+
* column_name: list<string>,
1854+
* foreign_table_name: string,
1855+
* foreign_column_name: list<string>,
1856+
* on_delete: string,
1857+
* on_update: string|null,
1858+
* match: string|null,
1859+
* }> $data
18511860
*
1852-
* array[
1853-
* {constraint_name} =>
1854-
* stdClass[
1855-
* 'constraint_name' => string,
1856-
* 'table_name' => string,
1857-
* 'column_name' => string[],
1858-
* 'foreign_table_name' => string,
1859-
* 'foreign_column_name' => string[],
1860-
* 'on_delete' => string,
1861-
* 'on_update' => string,
1862-
* 'match' => string
1863-
* ]
1864-
* ]
1861+
* @return array<string, stdClass>
18651862
*/
18661863
protected function foreignKeyDataToObjects(array $data)
18671864
{

system/Database/ConnectionInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function simpleQuery(string $sql);
118118
/**
119119
* Returns an instance of the query builder for this connection.
120120
*
121-
* @param array|string $tableName Table name.
121+
* @param array<array-key, string>|string $tableName Table name.
122122
*
123123
* @return BaseBuilder Builder.
124124
*/
@@ -139,15 +139,15 @@ public function getLastQuery();
139139
*
140140
* @param mixed $str
141141
*
142-
* @return ($str is array ? array : float|int|string)
142+
* @return ($str is array ? array<array-key, mixed> : float|int|string)
143143
*/
144144
public function escape($str);
145145

146146
/**
147147
* Allows for custom calls to the database engine that are not
148148
* supported through our database layer.
149149
*
150-
* @param array ...$params
150+
* @param mixed ...$params
151151
*
152152
* @return mixed
153153
*/

system/Database/MySQLi/Connection.php

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use CodeIgniter\Database\BaseConnection;
1717
use CodeIgniter\Database\Exceptions\DatabaseException;
18-
use CodeIgniter\Database\TableName;
1918
use CodeIgniter\Exceptions\LogicException;
2019
use mysqli;
2120
use mysqli_result;
@@ -51,7 +50,7 @@ class Connection extends BaseConnection
5150
/**
5251
* Identifier escape character
5352
*
54-
* @var string
53+
* @var list<string>|string
5554
*/
5655
public $escapeChar = '`';
5756

@@ -234,11 +233,6 @@ public function connect(bool $persistent = false)
234233
return false;
235234
}
236235

237-
/**
238-
* Close the database connection.
239-
*
240-
* @return void
241-
*/
242236
protected function _close()
243237
{
244238
$this->connID->close();
@@ -379,10 +373,7 @@ public function escapeLikeStringDirect($str)
379373
}
380374

381375
/**
382-
* Generates the SQL for listing tables in a platform-dependent manner.
383376
* Uses escapeLikeStringDirect().
384-
*
385-
* @param string|null $tableName If $tableName is provided will return only this table if exists.
386377
*/
387378
protected function _listTables(bool $prefixLimit = false, ?string $tableName = null): string
388379
{
@@ -399,11 +390,6 @@ protected function _listTables(bool $prefixLimit = false, ?string $tableName = n
399390
return $sql;
400391
}
401392

402-
/**
403-
* Generates a platform-specific query string so that the column names can be fetched.
404-
*
405-
* @param string|TableName $table
406-
*/
407393
protected function _listColumns($table = ''): string
408394
{
409395
$tableName = $this->protectIdentifiers(
@@ -417,10 +403,6 @@ protected function _listColumns($table = ''): string
417403
}
418404

419405
/**
420-
* Returns an array of objects with field data
421-
*
422-
* @return list<stdClass>
423-
*
424406
* @throws DatabaseException
425407
*/
426408
protected function _fieldData(string $table): array
@@ -449,10 +431,6 @@ protected function _fieldData(string $table): array
449431
}
450432

451433
/**
452-
* Returns an array of objects with index data
453-
*
454-
* @return array<string, stdClass>
455-
*
456434
* @throws DatabaseException
457435
* @throws LogicException
458436
*/
@@ -497,10 +475,6 @@ protected function _indexData(string $table): array
497475
}
498476

499477
/**
500-
* Returns an array of objects with Foreign key data
501-
*
502-
* @return array<string, stdClass>
503-
*
504478
* @throws DatabaseException
505479
*/
506480
protected function _foreignKeyData(string $table): array
@@ -598,25 +572,16 @@ public function insertID(): int
598572
return $this->connID->insert_id;
599573
}
600574

601-
/**
602-
* Begin Transaction
603-
*/
604575
protected function _transBegin(): bool
605576
{
606577
return $this->connID->begin_transaction();
607578
}
608579

609-
/**
610-
* Commit Transaction
611-
*/
612580
protected function _transCommit(): bool
613581
{
614582
return $this->connID->commit();
615583
}
616584

617-
/**
618-
* Rollback Transaction
619-
*/
620585
protected function _transRollback(): bool
621586
{
622587
return $this->connID->rollback();

0 commit comments

Comments
 (0)