Skip to content

Commit

Permalink
Add return type declaration to fetch row methods
Browse files Browse the repository at this point in the history
DatabaseInterface's fetchArray, fetchAssoc, fetchRow and fetchSingleRow
methods.

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Jun 29, 2020
1 parent 4cb24fe commit dfa5f92
Show file tree
Hide file tree
Showing 23 changed files with 96 additions and 355 deletions.
31 changes: 10 additions & 21 deletions libraries/classes/DatabaseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ public function fetchValue(
}

/**
* returns only the first row from the result
* Returns only the first row from the result or null if result is empty.
*
* <code>
* $sql = 'SELECT * FROM `user` WHERE `id` = 123';
Expand All @@ -1276,44 +1276,39 @@ public function fetchValue(
* @param string $type NUM|ASSOC|BOTH returned array should either numeric
* associative or both
* @param int $link link type
*
* @return array|bool first row from result
* or false if result is empty
*/
public function fetchSingleRow(
string $query,
string $type = 'ASSOC',
$link = self::CONNECT_USER
) {
): ?array {
$result = $this->tryQuery(
$query,
$link,
self::QUERY_STORE,
false
);
if ($result === false) {
return false;
return null;
}

// return false if result is empty or false
if (! $this->numRows($result)) {
return false;
return null;
}

switch ($type) {
case 'NUM':
$fetch_function = 'fetchRow';
$row = $this->fetchRow($result);
break;
case 'ASSOC':
$fetch_function = 'fetchAssoc';
$row = $this->fetchAssoc($result);
break;
case 'BOTH':
default:
$fetch_function = 'fetchArray';
$row = $this->fetchArray($result);
break;
}

$row = $this->$fetch_function($result);
$this->freeResult($result);

return $row;
Expand Down Expand Up @@ -1962,10 +1957,8 @@ public function selectDb(string $dbname, $link = self::CONNECT_USER): bool
* returns array of rows with associative and numeric keys from $result
*
* @param object $result result set identifier
*
* @return array|null
*/
public function fetchArray($result)
public function fetchArray($result): ?array
{
return $this->_extension->fetchArray($result);
}
Expand All @@ -1974,10 +1967,8 @@ public function fetchArray($result)
* returns array of rows with associative keys from $result
*
* @param object $result result set identifier
*
* @return array|null
*/
public function fetchAssoc($result)
public function fetchAssoc($result): ?array
{
return $this->_extension->fetchAssoc($result);
}
Expand All @@ -1986,10 +1977,8 @@ public function fetchAssoc($result)
* returns array of rows with numeric keys from $result
*
* @param object $result result set identifier
*
* @return array|null
*/
public function fetchRow($result)
public function fetchRow($result): ?array
{
return $this->_extension->fetchRow($result);
}
Expand Down
19 changes: 5 additions & 14 deletions libraries/classes/Dbal/DbalInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public function fetchValue(
);

/**
* returns only the first row from the result
* Returns only the first row from the result or null if result is empty.
*
* <code>
* $sql = 'SELECT * FROM `user` WHERE `id` = 123';
Expand All @@ -330,15 +330,12 @@ public function fetchValue(
* @param string $type NUM|ASSOC|BOTH returned array should either numeric
* associative or both
* @param int $link link type
*
* @return array|bool first row from result
* or false if result is empty
*/
public function fetchSingleRow(
string $query,
string $type = 'ASSOC',
$link = DatabaseInterface::CONNECT_USER
);
): ?array;

/**
* returns all rows in the resultset in one array
Expand Down Expand Up @@ -548,28 +545,22 @@ public function selectDb(string $dbname, $link = DatabaseInterface::CONNECT_USER
* returns array of rows with associative and numeric keys from $result
*
* @param object $result result set identifier
*
* @return array
*/
public function fetchArray($result);
public function fetchArray($result): ?array;

/**
* returns array of rows with associative keys from $result
*
* @param object $result result set identifier
*
* @return array|bool
*/
public function fetchAssoc($result);
public function fetchAssoc($result): ?array;

/**
* returns array of rows with numeric keys from $result
*
* @param object $result result set identifier
*
* @return array|bool
*/
public function fetchRow($result);
public function fetchRow($result): ?array;

/**
* Adjusts the result pointer to an arbitrary row in the result
Expand Down
12 changes: 3 additions & 9 deletions libraries/classes/Dbal/DbiExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,22 @@ public function realMultiQuery($link, $query);
* returns array of rows with associative and numeric keys from $result
*
* @param object $result result set identifier
*
* @return array|null
*/
public function fetchArray($result);
public function fetchArray($result): ?array;

/**
* returns array of rows with associative keys from $result
*
* @param object $result result set identifier
*
* @return array|null
*/
public function fetchAssoc($result);
public function fetchAssoc($result): ?array;

/**
* returns array of rows with numeric keys from $result
*
* @param object $result result set identifier
*
* @return array|null
*/
public function fetchRow($result);
public function fetchRow($result): ?array;

/**
* Adjusts the result pointer to an arbitrary row in the result
Expand Down
12 changes: 3 additions & 9 deletions libraries/classes/Dbal/DbiMysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,8 @@ public function realMultiQuery($mysqli, $query)
* returns array of rows with associative and numeric keys from $result
*
* @param mysqli_result $result result set identifier
*
* @return array|null
*/
public function fetchArray($result)
public function fetchArray($result): ?array
{
if (! $result instanceof mysqli_result) {
return null;
Expand All @@ -272,10 +270,8 @@ public function fetchArray($result)
* returns array of rows with associative keys from $result
*
* @param mysqli_result $result result set identifier
*
* @return array|null
*/
public function fetchAssoc($result)
public function fetchAssoc($result): ?array
{
if (! $result instanceof mysqli_result) {
return null;
Expand All @@ -288,10 +284,8 @@ public function fetchAssoc($result)
* returns array of rows with numeric keys from $result
*
* @param mysqli_result $result result set identifier
*
* @return array|null
*/
public function fetchRow($result)
public function fetchRow($result): ?array
{
if (! $result instanceof mysqli_result) {
return null;
Expand Down
Loading

0 comments on commit dfa5f92

Please sign in to comment.