Skip to content

Commit 4c3b162

Browse files
authored
Merge pull request #6709 from paulbalandan/connection-template-types
Add template types to `Connection`
2 parents dfe7891 + 62d5cf4 commit 4c3b162

15 files changed

+55
-155
lines changed

phpstan-baseline.neon.dist

Lines changed: 3 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -106,87 +106,12 @@ parameters:
106106
path: system/Database/Migration.php
107107

108108
-
109-
message: "#^Cannot access property \\$errno on bool\\|object\\|resource\\.$#"
109+
message: "#^Property CodeIgniter\\\\Database\\\\BaseConnection\\<mysqli\\|false\\>\\:\\:\\$strictOn \\(bool\\) in isset\\(\\) is not nullable\\.$#"
110110
count: 1
111111
path: system/Database/MySQLi/Connection.php
112112

113113
-
114-
message: "#^Cannot access property \\$error on bool\\|object\\|resource\\.$#"
115-
count: 1
116-
path: system/Database/MySQLi/Connection.php
117-
118-
-
119-
message: "#^Cannot access property \\$insert_id on bool\\|object\\|resource\\.$#"
120-
count: 1
121-
path: system/Database/MySQLi/Connection.php
122-
123-
-
124-
message: "#^Cannot call method autocommit\\(\\) on bool\\|object\\|resource\\.$#"
125-
count: 3
126-
path: system/Database/MySQLi/Connection.php
127-
128-
-
129-
message: "#^Cannot call method begin_transaction\\(\\) on bool\\|object\\|resource\\.$#"
130-
count: 1
131-
path: system/Database/MySQLi/Connection.php
132-
133-
-
134-
message: "#^Cannot call method close\\(\\) on bool\\|object\\|resource\\.$#"
135-
count: 1
136-
path: system/Database/MySQLi/Connection.php
137-
138-
-
139-
message: "#^Cannot call method commit\\(\\) on bool\\|object\\|resource\\.$#"
140-
count: 1
141-
path: system/Database/MySQLi/Connection.php
142-
143-
-
144-
message: "#^Cannot call method more_results\\(\\) on bool\\|object\\|resource\\.$#"
145-
count: 1
146-
path: system/Database/MySQLi/Connection.php
147-
148-
-
149-
message: "#^Cannot call method next_result\\(\\) on bool\\|object\\|resource\\.$#"
150-
count: 1
151-
path: system/Database/MySQLi/Connection.php
152-
153-
-
154-
message: "#^Cannot call method query\\(\\) on bool\\|object\\|resource\\.$#"
155-
count: 1
156-
path: system/Database/MySQLi/Connection.php
157-
158-
-
159-
message: "#^Cannot call method real_escape_string\\(\\) on bool\\|object\\|resource\\.$#"
160-
count: 1
161-
path: system/Database/MySQLi/Connection.php
162-
163-
-
164-
message: "#^Cannot call method rollback\\(\\) on bool\\|object\\|resource\\.$#"
165-
count: 1
166-
path: system/Database/MySQLi/Connection.php
167-
168-
-
169-
message: "#^Cannot call method select_db\\(\\) on bool\\|object\\|resource\\.$#"
170-
count: 1
171-
path: system/Database/MySQLi/Connection.php
172-
173-
-
174-
message: "#^Cannot call method store_result\\(\\) on bool\\|object\\|resource\\.$#"
175-
count: 1
176-
path: system/Database/MySQLi/Connection.php
177-
178-
-
179-
message: "#^Property CodeIgniter\\\\Database\\\\BaseConnection\\:\\:\\$strictOn \\(bool\\) in isset\\(\\) is not nullable\\.$#"
180-
count: 1
181-
path: system/Database/MySQLi/Connection.php
182-
183-
-
184-
message: "#^Property CodeIgniter\\\\Database\\\\MySQLi\\\\Connection\\:\\:\\$mysqli \\(mysqli\\) in empty\\(\\) is not falsy\\.$#"
185-
count: 1
186-
path: system/Database/MySQLi/Connection.php
187-
188-
-
189-
message: "#^Access to an undefined property CodeIgniter\\\\Database\\\\BaseConnection\\:\\:\\$mysqli\\.$#"
114+
message: "#^Access to an undefined property CodeIgniter\\\\Database\\\\BaseConnection\\<mysqli\\|false\\>\\:\\:\\$mysqli\\.$#"
190115
count: 3
191116
path: system/Database/MySQLi/PreparedQuery.php
192117

@@ -245,61 +170,6 @@ parameters:
245170
count: 13
246171
path: system/Database/SQLSRV/Forge.php
247172

