Skip to content

Commit 4a8aa77

Browse files
committed
Add type checking to template-db-queries
1 parent 19cc2e7 commit 4a8aa77

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

plugins/performance-lab/includes/server-timing/defaults.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,19 +192,35 @@ static function (): void {
192192
array(
193193
'measure_callback' => static function ( $metric ): void {
194194
// This global should typically be set when this is called, but check just in case.
195-
if ( ! isset( $GLOBALS['perflab_query_time_before_template'] ) ) {
195+
if ( ! isset( $GLOBALS['perflab_query_time_before_template'] ) || ! is_float( $GLOBALS['perflab_query_time_before_template'] ) ) {
196196
return;
197197
}
198198

199199
// This should never happen, but some odd database implementations may be doing it wrong.
200200
if ( ! isset( $GLOBALS['wpdb']->queries ) || ! is_array( $GLOBALS['wpdb']->queries ) ) {
201+
// A notice is already emitted above, but if $perflab_query_time_before_template was not
202+
// set, then this condition wouldn't be checked in the first place.
201203
return;
202204
}
203205

206+
/**
207+
* Query times.
208+
*
209+
* @var float[] $query_times
210+
*/
211+
$query_times = array();
212+
foreach ( $GLOBALS['wpdb']->queries as $query ) {
213+
if ( ! is_array( $query ) || ! isset( $query[1] ) || ! is_float( $query[1] ) ) {
214+
// A notice is already emitted above.
215+
return;
216+
}
217+
$query_times[] = $query[1];
218+
}
219+
204220
$total_query_time = array_reduce(
205-
$GLOBALS['wpdb']->queries,
206-
static function ( $acc, $query ) {
207-
return $acc + $query[1];
221+
$query_times,
222+
static function ( float $acc, float $query_time ): float {
223+
return $acc + $query_time;
208224
},
209225
0.0
210226
);

0 commit comments

Comments
 (0)