From a789b3ce344fe15dbba246c8fed10da7ad0c9f52 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Thu, 8 Sep 2016 06:15:10 -0700 Subject: [PATCH] Fix bottom of the table when there aren't any items displayed The cells need to be filled in appropriately --- features/profile.feature | 9 +++++++++ inc/class-formatter.php | 10 ++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/features/profile.feature b/features/profile.feature index 31d3490..832cc55 100644 --- a/features/profile.feature +++ b/features/profile.feature @@ -28,3 +28,12 @@ Feature: Basic profile usage """ Error: 'SAVEQUERIES' is defined as false, and must be true. Please check your wp-config.php """ + + Scenario: Profile a hook without any callbacks + Given a WP install + + When I run `wp profile --hook=setup_theme --fields=callback,time` + Then STDOUT should be a table containing rows: + | callback | time | + | total | | + And STDERR should be empty diff --git a/inc/class-formatter.php b/inc/class-formatter.php index eddca3d..12901d1 100644 --- a/inc/class-formatter.php +++ b/inc/class-formatter.php @@ -60,16 +60,15 @@ class Formatter { $table->setHeaders( $fields ); - $totals = array( - 'total', - ); + $totals = array_fill( 0, count( $fields ), null ); + $totals[0] = 'total'; foreach ( $items as $item ) { $values = array_values( \WP_CLI\Utils\pick_fields( $item, $fields ) ); foreach( $values as $i => $value ) { if ( 0 === $i ) { continue; } - if ( ! isset( $totals[ $i ] ) ) { + if ( null === $totals[ $i ] ) { if ( stripos( $fields[ $i ], '_ratio' ) ) { $totals[ $i ] = array(); } else { @@ -90,6 +89,9 @@ class Formatter { $table->addRow( $values ); } foreach( $totals as $i => $value ) { + if ( null === $value ) { + continue; + } if ( stripos( $fields[ $i ], '_time' ) || 'time' === $fields[ $i ] ) { $totals[ $i ] = round( $value, 4 ) . 's'; }