Skip to content

Commit

Permalink
Merge pull request #17979 from kamil-tekiela/Optimize-fetchResult
Browse files Browse the repository at this point in the history
Microoptimization of fetchResult()

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Dec 24, 2022
2 parents f1a0f9d + fd52b85 commit d5f3c91
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 30 deletions.
46 changes: 22 additions & 24 deletions libraries/classes/DatabaseInterface.php
Expand Up @@ -25,6 +25,7 @@
use stdClass;

use function __;
use function array_column;
use function array_diff;
use function array_keys;
use function array_map;
Expand Down Expand Up @@ -1293,11 +1294,7 @@ private function fetchValueOrValueByIndex($row, $value)
*/
private function fetchByMode(ResultInterface $result, string $mode): array
{
if ($mode === self::FETCH_NUM) {
return $result->fetchRow();
}

return $result->fetchAssoc();
return $mode === self::FETCH_NUM ? $result->fetchRow() : $result->fetchAssoc();
}

/**
Expand Down Expand Up @@ -1364,23 +1361,20 @@ public function fetchResult(

// return empty array if result is empty or false
if ($result === false) {
return $resultRows;
return [];
}

$fetchFunction = self::FETCH_ASSOC;

if ($key === null) {
// no nested array if only one field is in result
if ($result->numFields() === 1) {
$value = 0;
$fetchFunction = self::FETCH_NUM;
if ($value === 0 || $result->numFields() === 1) {
return $result->fetchAllColumn();
}

while ($row = $this->fetchByMode($result, $fetchFunction)) {
$resultRows[] = $this->fetchValueOrValueByIndex($row, $value);
}
} elseif (is_array($key)) {
while ($row = $this->fetchByMode($result, $fetchFunction)) {
return $value === null ? $result->fetchAllAssoc() : array_column($result->fetchAllAssoc(), $value);
}

if (is_array($key)) {
while ($row = $result->fetchAssoc()) {
$resultTarget =& $resultRows;
foreach ($key as $keyIndex) {
if ($keyIndex === null) {
Expand All @@ -1397,15 +1391,19 @@ public function fetchResult(

$resultTarget = $this->fetchValueOrValueByIndex($row, $value);
}
} else {
// if $key is an integer use non associative mysql fetch function
if (is_int($key)) {
$fetchFunction = self::FETCH_NUM;
}

while ($row = $this->fetchByMode($result, $fetchFunction)) {
$resultRows[$row[$key]] = $this->fetchValueOrValueByIndex($row, $value);
}
return $resultRows;
}

if ($key === 0 && $value === 1) {
return $result->fetchAllKeyPair();
}

// if $key is an integer use non associative mysql fetch function
$fetchFunction = is_int($key) ? self::FETCH_NUM : self::FETCH_ASSOC;

while ($row = $this->fetchByMode($result, $fetchFunction)) {
$resultRows[$row[$key]] = $this->fetchValueOrValueByIndex($row, $value);
}

return $resultRows;
Expand Down
12 changes: 6 additions & 6 deletions psalm-baseline.xml
Expand Up @@ -5675,22 +5675,18 @@
<code>$tableData['Engine']</code>
<code>$tableData['Index_length']</code>
</MixedArrayAccess>
<MixedArrayOffset occurrences="7">
<MixedArrayOffset occurrences="4">
<code>$resultRows[$row[$key]]</code>
<code>$resultTarget[$row[$keyIndex]]</code>
<code>$resultTarget[$row[$keyIndex]]</code>
<code>$resultTarget[$row[$keyIndex]]</code>
<code>$row[$keyIndex]</code>
<code>$row[$keyIndex]</code>
<code>$row[$keyIndex]</code>
</MixedArrayOffset>
<MixedAssignment occurrences="14">
<MixedAssignment occurrences="13">
<code>$aLength</code>
<code>$bLength</code>
<code>$keyIndex</code>
<code>$map['real_column']</code>
<code>$resultRows[$row[$key]]</code>
<code>$resultRows[]</code>
<code>$resultTarget</code>
<code>$table</code>
<code>$tableData</code>
Expand Down Expand Up @@ -5728,6 +5724,10 @@
<PossiblyInvalidArrayOffset occurrences="1">
<code>$row[$value]</code>
</PossiblyInvalidArrayOffset>
<PossiblyNullArrayOffset occurrences="2">
<code>$resultTarget</code>
<code>$resultTarget</code>
</PossiblyNullArrayOffset>
<PossiblyNullOperand occurrences="7">
<code>$row['Data_free']</code>
<code>$row['Data_length']</code>
Expand Down

0 comments on commit d5f3c91

Please sign in to comment.