248-
-
249-
message: "#^Cannot call method changes\\(\\) on bool\\|object\\|resource\\.$#"
250-
count: 1
251-
path: system/Database/SQLite3/Connection.php
252-
253-
-
254-
message: "#^Cannot call method close\\(\\) on bool\\|object\\|resource\\.$#"
255-
count: 1
256-
path: system/Database/SQLite3/Connection.php
257-
258-
-
259-
message: "#^Cannot call method escapeString\\(\\) on bool\\|object\\|resource\\.$#"
260-
count: 1
261-
path: system/Database/SQLite3/Connection.php
262-
263-
-
264-
message: "#^Cannot call method exec\\(\\) on bool\\|object\\|resource\\.$#"
265-
count: 4
266-
path: system/Database/SQLite3/Connection.php
267-
268-
-
269-
message: "#^Cannot call method lastErrorCode\\(\\) on bool\\|object\\|resource\\.$#"
270-
count: 1
271-
path: system/Database/SQLite3/Connection.php
272-
273-
-
274-
message: "#^Cannot call method lastErrorMsg\\(\\) on bool\\|object\\|resource\\.$#"
275-
count: 1
276-
path: system/Database/SQLite3/Connection.php
277-
278-
-
279-
message: "#^Cannot call method lastInsertRowID\\(\\) on bool\\|object\\|resource\\.$#"
280-
count: 1
281-
path: system/Database/SQLite3/Connection.php
282-
283-
-
284-
message: "#^Cannot call method query\\(\\) on bool\\|object\\|resource\\.$#"
285-
count: 1
286-
path: system/Database/SQLite3/Connection.php
287-
288-
-
289-
message: "#^Cannot call method lastErrorCode\\(\\) on bool\\|object\\|resource\\.$#"
290-
count: 1
291-
path: system/Database/SQLite3/PreparedQuery.php
292-
293-
-
294-
message: "#^Cannot call method lastErrorMsg\\(\\) on bool\\|object\\|resource\\.$#"
295-
count: 1
296-
path: system/Database/SQLite3/PreparedQuery.php
297-
298-
-
299-
message: "#^Cannot call method prepare\\(\\) on bool\\|object\\|resource\\.$#"
300-
count: 1
301-
path: system/Database/SQLite3/PreparedQuery.php
302-
303173
-
304174
message: "#^Cannot call method columnName\\(\\) on object\\|resource\\|false\\.$#"
305175
count: 2
@@ -566,7 +436,7 @@ parameters:
566436
path: system/Test/FeatureTestCase.php
567437

568438
-
569-
message: "#^Cannot access property \\$insert_id on bool\\|object\\|resource\\.$#"
439+
message: "#^Cannot access property \\$insert_id on object\\|resource\\|false\\.$#"
570440
count: 1
571441
path: system/Test/Mock/MockConnection.php
572442

