Skip to content

Commit

Permalink
Add initial values to traceable PDO array_reduce() (#523)
Browse files Browse the repository at this point in the history
If the PDOTraceableDataCollector is enabled and no database calls
happen to be made in a request, PHP 8.1 emits a fatal error, e.g.,

```
<b>Fatal error</b>: Uncaught TypeError:
DebugBar\DataCollector\PDO\TraceablePDO::getAccumulatedStatementsDuration():
Return value must be of type float, null returned in
vendor/maximebf/debugbar/src/DebugBar/DataCollector/PDO/TraceablePDO.php:251
```

The same error occurs in getMemoryUsage() and getPeakMemoryUsage().

Adding an initial value of 0 to `array_reduce()` corrects the problem.

Corrects bug #522.
  • Loading branch information
kstephensop authored Feb 6, 2023
1 parent 17dcf3f commit ea5bfc4
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/DebugBar/DataCollector/PDO/TraceablePDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public function addExecutedStatement(TracedStatement $stmt) : void
*/
public function getAccumulatedStatementsDuration() : float
{
return array_reduce($this->executedStatements, function ($v, $s) { return $v + $s->getDuration(); });
return array_reduce($this->executedStatements, function ($v, $s) { return $v + $s->getDuration(); }, 0.0);
}

/**
Expand All @@ -258,7 +258,7 @@ public function getAccumulatedStatementsDuration() : float
*/
public function getMemoryUsage() : int
{
return array_reduce($this->executedStatements, function ($v, $s) { return $v + $s->getMemoryUsage(); });
return array_reduce($this->executedStatements, function ($v, $s) { return $v + $s->getMemoryUsage(); }, 0);
}

/**
Expand All @@ -268,7 +268,7 @@ public function getMemoryUsage() : int
*/
public function getPeakMemoryUsage() : int
{
return array_reduce($this->executedStatements, function ($v, $s) { $m = $s->getEndMemory(); return $m > $v ? $m : $v; });
return array_reduce($this->executedStatements, function ($v, $s) { $m = $s->getEndMemory(); return $m > $v ? $m : $v; }, 0);
}

/**
Expand Down

0 comments on commit ea5bfc4

Please sign in to comment.