Skip to content

Commit 24d4944

Browse files
authored
Merge pull request #6749 from paulbalandan/result-template-types
Add template types to Result
2 parents 2ce4b48 + 9bdce75 commit 24d4944

22 files changed

+93
-122
lines changed

phpstan-baseline.neon.dist

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,6 @@ parameters:
7575
count: 1
7676
path: system/Database/BaseConnection.php
7777

78-
-
79-
message: "#^Negated boolean expression is always false\\.$#"
80-
count: 3
81-
path: system/Database/BaseResult.php
82-
8378
-
8479
message: "#^Unreachable statement \\- code above always terminates\\.$#"
8580
count: 2
@@ -106,55 +101,15 @@ parameters:
106101
path: system/Database/Migration.php
107102

108103
-
109-
message: "#^Property CodeIgniter\\\\Database\\\\BaseConnection\\<mysqli\\|false\\>\\:\\:\\$strictOn \\(bool\\) in isset\\(\\) is not nullable\\.$#"
104+
message: "#^Property CodeIgniter\\\\Database\\\\BaseConnection\\<mysqli,mysqli_result\\>\\:\\:\\$strictOn \\(bool\\) in isset\\(\\) is not nullable\\.$#"
110105
count: 1
111106
path: system/Database/MySQLi/Connection.php
112107

113108
-
114-
message: "#^Access to an undefined property CodeIgniter\\\\Database\\\\BaseConnection\\<mysqli\\|false\\>\\:\\:\\$mysqli\\.$#"
109+
message: "#^Access to an undefined property CodeIgniter\\\\Database\\\\BaseConnection\\<mysqli, mysqli_result\\>\\:\\:\\$mysqli\\.$#"
115110
count: 3
116111
path: system/Database/MySQLi/PreparedQuery.php
117112

118-
-
119-
message: "#^Cannot access property \\$field_count on object\\|resource\\|false\\.$#"
120-
count: 1
121-
path: system/Database/MySQLi/Result.php
122-
123-
-
124-
message: "#^Cannot access property \\$num_rows on object\\|resource\\|false\\.$#"
125-
count: 1
126-
path: system/Database/MySQLi/Result.php
127-
128-
-
129-
message: "#^Cannot call method data_seek\\(\\) on object\\|resource\\|false\\.$#"
130-
count: 1
131-
path: system/Database/MySQLi/Result.php
132-
133-
-
134-
message: "#^Cannot call method fetch_assoc\\(\\) on object\\|resource\\|false\\.$#"
135-
count: 1
136-
path: system/Database/MySQLi/Result.php
137-
138-
-
139-
message: "#^Cannot call method fetch_field\\(\\) on object\\|resource\\|false\\.$#"
140-
count: 1
141-
path: system/Database/MySQLi/Result.php
142-
143-
-
144-
message: "#^Cannot call method fetch_fields\\(\\) on object\\|resource\\|false\\.$#"
145-
count: 1
146-
path: system/Database/MySQLi/Result.php
147-
148-
-
149-
message: "#^Cannot call method fetch_object\\(\\) on object\\|resource\\|false\\.$#"
150-
count: 1
151-
path: system/Database/MySQLi/Result.php
152-
153-
-
154-
message: "#^Cannot call method field_seek\\(\\) on object\\|resource\\|false\\.$#"
155-
count: 1
156-
path: system/Database/MySQLi/Result.php
157-
158113
-
159114
message: "#^Strict comparison using \\=\\=\\= between array<string, int|string|null> and false will always evaluate to false\\.$#"
160115
count: 1
@@ -170,31 +125,6 @@ parameters:
170125
count: 13
171126
path: system/Database/SQLSRV/Forge.php
172127

173-
-
174-
message: "#^Cannot call method columnName\\(\\) on object\\|resource\\|false\\.$#"
175-
count: 2
176-
path: system/Database/SQLite3/Result.php
177-
178-
-
179-
message: "#^Cannot call method columnType\\(\\) on object\\|resource\\|false\\.$#"
180-
count: 1
181-
path: system/Database/SQLite3/Result.php
182-
183-
-
184-
message: "#^Cannot call method fetchArray\\(\\) on object\\|resource\\|false\\.$#"
185-
count: 2
186-
path: system/Database/SQLite3/Result.php
187-
188-
-
189-
message: "#^Cannot call method numColumns\\(\\) on object\\|resource\\|false\\.$#"
190-
count: 1
191-
path: system/Database/SQLite3/Result.php
192-
193-
-
194-
message: "#^Cannot call method reset\\(\\) on object\\|resource\\|false\\.$#"
195-
count: 2
196-
path: system/Database/SQLite3/Result.php
197-
198128
-
199129
message: "#^Call to an undefined method CodeIgniter\\\\View\\\\RendererInterface\\:\\:getPerformanceData\\(\\)\\.$#"
200130
count: 1

