From 478c4892f6f8172ff0c5fb6b30ebea115a2356ef Mon Sep 17 00:00:00 2001 From: Umherirrender Date: Wed, 9 Mar 2022 20:28:00 +0100 Subject: [PATCH] database: Fix various scalar and null types Fix return values to match documentation Provide fallback if no null value is allowed Return from inside of try/finally makes it clear for static analyzer Change-Id: I8bdfa453fc56dc50749cbd324b3d0d36e8e7065e --- includes/libs/rdbms/database/Database.php | 12 +++++------- includes/libs/rdbms/database/DatabasePostgres.php | 10 ++++++---- includes/libs/rdbms/database/IDatabase.php | 6 +++--- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/includes/libs/rdbms/database/Database.php b/includes/libs/rdbms/database/Database.php index dae87b82abc..eddba36ccaf 100644 --- a/includes/libs/rdbms/database/Database.php +++ b/includes/libs/rdbms/database/Database.php @@ -105,7 +105,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware protected $flags; /** @var array Current LoadBalancer tracking information */ protected $lbInfo = []; - /** @var string Current SQL query delimiter */ + /** @var string|false Current SQL query delimiter */ protected $delimiter = ';'; /** @var array[] Current map of (table => (dbname, schema, prefix) map) */ protected $tableAliases = []; @@ -2313,7 +2313,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware $indexInfo = $this->indexInfo( $table, $index, $fname ); if ( !$indexInfo ) { - return null; + return false; } return !$indexInfo[0]->Non_unique; @@ -2687,7 +2687,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware ) { $fld = "GROUP_CONCAT($field SEPARATOR " . $this->addQuotes( $delim ) . ')'; - return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')'; + return '(' . $this->selectSQLText( $table, $fld, $conds, __METHOD__, [], $join_conds ) . ')'; } /** @@ -2893,7 +2893,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware } public function getServerName() { - return $this->serverName ?? $this->getServer(); + return $this->serverName ?? $this->getServer() ?? 'unknown'; } /** @@ -4885,7 +4885,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware } try { - $error = $this->sourceStream( + return $this->sourceStream( $fp, $lineCallback, $resultCallback, @@ -4895,8 +4895,6 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware } finally { fclose( $fp ); } - - return $error; } public function setSchemaVars( $vars ) { diff --git a/includes/libs/rdbms/database/DatabasePostgres.php b/includes/libs/rdbms/database/DatabasePostgres.php index 4e18a288f22..b814f3134a0 100644 --- a/includes/libs/rdbms/database/DatabasePostgres.php +++ b/includes/libs/rdbms/database/DatabasePostgres.php @@ -265,10 +265,12 @@ class DatabasePostgres extends Database { public function lastErrno() { if ( $this->lastResultHandle ) { - return pg_result_error_field( $this->lastResultHandle, PGSQL_DIAG_SQLSTATE ); - } else { - return false; + $lastErrno = pg_result_error_field( $this->lastResultHandle, PGSQL_DIAG_SQLSTATE ); + if ( $lastErrno !== false ) { + return (int)$lastErrno; + } } + return 0; } protected function fetchAffectedRowCount() { @@ -407,7 +409,7 @@ __INDEXATTR__; ")'"; $res = $this->query( $sql, $fname, $flags ); if ( !$res ) { - return null; + return false; } return $res->numRows() > 0; diff --git a/includes/libs/rdbms/database/IDatabase.php b/includes/libs/rdbms/database/IDatabase.php index c674fd0ec56..866fb7e84f3 100644 --- a/includes/libs/rdbms/database/IDatabase.php +++ b/includes/libs/rdbms/database/IDatabase.php @@ -271,7 +271,7 @@ interface IDatabase { * Keys matching the IDatabase::LB_* constants are also used internally by subclasses * * @param array|string $nameOrArray The new array or the name of a key to set - * @param array|null $value If $nameOrArray is a string, the new key value (null to unset) + * @param array|mixed|null $value If $nameOrArray is a string, the new key value (null to unset) */ public function setLBInfo( $nameOrArray, $value = null ); @@ -293,7 +293,7 @@ interface IDatabase { /** * Get the last time the connection may have been used for a write query * - * @return int|float UNIX timestamp or false + * @return int|float|false UNIX timestamp or false * @since 1.24 */ public function lastDoneWrites(); @@ -2190,7 +2190,7 @@ interface IDatabase { * @param string $method Name of the calling method * @param int $timeout Acquisition timeout in seconds (0 means non-blocking) * @param int $flags Bit field of IDatabase::LOCK_* constants - * @return bool Success + * @return bool|float Success * @throws DBError If an error occurs, {@see query} */ public function lock( $lockName, $method, $timeout = 5, $flags = 0 );