system/Database/BaseConnection.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
* @property int $transDepth
4646
* @property bool $transFailure
4747
* @property bool $transStatus
48+
*
49+
* @template TConnection of false|object|resource
50+
*
51+
* @implements ConnectionInterface<TConnection>
4852
*/
4953
abstract class BaseConnection implements ConnectionInterface
5054
{
@@ -189,7 +193,8 @@ abstract class BaseConnection implements ConnectionInterface
189193
/**
190194
* Connection ID
191195
*
192-
* @var bool|object|resource
196+
* @var false|object|resource
197+
* @phpstan-var TConnection
193198
*/
194199
public $connID = false;
195200

@@ -457,7 +462,8 @@ public function persistentConnect()
457462
* get that connection. If you pass either alias in and only a single
458463
* connection is present, it must return the sole connection.
459464
*
460-
* @return mixed
465+
* @return false|object|resource
466+
* @phpstan-return TConnection
461467
*/
462468
public function getConnection(?string $alias = null)
463469
{

system/Database/BasePreparedQuery.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
/**
2121
* Base prepared query
2222
*
23+
* @template TConnection of false|object|resource
2324
* @template TStatement of object|resource
2425
*
25-
* @implements PreparedQueryInterface<TStatement>
26+
* @implements PreparedQueryInterface<TConnection, TStatement>
2627
*/
2728
abstract class BasePreparedQuery implements PreparedQueryInterface
2829
{
@@ -60,6 +61,7 @@ abstract class BasePreparedQuery implements PreparedQueryInterface
6061
* A reference to the db connection to use.
6162
*
6263
* @var BaseConnection
64+
* @phpstan-var BaseConnection<TConnection>
6365
*/
6466
protected $db;
6567

system/Database/ConnectionInterface.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,39 @@
1313

1414
/**
1515
* Interface ConnectionInterface
16+
*
17+
* @template TConnection of false|object|resource
1618
*/
1719
interface ConnectionInterface
1820
{
1921
/**
2022
* Initializes the database connection/settings.
2123
*
22-
* @return mixed
24+
* @return void
2325
*/
2426
public function initialize();
2527

2628
/**
2729
* Connect to the database.
2830
*
29-
* @return mixed
31+
* @return false|object|resource
32+
* @phpstan-return TConnection
3033
*/
3134
public function connect(bool $persistent = false);
3235

3336
/**
3437
* Create a persistent database connection.
3538
*
36-
* @return mixed
39+
* @return false|object|resource
40+
* @phpstan-return TConnection
3741
*/
3842
public function persistentConnect();
3943

4044
/**
4145
* Keep or establish the connection if no queries have been sent for
4246
* a length of time exceeding the server's idle timeout.
4347
*
44-
* @return mixed
48+
* @return void
4549
*/
4650
public function reconnect();
4751

@@ -51,7 +55,8 @@ public function reconnect();
5155
* get that connection. If you pass either alias in and only a single
5256
* connection is present, it must return the sole connection.
5357
*
54-
* @return mixed
58+
* @return false|object|resource
59+
* @phpstan-return TConnection
5560
*/
5661
public function getConnection(?string $alias = null);
5762

system/Database/MySQLi/Connection.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
use CodeIgniter\Database\BaseConnection;
1515
use CodeIgniter\Database\Exceptions\DatabaseException;
1616
use LogicException;
17-
use MySQLi;
17+
use mysqli;
1818
use mysqli_sql_exception;
1919
use stdClass;
2020
use Throwable;
2121

2222
/**
2323
* Connection for MySQLi
24+
*
25+
* @extends BaseConnection<false|mysqli>
2426
*/
2527
class Connection extends BaseConnection
2628
{
@@ -54,7 +56,7 @@ class Connection extends BaseConnection
5456
*
5557
* Has to be preserved without being assigned to $conn_id.
5658
*
57-
* @var MySQLi
59+
* @var false|mysqli
5860
*/
5961
public $mysqli;
6062

@@ -72,7 +74,7 @@ class Connection extends BaseConnection
7274
/**
7375
* Connect to the database.
7476
*
75-
* @return mixed
77+
* @return false|mysqli
7678
*
7779
* @throws DatabaseException
7880
*/

system/Database/MySQLi/PreparedQuery.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
use BadMethodCallException;
1515
use CodeIgniter\Database\BasePreparedQuery;
1616
use CodeIgniter\Database\Exceptions\DatabaseException;
17+
use mysqli;
1718
use mysqli_stmt;
1819

1920
/**
2021
* Prepared query for MySQLi
2122
*
22-
* @extends BasePreparedQuery<mysqli_stmt>
23+
* @extends BasePreparedQuery<false|mysqli, mysqli_stmt>
2324
*/
2425
class PreparedQuery extends BasePreparedQuery
2526
{

system/Database/OCI8/Connection.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
/**
2222
* Connection for OCI8
23+
*
24+
* @extends BaseConnection<false|resource>
2325
*/
2426
class Connection extends BaseConnection implements ConnectionInterface
2527
{
@@ -113,7 +115,7 @@ private function isValidDSN(): bool
113115
/**
114116
* Connect to the database.
115117
*
116-
* @return mixed
118+
* @return false|resource
117119
*/
118120
public function connect(bool $persistent = false)
119121
{

system/Database/OCI8/PreparedQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/**
2020
* Prepared query for OCI8
2121
*
22-
* @extends BasePreparedQuery<resource>
22+
* @extends BasePreparedQuery<false|resource, resource>
2323
*/
2424
class PreparedQuery extends BasePreparedQuery implements PreparedQueryInterface
2525
{

system/Database/Postgre/Connection.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
use CodeIgniter\Database\Exceptions\DatabaseException;
1616
use CodeIgniter\Database\RawSql;
1717
use ErrorException;
18+
use PgSql\Connection as PgSqlConnection;
1819
use stdClass;
1920

2021
/**
2122
* Connection for Postgre
23+
*
24+
* @extends BaseConnection<false|PgSqlConnection>
2225
*/
2326
class Connection extends BaseConnection
2427
{
@@ -51,7 +54,8 @@ class Connection extends BaseConnection
5154
/**
5255
* Connect to the database.
5356
*
54-
* @return mixed
57+
* @return false|resource
58+
* @phpstan-return false|PgSqlConnection
5559
*/
5660
public function connect(bool $persistent = false)
5761
{

system/Database/Postgre/PreparedQuery.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
use CodeIgniter\Database\BasePreparedQuery;
1616
use CodeIgniter\Database\Exceptions\DatabaseException;
1717
use Exception;
18+
use PgSql\Connection as PgSqlConnection;
19+
use PgSql\Result as PgSqlResult;
1820

1921
/**
2022
* Prepared query for Postgre
2123
*
22-
* @extends BasePreparedQuery<\PgSql\Result>
24+
* @extends BasePreparedQuery<false|PgSqlConnection, PgSqlResult>
2325
*/
2426
class PreparedQuery extends BasePreparedQuery
2527
{

system/Database/PreparedQueryInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
/**
1717
* Prepared query interface
1818
*
19+
* @template TConnection of false|object|resource
1920
* @template TStatement of object|resource
2021
*/
2122
interface PreparedQueryInterface

0 commit comments

Comments
 (0)