From a0a7389d3b25efc135931607385b6377ac4dedad Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Thu, 3 Mar 2022 06:49:15 +0900 Subject: [PATCH] Fixes for phan and phpstan Note that the (string) for pg_escape_string and pg_escape_literal are forced for phpstan because it thinks this might return false even thought both function only return string ever --- www/lib/CoreLibs/DB/IO.php | 22 +++++++++++++--------- www/lib/CoreLibs/DB/SQL/PgSQL.php | 10 +++++++--- www/lib/CoreLibs/Debug/Support.php | 2 +- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/www/lib/CoreLibs/DB/IO.php b/www/lib/CoreLibs/DB/IO.php index 9eb17390..6d4e03aa 100644 --- a/www/lib/CoreLibs/DB/IO.php +++ b/www/lib/CoreLibs/DB/IO.php @@ -694,10 +694,10 @@ class IO * @param object|resource|bool $cursor current cursor for pg_result_error, * pg_last_error too, but pg_result_error * is more accurate (PgSql\Result) - * @return array Pos 0: if we could get the method where it was called - * if not found [Uknown Method] - * Pos 1: if we have the pg_error_string from last error - * if nothing then empty string + * @return array Pos 0: if we could get the method where it was called + * if not found [Uknown Method] + * Pos 1: if we have the pg_error_string from last error + * if nothing then empty string */ private function __dbErrorPreprocessor($cursor = false): array { @@ -898,7 +898,7 @@ class IO * Read data from previous written data cache * @param string $query_hash The hash for the current query * @param boolean $assoc_only Only return assoc value (key named) - * @return array Current position query data from cache + * @return array Current position query data from cache */ private function __dbReturnCacheRead(string $query_hash, bool $assoc_only): array { @@ -1479,9 +1479,12 @@ class IO break; // bytea data case 'by': - $value = empty($value) ? 'NULL' : $this->dbEscapeBytea($value); + $value = empty($value) ? 'NULL' : $this->dbEscapeBytea((string)$value); break; case 'b': + if (is_float($value)) { + $value = (int)$value; + } $value = $value === '' || $value === null ? 'NULL' : "'" . $this->dbBoolean($value, true) . "'"; @@ -2840,7 +2843,7 @@ class IO /** * Return field names from query - * @return array Field names as array + * @return array Field names as array */ public function dbGetFieldNames(): array { @@ -2896,7 +2899,8 @@ class IO /** * Return the combined warning and error history - * @return array + * TODO: add options to return only needed (Eg error, time based) + * @return array Complete long error history string */ public function dbGetCombinedErrorHistory(): array { @@ -2996,7 +3000,7 @@ class IO /** * DEPRECATED: getCursorExt * @param string|null $q [DEPRECATED] - * @return array|null [DEPRECATED] + * @return array|string|int|resource|object|null [DEPRECATED] * @deprecated use dbGetCursorExt($q = null) instead */ public function getCursorExt($q = null) diff --git a/www/lib/CoreLibs/DB/SQL/PgSQL.php b/www/lib/CoreLibs/DB/SQL/PgSQL.php index fda5258a..aa0db2b1 100644 --- a/www/lib/CoreLibs/DB/SQL/PgSQL.php +++ b/www/lib/CoreLibs/DB/SQL/PgSQL.php @@ -528,9 +528,10 @@ class PgSQL public function __dbEscapeLiteral($string): string { if ($this->dbh === false || is_bool($this->dbh)) { - return ''; + return (string)''; } - return pg_escape_literal($this->dbh, (string)$string); + // for phpstan, thinks this is string|false? + return (string)pg_escape_literal($this->dbh, (string)$string); } /** @@ -544,7 +545,8 @@ class PgSQL if ($this->dbh === false || is_bool($this->dbh)) { return ''; } - return pg_escape_identifier($this->dbh, (string)$string); + // for phpstan, thinks this is string|false? + return (string)pg_escape_identifier($this->dbh, (string)$string); } /** @@ -583,7 +585,9 @@ class PgSQL return false; } $busy = pg_connection_busy($this->dbh); + /** @var array|null */ $socket = [pg_socket($this->dbh)]; + $null = null; while ($busy) { // Will wait on that socket until that happens or the timeout is reached stream_select($socket, $null, $null, $timeout_seconds); diff --git a/www/lib/CoreLibs/Debug/Support.php b/www/lib/CoreLibs/Debug/Support.php index b0b02ad6..284e01f6 100644 --- a/www/lib/CoreLibs/Debug/Support.php +++ b/www/lib/CoreLibs/Debug/Support.php @@ -97,7 +97,7 @@ class Support * Defaults to skip level 0 wich is this methid * @param integer $start_level From what level on, as defaul starts with 1 * to exclude self - * @return array All method names in list where max is last called + * @return array All method names in list where max is last called */ public static function getCallerMethodList(int $start_level = 1): array {