Skip to content

Commit 22a7e4e

Browse files
authored
refactor: fix phpstan errors in Result (#10473)
1 parent 9ec5138 commit 22a7e4e

15 files changed

Lines changed: 43 additions & 611 deletions

File tree

system/Database/BaseResult.php

Lines changed: 4 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,32 @@
2525
abstract class BaseResult implements ResultInterface
2626
{
2727
/**
28-
* Connection ID
29-
*
3028
* @var TConnection
3129
*/
3230
public $connID;
3331

3432
/**
35-
* Result ID
36-
*
3733
* @var false|TResult
3834
*/
3935
public $resultID;
4036

4137
/**
42-
* Result Array
43-
*
4438
* @var list<array>
4539
*/
4640
public $resultArray = [];
4741

4842
/**
49-
* Result Object
50-
*
5143
* @var list<object>
5244
*/
5345
public $resultObject = [];
5446

5547
/**
56-
* Custom Result Object
57-
*
58-
* @var array
48+
* @var array<class-string, list<object>>
5949
*/
6050
public $customResultObject = [];
6151

6252
/**
63-
* Current Row index
53+
* Current row index
6454
*
6555
* @var int
6656
*/
@@ -74,15 +64,11 @@ abstract class BaseResult implements ResultInterface
7464
protected $numRows;
7565

7666
/**
77-
* Row data
78-
*
79-
* @var array|null
67+
* @var array<string, mixed>|null
8068
*/
8169
public $rowData;
8270

8371
/**
84-
* Constructor
85-
*
8672
* @param TConnection $connID
8773
* @param TResult $resultID
8874
*/
@@ -92,13 +78,6 @@ public function __construct(&$connID, &$resultID)
9278
$this->resultID = $resultID;
9379
}
9480

95-
/**
96-
* Retrieve the results of the query. Typically an array of
97-
* individual data rows, which can be either an 'array', an
98-
* 'object', or a custom class name.
99-
*
100-
* @param string $type The row type. Either 'array', 'object', or a class name to use
101-
*/
10281
public function getResult(string $type = 'object'): array
10382
{
10483
if ($type === 'array') {
@@ -112,13 +91,6 @@ public function getResult(string $type = 'object'): array
11291
return $this->getCustomResultObject($type);
11392
}
11493

115-
/**
116-
* Returns the results as an array of custom objects.
117-
*
118-
* @param class-string $className
119-
*
120-
* @return array
121-
*/
12294
public function getCustomResultObject(string $className)
12395
{
12496
if (isset($this->customResultObject[$className])) {
@@ -165,11 +137,6 @@ public function getCustomResultObject(string $className)
165137
return $this->customResultObject[$className];
166138
}
167139

168-
/**
169-
* Returns the results as an array of arrays.
170-
*
171-
* If no results, an empty array is returned.
172-
*/
173140
public function getResultArray(): array
174141
{
175142
if ($this->resultArray !== []) {
@@ -202,13 +169,6 @@ public function getResultArray(): array
202169
return $this->resultArray;
203170
}
204171

205-
/**
206-
* Returns the results as an array of objects.
207-
*
208-
* If no results, an empty array is returned.
209-
*
210-
* @return list<stdClass>
211-
*/
212172
public function getResultObject(): array
213173
{
214174
if ($this->resultObject !== []) {
@@ -245,19 +205,6 @@ public function getResultObject(): array
245205
return $this->resultObject;
246206
}
247207

248-
/**
249-
* Wrapper object to return a row as either an array, an object, or
250-
* a custom class.
251-
*
252-
* If the row doesn't exist, returns null.
253-
*
254-
* @template T of object
255-
*
256-
* @param int|string $n The index of the results to return, or column name.
257-
* @param 'array'|'object'|class-string<T> $type The type of result object. 'array', 'object' or class name.
258-
*
259-
* @return ($n is string ? float|int|string|null : ($type is 'object' ? stdClass|null : ($type is 'array' ? array|null : T|null)))
260-
*/
261208
public function getRow($n = 0, string $type = 'object')
262209
{
263210
// $n is a column name.
@@ -286,18 +233,6 @@ public function getRow($n = 0, string $type = 'object')
286233
return $this->getCustomRowObject($n, $type);
287234
}
288235

289-
/**
290-
* Returns a row as a custom class instance.
291-
*
292-
* If the row doesn't exist, returns null.
293-
*
294-
* @template T of object
295-
*
296-
* @param int $n The index of the results to return.
297-
* @param class-string<T> $className
298-
*
299-
* @return T|null
300-
*/
301236
public function getCustomRowObject(int $n, string $className)
302237
{
303238
if (! isset($this->customResultObject[$className])) {
@@ -315,13 +250,6 @@ public function getCustomRowObject(int $n, string $className)
315250
return $this->customResultObject[$className][$this->currentRow] ?? null;
316251
}
317252

318-
/**
319-
* Returns a single row from the results as an array.
320-
*
321-
* If row doesn't exist, returns null.
322-
*
323-
* @return array|null
324-
*/
325253
public function getRowArray(int $n = 0)
326254
{
327255
$result = $this->getResultArray();
@@ -336,13 +264,6 @@ public function getRowArray(int $n = 0)
336264
return $result[$this->currentRow] ?? null;
337265
}
338266

339-
/**
340-
* Returns a single row from the results as an object.
341-
*
342-
* If row doesn't exist, returns null.
343-
*
344-
* @return object|stdClass|null
345-
*/
346267
public function getRowObject(int $n = 0)
347268
{
348269
$result = $this->getResultObject();
@@ -357,14 +278,6 @@ public function getRowObject(int $n = 0)
357278
return $result[$this->currentRow] ?? null;
358279
}
359280

360-
/**
361-
* Assigns an item into a particular column slot.
362-
*
363-
* @param array|string $key
364-
* @param array|object|stdClass|null $value
365-
*
366-
* @return void
367-
*/
368281
public function setRow($key, $value = null)
369282
{
370283
// We cache the row data for subsequent uses
@@ -385,29 +298,20 @@ public function setRow($key, $value = null)
385298
}
386299
}
387300

388-
/**
389-
* Returns the "first" row of the current results.
390-
*/
391301
public function getFirstRow(string $type = 'object')
392302
{
393303
$result = $this->getResult($type);
394304

395305
return ($result === []) ? null : $result[0];
396306
}
397307

398-
/**
399-
* Returns the "last" row of the current results.
400-
*/
401308
public function getLastRow(string $type = 'object')
402309
{
403310
$result = $this->getResult($type);
404311

405312
return ($result === []) ? null : $result[count($result) - 1];
406313
}
407314

408-
/**
409-
* Returns the "next" row of the current results.
410-
*/
411315
public function getNextRow(string $type = 'object')
412316
{
413317
$result = $this->getResult($type);
@@ -418,9 +322,6 @@ public function getNextRow(string $type = 'object')
418322
return isset($result[$this->currentRow + 1]) ? $result[++$this->currentRow] : null;
419323
}
420324

421-
/**
422-
* Returns the "previous" row of the current results.
423-
*/
424325
public function getPreviousRow(string $type = 'object')
425326
{
426327
$result = $this->getResult($type);
@@ -435,11 +336,6 @@ public function getPreviousRow(string $type = 'object')
435336
return $result[$this->currentRow] ?? null;
436337
}
437338

438-
/**
439-
* Returns an unbuffered row and move the pointer to the next row.
440-
*
441-
* @return array|object|null
442-
*/
443339
public function getUnbufferedRow(string $type = 'object')
444340
{
445341
if ($type === 'array') {
@@ -478,43 +374,12 @@ private function isValidResultId(): bool
478374
return is_resource($this->resultID) || is_object($this->resultID);
479375
}
480376

481-
/**
482-
* Gets the number of fields in the result set.
483-
*/
484-
abstract public function getFieldCount(): int;
485-
486-
/**
487-
* Generates an array of column names in the result set.
488-
*/
489-
abstract public function getFieldNames(): array;
490-
491-
/**
492-
* Generates an array of objects representing field meta-data.
493-
*/
494-
abstract public function getFieldData(): array;
495-
496-
/**
497-
* Frees the current result.
498-
*
499-
* @return void
500-
*/
501-
abstract public function freeResult();
502-
503-
/**
504-
* Moves the internal pointer to the desired offset. This is called
505-
* internally before fetching results to make sure the result set
506-
* starts at zero.
507-
*
508-
* @return bool
509-
*/
510-
abstract public function dataSeek(int $n = 0);
511-
512377
/**
513378
* Returns the result set as an array.
514379
*
515380
* Overridden by driver classes.
516381
*
517-
* @return array|false|null
382+
* @return array<string, mixed>|false|null
518383
*/
519384
abstract protected function fetchAssoc();
520385

system/Database/MySQLi/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ protected function _indexData(string $table): array
481481
$type = 'PRIMARY';
482482
} elseif ($index['Index_type'] === 'FULLTEXT') {
483483
$type = 'FULLTEXT';
484-
} elseif ($index['Non_unique']) {
484+
} elseif ((bool) $index['Non_unique']) {
485485
$type = $index['Index_type'] === 'SPATIAL' ? 'SPATIAL' : 'INDEX';
486486
} else {
487487
$type = 'UNIQUE';

system/Database/MySQLi/Result.php

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,11 @@
2626
*/
2727
class Result extends BaseResult
2828
{
29-
/**
30-
* Gets the number of fields in the result set.
31-
*/
3229
public function getFieldCount(): int
3330
{
3431
return $this->resultID->field_count;
3532
}
3633

37-
/**
38-
* Generates an array of column names in the result set.
39-
*/
4034
public function getFieldNames(): array
4135
{
4236
$fieldNames = [];
@@ -49,9 +43,6 @@ public function getFieldNames(): array
4943
return $fieldNames;
5044
}
5145

52-
/**
53-
* Generates an array of objects representing field meta-data.
54-
*/
5546
public function getFieldData(): array
5647
{
5748
static $dataTypes = [
@@ -103,11 +94,6 @@ public function getFieldData(): array
10394
return $retVal;
10495
}
10596

106-
/**
107-
* Frees the current result.
108-
*
109-
* @return void
110-
*/
11197
public function freeResult()
11298
{
11399
if (is_object($this->resultID)) {
@@ -116,38 +102,17 @@ public function freeResult()
116102
}
117103
}
118104

119-
/**
120-
* Moves the internal pointer to the desired offset. This is called
121-
* internally before fetching results to make sure the result set
122-
* starts at zero.
123-
*
124-
* @return bool
125-
*/
126105
public function dataSeek(int $n = 0)
127106
{
128107
return $this->resultID->data_seek($n);
129108
}
130109

131-
/**
132-
* Returns the result set as an array.
133-
*
134-
* Overridden by driver classes.
135-
*
136-
* @return array|false|null
137-
*/
138110
protected function fetchAssoc()
139111
{
140112
return $this->resultID->fetch_assoc();
141113
}
142114

143-
/**
144-
* Returns the result set as an object.
145-
*
146-
* Overridden by child classes.
147-
*
148-
* @return Entity|false|object|stdClass
149-
*/
150-
protected function fetchObject(string $className = 'stdClass')
115+
protected function fetchObject(string $className = stdClass::class)
151116
{
152117
if (is_subclass_of($className, Entity::class)) {
153118
$data = $this->fetchAssoc();
@@ -158,9 +123,6 @@ protected function fetchObject(string $className = 'stdClass')
158123
return $this->resultID->fetch_object($className);
159124
}
160125

161-
/**
162-
* Returns the number of rows in the resultID (i.e., mysqli_result object)
163-
*/
164126
public function getNumRows(): int
165127
{
166128
if (! is_int($this->numRows)) {

0 commit comments

Comments
 (0)