From 6a139d3b7cd65ce6104632ceeb738a2f6a0e9c80 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Thu, 20 Jan 2022 13:09:02 +0900 Subject: [PATCH] Ignore errors in DB\SQL\PgSQL for phpstan phpstan will throw an error because pg_* methods have changed from resource to object in php 8.1 So current var has object|resource dual type and this will fail Added ignore for phpstan in the config file. Also added conditional config file for phpstan where we can set based on current active PHP version baseline file created with --generate-baseline is added for error check --- phpstan-baseline.neon | 7 +++++ phpstan-conditional.php | 21 +++++++++++++ phpstan.neon | 6 +++- www/lib/CoreLibs/DB/IO.php | 3 +- www/lib/CoreLibs/DB/SQL/PgSQL.php | 51 +++++++++++++++++-------------- 5 files changed, 63 insertions(+), 25 deletions(-) create mode 100644 phpstan-baseline.neon create mode 100644 phpstan-conditional.php diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 00000000..9e809591 --- /dev/null +++ b/phpstan-baseline.neon @@ -0,0 +1,7 @@ +parameters: + ignoreErrors: + - + message: "#^Parameter \\#1 \\$result of function pg_result_error expects resource, object\\|resource\\|true given\\.$#" + count: 1 + path: www/lib/CoreLibs/DB/SQL/PgSQL.php + diff --git a/phpstan-conditional.php b/phpstan-conditional.php new file mode 100644 index 00000000..eaa4c8e1 --- /dev/null +++ b/phpstan-conditional.php @@ -0,0 +1,21 @@ += 8_00_00) { + // Change of signature in PHP 8.1 + /* $config['parameters']['ignoreErrors'][] = [ + 'message' => '~Parameter #1 \$(result|connection) of function pg_\w+ ' + . 'expects resource(\|null)?, object\|resource given\.~', + 'path' => 'www/lib/CoreLibs/DB/SQL/PgSQL.php', + // 'count' => 1, + ]; */ +} + +return $config; + +// __END_ diff --git a/phpstan.neon b/phpstan.neon index e7838643..c9f147be 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,6 @@ # PHP Stan Config - +includes: + - phpstan-conditional.php parameters: tmpDir: /tmp/phpstan-corelibs level: 8 # max is now 9 @@ -48,6 +49,9 @@ parameters: - www/admin/class_test.smarty4.php # ignore errores with ignoreErrors: + - # this error is ignore because of the PHP 8.0 to 8.1 change for pg_* + message: "#^Parameter \\#1 \\$(result|connection) of function pg_\\w+ expects resource(\\|null)?, object\\|resource(\\|bool)? given\\.$#" + path: %currentWorkingDirectory%/www/lib/CoreLibs/DB/SQL/PgSQL.php # this is ignored for now # - '#Expression in empty\(\) is always falsy.#' # - diff --git a/www/lib/CoreLibs/DB/IO.php b/www/lib/CoreLibs/DB/IO.php index f94ddc05..845b5945 100644 --- a/www/lib/CoreLibs/DB/IO.php +++ b/www/lib/CoreLibs/DB/IO.php @@ -252,7 +252,8 @@ declare(strict_types=1); namespace CoreLibs\DB; -// this is for running in php 8.0 phan +// below no ignore is needed if we want to use PgSql interface checks with PHP 8.0 +// as main system. Currently all @var sets are written as object /** @#phan-file-suppress PhanUndeclaredTypeProperty,PhanUndeclaredTypeParameter,PhanUndeclaredTypeReturnType */ class IO diff --git a/www/lib/CoreLibs/DB/SQL/PgSQL.php b/www/lib/CoreLibs/DB/SQL/PgSQL.php index f1935c03..abd041a3 100644 --- a/www/lib/CoreLibs/DB/SQL/PgSQL.php +++ b/www/lib/CoreLibs/DB/SQL/PgSQL.php @@ -47,6 +47,8 @@ declare(strict_types=1); namespace CoreLibs\DB\SQL; +// below no ignore is needed if we want to use PgSql interface checks with PHP 8.0 +// as main system. Currently all @var sets are written as object /** @#phan-file-suppress PhanUndeclaredTypeProperty,PhanUndeclaredTypeParameter,PhanUndeclaredTypeReturnType */ class PgSQL @@ -85,7 +87,7 @@ class PgSQL public function __dbQuery(string $query) { $this->last_error_query = ''; - if ($this->dbh === false) { + if ($this->dbh === false || is_bool($this->dbh)) { return false; } // read out the query status and save the query if needed @@ -103,7 +105,7 @@ class PgSQL */ public function __dbSendQuery(string $query): bool { - if ($this->dbh === false) { + if ($this->dbh === false || is_bool($this->dbh)) { return false; } $result = pg_send_query($this->dbh, $query); @@ -117,7 +119,7 @@ class PgSQL public function __dbGetResult() { $this->last_error_query = ''; - if ($this->dbh === false) { + if ($this->dbh === false || is_bool($this->dbh)) { return false; } $result = pg_get_result($this->dbh); @@ -136,7 +138,7 @@ class PgSQL */ public function __dbClose(): void { - if ($this->dbh === false) { + if ($this->dbh === false || is_bool($this->dbh)) { return; } if (pg_connection_status($this->dbh) === PGSQL_CONNECTION_OK) { @@ -153,7 +155,7 @@ class PgSQL */ public function __dbPrepare(string $name, string $query) { - if ($this->dbh === false) { + if ($this->dbh === false || is_bool($this->dbh)) { return false; } $result = pg_prepare($this->dbh, $name, $query); @@ -171,7 +173,7 @@ class PgSQL */ public function __dbExecute(string $name, array $data) { - if ($this->dbh === false) { + if ($this->dbh === false || is_bool($this->dbh)) { return false; } $result = pg_execute($this->dbh, $name, $data); @@ -188,7 +190,7 @@ class PgSQL */ public function __dbNumRows($cursor): int { - if (!$cursor) { + if ($cursor === false || is_bool($cursor)) { return -1; } return pg_num_rows($cursor); @@ -201,7 +203,7 @@ class PgSQL */ public function __dbNumFields($cursor): int { - if (!$cursor) { + if ($cursor === false || is_bool($cursor)) { return -1; } return pg_num_fields($cursor); @@ -215,7 +217,7 @@ class PgSQL */ public function __dbFieldName($cursor, $i) { - if (!$cursor) { + if ($cursor === false || is_bool($cursor)) { return false; } return pg_field_name($cursor, $i); @@ -230,7 +232,7 @@ class PgSQL */ public function __dbFetchArray($cursor, int $result_type = PGSQL_BOTH) { - if (!$cursor) { + if ($cursor === false || is_bool($cursor)) { return false; } // result type is passed on as is [should be checked] @@ -258,7 +260,7 @@ class PgSQL */ public function __dbFetchAll($cursor) { - if (!$cursor) { + if ($cursor === false || is_bool($cursor)) { return false; } return pg_fetch_all($cursor); @@ -271,7 +273,7 @@ class PgSQL */ public function __dbAffectedRows($cursor): int { - if (!$cursor) { + if ($cursor === false || is_bool($cursor)) { return -1; } return pg_affected_rows($cursor); @@ -426,17 +428,18 @@ class PgSQL */ public function __dbPrintError($cursor = false): string { - if ($this->dbh === false) { + if ($this->dbh === false || is_bool($this->dbh)) { return ''; } // run the query again for the error result here - if (!$cursor && $this->last_error_query) { + if (($cursor === false || is_bool($cursor)) && $this->last_error_query) { pg_send_query($this->dbh, $this->last_error_query); $this->last_error_query = ''; $cursor = pg_get_result($this->dbh); } - if ($cursor && pg_result_error($cursor)) { - return "-PostgreSQL-Error-> " . pg_result_error($cursor) . "
"; + if ($cursor && !is_bool($cursor) && $error_str = pg_result_error($cursor)) { + return "-PostgreSQL-Error-> " + . $error_str . "
"; } else { return ''; } @@ -450,7 +453,7 @@ class PgSQL */ public function __dbMetaData(string $table, $extended = false) { - if ($this->dbh === false) { + if ($this->dbh === false || is_bool($this->dbh)) { return false; } // needs to prefixed with @ or it throws a warning on not existing table @@ -464,7 +467,7 @@ class PgSQL */ public function __dbEscapeString($string): string { - if ($this->dbh === false) { + if ($this->dbh === false || is_bool($this->dbh)) { return ''; } return pg_escape_string($this->dbh, (string)$string); @@ -479,7 +482,7 @@ class PgSQL */ public function __dbEscapeLiteral($string): string { - if ($this->dbh === false) { + if ($this->dbh === false || is_bool($this->dbh)) { return ''; } return pg_escape_string($this->dbh, (string)$string); @@ -492,7 +495,7 @@ class PgSQL */ public function __dbEscapeBytea($bytea): string { - if ($this->dbh === false) { + if ($this->dbh === false || is_bool($this->dbh)) { return ''; } return pg_escape_bytea($this->dbh, $bytea); @@ -504,7 +507,7 @@ class PgSQL */ public function __dbConnectionBusy(): bool { - if ($this->dbh === false) { + if ($this->dbh === false || is_bool($this->dbh)) { return false; } return pg_connection_busy($this->dbh); @@ -518,7 +521,7 @@ class PgSQL */ public function __dbVersion(): string { - if ($this->dbh === false) { + if ($this->dbh === false || is_bool($this->dbh)) { return ''; } // array has client, protocol, server @@ -547,7 +550,9 @@ class PgSQL if ('{' != $text[$offset]) { preg_match("/(\\{?\"([^\"\\\\]|\\\\.)*\"|[^,{}]+)+([,}]+)/", $text, $match, 0, $offset); $offset += strlen($match[0]); - $output[] = ('"' != $match[1][0] ? $match[1] : stripcslashes(substr($match[1], 1, -1))); + $output[] = '"' != $match[1][0] ? + $match[1] : + stripcslashes(substr($match[1], 1, -1)); if ('},' == $match[3]) { return $offset; }