From a1d748eb5d41780643566137cb6bb6014b45a90b Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Fri, 25 Sep 2026 20:34:34 +0800 Subject: [PATCH] refactor: tighten docblock return types in Result, CLIRequest, url_helper and IncomingRequest --- system/Database/ResultInterface.php | 14 +++++++++----- system/HTTP/CLIRequest.php | 12 ++++++------ system/HTTP/IncomingRequest.php | 4 ++-- system/Helpers/url_helper.php | 6 ++++-- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/system/Database/ResultInterface.php b/system/Database/ResultInterface.php index 48de9f01c3d4..afe6ede83390 100644 --- a/system/Database/ResultInterface.php +++ b/system/Database/ResultInterface.php @@ -26,18 +26,22 @@ interface ResultInterface * individual data rows, which can be either an 'array', an * 'object', or a custom class name. * - * @param string $type The row type. Either 'array', 'object', or a class name to use + * @template T of object + * + * @param 'array'|'object'|class-string $type The row type. Either 'array', 'object', or a class name to use * - * @return ($type is 'array' ? list> : ($type is 'object' ? list : list)) + * @return ($type is 'array' ? list> : ($type is 'object' ? list : list)) */ public function getResult(string $type = 'object'): array; /** * Returns the results as an array of custom objects. * - * @param string $className The name of the class to use. + * @template T of object + * + * @param class-string $className The name of the class to use. * - * @return list + * @return list */ public function getCustomResultObject(string $className); @@ -102,7 +106,7 @@ public function getRowArray(int $n = 0); * * If row doesn't exist, returns null. * - * @return object|stdClass|null + * @return stdClass|null */ public function getRowObject(int $n = 0); diff --git a/system/HTTP/CLIRequest.php b/system/HTTP/CLIRequest.php index f1e98c50bf79..55c8b797b094 100644 --- a/system/HTTP/CLIRequest.php +++ b/system/HTTP/CLIRequest.php @@ -236,7 +236,7 @@ public function isCLI(): bool * @param int|null $filter A filter name to apply. * @param array|int|null $flags * - * @return array{}|null + * @return ($index is string ? null : array{}) */ public function getGet($index = null, $filter = null, $flags = null) { @@ -250,7 +250,7 @@ public function getGet($index = null, $filter = null, $flags = null) * @param int|null $filter A filter name to apply * @param array|int|null $flags * - * @return array{}|null + * @return ($index is string ? null : array{}) */ public function getPost($index = null, $filter = null, $flags = null) { @@ -264,7 +264,7 @@ public function getPost($index = null, $filter = null, $flags = null) * @param int|null $filter A filter name to apply * @param array|int|null $flags * - * @return array{}|null + * @return ($index is string ? null : array{}) */ public function getPostGet($index = null, $filter = null, $flags = null) { @@ -278,7 +278,7 @@ public function getPostGet($index = null, $filter = null, $flags = null) * @param int|null $filter A filter name to apply * @param array|int|null $flags * - * @return array{}|null + * @return ($index is string ? null : array{}) */ public function getGetPost($index = null, $filter = null, $flags = null) { @@ -292,7 +292,7 @@ public function getGetPost($index = null, $filter = null, $flags = null) * @param int|null $filter A filter name to be applied * @param mixed $flags * - * @return array{}|null + * @return ($index is string ? null : array{}) */ public function getCookie($index = null, $filter = null, $flags = null) { @@ -302,7 +302,7 @@ public function getCookie($index = null, $filter = null, $flags = null) /** * @param list|string|null $index * - * @return array{}|null + * @return ($index is string ? null : array{}) */ private function returnNullOrEmptyArray($index) { diff --git a/system/HTTP/IncomingRequest.php b/system/HTTP/IncomingRequest.php index 5eb029df8385..cc66e35f4ddc 100644 --- a/system/HTTP/IncomingRequest.php +++ b/system/HTTP/IncomingRequest.php @@ -403,7 +403,7 @@ public function getVar($index = null, $filter = null, $flags = null) * * @see http://php.net/manual/en/function.json-decode.php * - * @return array|bool|float|int|stdClass|null + * @return ($assoc is true ? array|bool|float|int|string|null : array|bool|float|int|stdClass|string|null) * * @throws HTTPException When the body is invalid as JSON. */ @@ -430,7 +430,7 @@ public function getJSON(bool $assoc = false, int $depth = 512, int $options = 0) * @param int|null $filter Filter Constant * @param array|int|null $flags Option * - * @return array|bool|float|int|stdClass|string|null + * @return ($assoc is true ? array|bool|float|int|string|null : array|bool|float|int|stdClass|string|null) */ public function getJsonVar($index = null, bool $assoc = false, ?int $filter = null, $flags = null) { diff --git a/system/Helpers/url_helper.php b/system/Helpers/url_helper.php index e43954909d65..c08187c6c40b 100644 --- a/system/Helpers/url_helper.php +++ b/system/Helpers/url_helper.php @@ -68,8 +68,8 @@ function base_url($relativePath = '', ?string $scheme = null): string * @param bool $returnObject True to return an object instead of a string * @param IncomingRequest|null $request A request to use when retrieving the path * - * @return string|URI When returning string, the query and fragment parts are removed. - * When returning URI, the query and fragment parts are preserved. + * @return ($returnObject is true ? URI : string) When returning string, the query and fragment parts are removed. + * When returning URI, the query and fragment parts are preserved. */ function current_url(bool $returnObject = false, ?IncomingRequest $request = null): string|URI { @@ -87,6 +87,8 @@ function current_url(bool $returnObject = false, ?IncomingRequest $request = nul * we first check in a saved session variable, if it exists, and use that. * If that's not available, however, we'll use a sanitized url from $_SERVER['HTTP_REFERER'] * which can be set by the user so is untrusted and not set by certain browsers/servers. + * + * @return ($returnObject is true ? URI : string) */ function previous_url(bool $returnObject = false): string|URI {