Skip to content

Commit ea5bfc4

Browse files
authored
Add initial values to traceable PDO array_reduce() (php-debugbar#523)
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 php-debugbar#522.
1 parent 17dcf3f commit ea5bfc4

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/DebugBar/DataCollector/PDO/TraceablePDO.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public function addExecutedStatement(TracedStatement $stmt) : void
248248
*/
249249
public function getAccumulatedStatementsDuration() : float
250250
{
251-
return array_reduce($this->executedStatements, function ($v, $s) { return $v + $s->getDuration(); });
251+
return array_reduce($this->executedStatements, function ($v, $s) { return $v + $s->getDuration(); }, 0.0);
252252
}
253253

254254
/**
@@ -258,7 +258,7 @@ public function getAccumulatedStatementsDuration() : float
258258
*/
259259
public function getMemoryUsage() : int
260260
{
261-
return array_reduce($this->executedStatements, function ($v, $s) { return $v + $s->getMemoryUsage(); });
261+
return array_reduce($this->executedStatements, function ($v, $s) { return $v + $s->getMemoryUsage(); }, 0);
262262
}
263263

264264
/**
@@ -268,7 +268,7 @@ public function getMemoryUsage() : int
268268
*/
269269
public function getPeakMemoryUsage() : int
270270
{
271-
return array_reduce($this->executedStatements, function ($v, $s) { $m = $s->getEndMemory(); return $m > $v ? $m : $v; });
271+
return array_reduce($this->executedStatements, function ($v, $s) { $m = $s->getEndMemory(); return $m > $v ? $m : $v; }, 0);
272272
}
273273

274274
/**

0 commit comments

Comments
 (0)