Skip to content

Commit 28bb14c

Browse files
authored
refactor: tighten docblock return types in Result, CLIRequest, url_helper and IncomingRequest (#10588)
1 parent 0785bf5 commit 28bb14c

4 files changed

Lines changed: 21 additions & 15 deletions

File tree

‎system/Database/ResultInterface.php‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,22 @@ interface ResultInterface
2626
* individual data rows, which can be either an 'array', an
2727
* 'object', or a custom class name.
2828
*
29-
* @param string $type The row type. Either 'array', 'object', or a class name to use
29+
* @template T of object
30+
*
31+
* @param 'array'|'object'|class-string<T> $type The row type. Either 'array', 'object', or a class name to use
3032
*
31-
* @return ($type is 'array' ? list<array<string, mixed>> : ($type is 'object' ? list<stdClass> : list<object>))
33+
* @return ($type is 'array' ? list<array<string, mixed>> : ($type is 'object' ? list<stdClass> : list<T>))
3234
*/
3335
public function getResult(string $type = 'object'): array;
3436

3537
/**
3638
* Returns the results as an array of custom objects.
3739
*
38-
* @param string $className The name of the class to use.
40+
* @template T of object
41+
*
42+
* @param class-string<T> $className The name of the class to use.
3943
*
40-
* @return list<object>
44+
* @return list<T>
4145
*/
4246
public function getCustomResultObject(string $className);
4347

@@ -102,7 +106,7 @@ public function getRowArray(int $n = 0);
102106
*
103107
* If row doesn't exist, returns null.
104108
*
105-
* @return object|stdClass|null
109+
* @return stdClass|null
106110
*/
107111
public function getRowObject(int $n = 0);
108112

‎system/HTTP/CLIRequest.php‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public function isCLI(): bool
236236
* @param int|null $filter A filter name to apply.
237237
* @param array<string, mixed>|int|null $flags
238238
*
239-
* @return array{}|null
239+
* @return ($index is string ? null : array{})
240240
*/
241241
public function getGet($index = null, $filter = null, $flags = null)
242242
{
@@ -250,7 +250,7 @@ public function getGet($index = null, $filter = null, $flags = null)
250250
* @param int|null $filter A filter name to apply
251251
* @param array<string, mixed>|int|null $flags
252252
*
253-
* @return array{}|null
253+
* @return ($index is string ? null : array{})
254254
*/
255255
public function getPost($index = null, $filter = null, $flags = null)
256256
{
@@ -264,7 +264,7 @@ public function getPost($index = null, $filter = null, $flags = null)
264264
* @param int|null $filter A filter name to apply
265265
* @param array<string, mixed>|int|null $flags
266266
*
267-
* @return array{}|null
267+
* @return ($index is string ? null : array{})
268268
*/
269269
public function getPostGet($index = null, $filter = null, $flags = null)
270270
{
@@ -278,7 +278,7 @@ public function getPostGet($index = null, $filter = null, $flags = null)
278278
* @param int|null $filter A filter name to apply
279279
* @param array<string, mixed>|int|null $flags
280280
*
281-
* @return array{}|null
281+
* @return ($index is string ? null : array{})
282282
*/
283283
public function getGetPost($index = null, $filter = null, $flags = null)
284284
{
@@ -292,7 +292,7 @@ public function getGetPost($index = null, $filter = null, $flags = null)
292292
* @param int|null $filter A filter name to be applied
293293
* @param mixed $flags
294294
*
295-
* @return array{}|null
295+
* @return ($index is string ? null : array{})
296296
*/
297297
public function getCookie($index = null, $filter = null, $flags = null)
298298
{
@@ -302,7 +302,7 @@ public function getCookie($index = null, $filter = null, $flags = null)
302302
/**
303303
* @param list<string>|string|null $index
304304
*
305-
* @return array{}|null
305+
* @return ($index is string ? null : array{})
306306
*/
307307
private function returnNullOrEmptyArray($index)
308308
{

‎system/HTTP/IncomingRequest.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ public function getVar($index = null, $filter = null, $flags = null)
403403
*
404404
* @see http://php.net/manual/en/function.json-decode.php
405405
*
406-
* @return array<array-key, mixed>|bool|float|int|stdClass|null
406+
* @return ($assoc is true ? array<array-key, mixed>|bool|float|int|string|null : array<array-key, mixed>|bool|float|int|stdClass|string|null)
407407
*
408408
* @throws HTTPException When the body is invalid as JSON.
409409
*/
@@ -430,7 +430,7 @@ public function getJSON(bool $assoc = false, int $depth = 512, int $options = 0)
430430
* @param int|null $filter Filter Constant
431431
* @param array<string, mixed>|int|null $flags Option
432432
*
433-
* @return array<array-key, mixed>|bool|float|int|stdClass|string|null
433+
* @return ($assoc is true ? array<array-key, mixed>|bool|float|int|string|null : array<array-key, mixed>|bool|float|int|stdClass|string|null)
434434
*/
435435
public function getJsonVar($index = null, bool $assoc = false, ?int $filter = null, $flags = null)
436436
{

‎system/Helpers/url_helper.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ function base_url($relativePath = '', ?string $scheme = null): string
6868
* @param bool $returnObject True to return an object instead of a string
6969
* @param IncomingRequest|null $request A request to use when retrieving the path
7070
*
71-
* @return string|URI When returning string, the query and fragment parts are removed.
72-
* When returning URI, the query and fragment parts are preserved.
71+
* @return ($returnObject is true ? URI : string) When returning string, the query and fragment parts are removed.
72+
* When returning URI, the query and fragment parts are preserved.
7373
*/
7474
function current_url(bool $returnObject = false, ?IncomingRequest $request = null): string|URI
7575
{
@@ -87,6 +87,8 @@ function current_url(bool $returnObject = false, ?IncomingRequest $request = nul
8787
* we first check in a saved session variable, if it exists, and use that.
8888
* If that's not available, however, we'll use a sanitized url from $_SERVER['HTTP_REFERER']
8989
* which can be set by the user so is untrusted and not set by certain browsers/servers.
90+
*
91+
* @return ($returnObject is true ? URI : string)
9092
*/
9193
function previous_url(bool $returnObject = false): string|URI
9294
{

0 commit comments

Comments
 (0)