system/Database/BaseConnection.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@
4646
* @property bool $transFailure
4747
* @property bool $transStatus
4848
*
49-
* @template TConnection of false|object|resource
49+
* @template TConnection of object|resource
50+
* @template TResult of object|resource
5051
*
51-
* @implements ConnectionInterface<TConnection>
52+
* @implements ConnectionInterface<TConnection, TResult>
5253
*/
5354
abstract class BaseConnection implements ConnectionInterface
5455
{
@@ -194,14 +195,15 @@ abstract class BaseConnection implements ConnectionInterface
194195
* Connection ID
195196
*
196197
* @var false|object|resource
197-
* @phpstan-var TConnection
198+
* @phpstan-var false|TConnection
198199
*/
199200
public $connID = false;
200201

201202
/**
202203
* Result ID
203204
*
204-
* @var bool|object|resource
205+
* @var false|object|resource
206+
* @phpstan-var false|TResult
205207
*/
206208
public $resultID = false;
207209

@@ -538,7 +540,8 @@ public function addTableAlias(string $table)
538540
/**
539541
* Executes the query against the database.
540542
*
541-
* @return bool|object|resource
543+
* @return false|object|resource
544+
* @phpstan-return false|TResult
542545
*/
543546
abstract protected function execute(string $sql);
544547

@@ -553,6 +556,7 @@ abstract protected function execute(string $sql);
553556
* @param mixed ...$binds
554557
*
555558
* @return BaseResult|bool|Query BaseResult when “read” type query, bool when “write” type query, Query when prepared query
559+
* @phpstan-return BaseResult<TConnection, TResult>|bool|Query
556560
*
557561
* @todo BC set $queryClass default as null in 4.1
558562
*/
@@ -662,7 +666,8 @@ public function query(string $sql, $binds = null, bool $setEscapeFlags = true, s
662666
* is performed, nor are transactions handled. Simply takes a raw
663667
* query string and returns the database-specific result id.
664668
*
665-
* @return mixed
669+
* @return false|object|resource
670+
* @phpstan-return false|TResult
666671
*/
667672
public function simpleQuery(string $sql)
668673
{

system/Database/BasePreparedQuery.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@
1818
use ErrorException;
1919

2020
/**
21-
* Base prepared query
22-
*
23-
* @template TConnection of false|object|resource
21+
* @template TConnection of object|resource
2422
* @template TStatement of object|resource
23+
* @template TResult of object|resource
2524
*
26-
* @implements PreparedQueryInterface<TConnection, TStatement>
25+
* @implements PreparedQueryInterface<TConnection, TStatement, TResult>
2726
*/
2827
abstract class BasePreparedQuery implements PreparedQueryInterface
2928
{
@@ -61,7 +60,7 @@ abstract class BasePreparedQuery implements PreparedQueryInterface
6160
* A reference to the db connection to use.
6261
*
6362
* @var BaseConnection
64-
* @phpstan-var BaseConnection<TConnection>
63+
* @phpstan-var BaseConnection<TConnection, TResult>
6564
*/
6665
protected $db;
6766

@@ -77,7 +76,7 @@ public function __construct(BaseConnection $db)
7776
* NOTE: This version is based on SQL code. Child classes should
7877
* override this method.
7978
*
80-
* @return mixed
79+
* @return $this
8180
*/
8281
public function prepare(string $sql, array $options = [], string $queryClass = Query::class)
8382
{
@@ -103,7 +102,7 @@ public function prepare(string $sql, array $options = [], string $queryClass = Q
103102
/**
104103
* The database-dependent portion of the prepare statement.
105104
*
106-
* @return mixed
105+
* @return $this
107106
*/
108107
abstract public function _prepare(string $sql, array $options = []);
109108

@@ -112,6 +111,7 @@ abstract public function _prepare(string $sql, array $options = []);
112111
* prepared query. Upon success, will return a Results object.
113112
*
114113
* @return bool|ResultInterface
114+
* @phpstan-return bool|ResultInterface<TConnection, TResult>
115115
*
116116
* @throws DatabaseException
117117
*/
@@ -195,7 +195,7 @@ abstract public function _execute(array $data): bool;
195195
/**
196196
* Returns the result object for the prepared query.
197197
*
198-
* @return mixed
198+
* @return object|resource|null
199199
*/
200200
abstract public function _getResult();
201201

system/Database/BaseResult.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,26 @@
1414
use CodeIgniter\Entity\Entity;
1515

1616
/**
17-
* Class BaseResult
17+
* @template TConnection of object|resource
18+
* @template TResult of object|resource
19+
*
20+
* @implements ResultInterface<TConnection, TResult>
1821
*/
1922
abstract class BaseResult implements ResultInterface
2023
{
2124
/**
2225
* Connection ID
2326
*
2427
* @var object|resource
28+
* @phpstan-var TConnection
2529
*/
2630
public $connID;
2731

2832
/**
2933
* Result ID
3034
*
3135
* @var false|object|resource
36+
* @phpstan-var false|TResult
3237
*/
3338
public $resultID;
3439

@@ -79,6 +84,8 @@ abstract class BaseResult implements ResultInterface
7984
*
8085
* @param object|resource $connID
8186
* @param object|resource $resultID
87+
* @phpstan-param TConnection $connID
88+
* @phpstan-param TResult $resultID
8289
*/
8390
public function __construct(&$connID, &$resultID)
8491
{
@@ -117,7 +124,7 @@ public function getCustomResultObject(string $className)
117124
return $this->customResultObject[$className];
118125
}
119126

120-
if (is_bool($this->resultID) || ! $this->resultID) {
127+
if (! $this->isValidResultId()) {
121128
return [];
122129
}
123130

@@ -171,7 +178,7 @@ public function getResultArray(): array
171178
// In the event that query caching is on, the result_id variable
172179
// will not be a valid resource so we'll simply return an empty
173180
// array.
174-
if (is_bool($this->resultID) || ! $this->resultID) {
181+
if (! $this->isValidResultId()) {
175182
return [];
176183
}
177184

@@ -208,7 +215,7 @@ public function getResultObject(): array
208215
// In the event that query caching is on, the result_id variable
209216
// will not be a valid resource so we'll simply return an empty
210217
// array.
211-
if (is_bool($this->resultID) || ! $this->resultID) {
218+
if (! $this->isValidResultId()) {
212219
return [];
213220
}
214221

@@ -463,6 +470,11 @@ public function getNumRows(): int
463470
return $this->numRows = count($this->getResultArray());
464471
}
465472

473+
private function isValidResultId(): bool
474+
{
475+
return is_resource($this->resultID) || is_object($this->resultID);
476+
}
477+
466478
/**
467479
* Gets the number of fields in the result set.
468480
*/

system/Database/ConnectionInterface.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
namespace CodeIgniter\Database;
1313

1414
/**
15-
* Interface ConnectionInterface
16-
*
17-
* @template TConnection of false|object|resource
15+
* @template TConnection of object|resource
16+
* @template TResult of object|resource
1817
*/
1918
interface ConnectionInterface
2019
{
@@ -29,15 +28,15 @@ public function initialize();
2928
* Connect to the database.
3029
*
3130
* @return false|object|resource
32-
* @phpstan-return TConnection
31+
* @phpstan-return false|TConnection
3332
*/
3433
public function connect(bool $persistent = false);
3534

3635
/**
3736
* Create a persistent database connection.
3837
*
3938
* @return false|object|resource
40-
* @phpstan-return TConnection
39+
* @phpstan-return false|TConnection
4140
*/
4241
public function persistentConnect();
4342

@@ -56,7 +55,7 @@ public function reconnect();
5655
* connection is present, it must return the sole connection.
5756
*
5857
* @return false|object|resource
59-
* @phpstan-return TConnection
58+
* @phpstan-return false|TConnection
6059
*/
6160
public function getConnection(?string $alias = null);
6261

@@ -102,6 +101,7 @@ public function getVersion(): string;
102101
* @param mixed ...$binds
103102
*
104103
* @return BaseResult|bool|Query
104+
* @phpstan-return BaseResult<TConnection, TResult>|bool|Query
105105
*/
106106
public function query(string $sql, $binds = null);
107107

@@ -110,7 +110,8 @@ public function query(string $sql, $binds = null);
110110
* is performed, nor are transactions handled. Simply takes a raw
111111
* query string and returns the database-specific result id.
112112
*
113-
* @return mixed
113+
* @return false|object|resource
114+
* @phpstan-return false|TResult
114115
*/
115116
public function simpleQuery(string $sql);
116117

system/Database/MySQLi/Connection.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
use CodeIgniter\Database\Exceptions\DatabaseException;
1616
use LogicException;
1717
use mysqli;
18+
use mysqli_result;
1819
use mysqli_sql_exception;
1920
use stdClass;
2021
use Throwable;
2122

2223
/**
2324
* Connection for MySQLi
2425
*
25-
* @extends BaseConnection<false|mysqli>
26+
* @extends BaseConnection<mysqli, mysqli_result>
2627
*/
2728
class Connection extends BaseConnection
2829
{
@@ -279,7 +280,7 @@ public function getVersion(): string
279280
/**
280281
* Executes the query against the database.
281282
*
282-
* @return bool|object
283+
* @return false|mysqli_result;
283284
*/
284285
protected function execute(string $sql)
285286
{

0 commit comments

Comments
 (0)