diff --git a/src/Command.php b/src/Command.php index 8bc4f82..8e1830d 100644 --- a/src/Command.php +++ b/src/Command.php @@ -565,7 +565,11 @@ class Command { // Set up profiler to track hooks and callbacks $type = null; $focus = null; - if ( $hook ) { + if ( $hook && $callback ) { + // When both are provided, profile all hooks to find the specific callback + $type = 'hook'; + $focus = true; + } elseif ( $hook ) { $type = 'hook'; $focus = $hook; } elseif ( $callback ) { @@ -581,17 +585,22 @@ class Command { if ( $hook || $callback ) { $loggers = $profiler->get_loggers(); foreach ( $loggers as $logger ) { + // Skip if filtering by callback and this logger doesn't have a callback + if ( $callback && ! isset( $logger->callback ) ) { + continue; + } + // Skip if filtering by callback and this isn't the right one if ( $callback && isset( $logger->callback ) ) { // Normalize callback for comparison - $normalized_callback = str_replace( array( '->', '::' ), '', (string) $logger->callback ); - $normalized_filter = str_replace( array( '->', '::' ), '', $callback ); + $normalized_callback = trim((string) $logger->callback); + $normalized_filter = trim($callback); if ( false === stripos( $normalized_callback, $normalized_filter ) ) { continue; } } - // Skip if filtering by hook and this isn't the right one + // Skip if filtering for a specific hook and this isn't the right one if ( $hook && isset( $logger->hook ) && $logger->hook !== $hook ) { continue; } diff --git a/src/Formatter.php b/src/Formatter.php index d09101e..7ea5ef5 100644 --- a/src/Formatter.php +++ b/src/Formatter.php @@ -107,6 +107,7 @@ class Formatter { } $location_index = array_search( 'location', $fields, true ); + $non_numeric_fields = array( 'query', 'caller', 'hook', 'callback' ); foreach ( $items as $item ) { $values = array_values( \WP_CLI\Utils\pick_fields( $item, $fields ) ); foreach ( $values as $i => $value ) { @@ -119,6 +120,11 @@ class Formatter { continue; } + // Ignore non-numeric fields (query, caller, hook, callback) + if ( in_array( $fields[ $i ], $non_numeric_fields, true ) ) { + continue; + } + if ( null === $totals[ $i ] ) { if ( stripos( $fields[ $i ], '_ratio' ) ) { $totals[ $i ] = array(); @@ -131,7 +137,10 @@ class Formatter { $totals[ $i ][] = $value; } } else { - $totals[ $i ] += $value; + // Only add numeric values to prevent warnings + if ( is_numeric( $value ) ) { + $totals[ $i ] += $value; + } } if ( stripos( $fields[ $i ], '_time' ) || 'time' === $fields[ $i ] ) { $values[ $i ] = round( $value, 4 ) . 's'; diff --git a/src/Logger.php b/src/Logger.php index 2048cfe..03c2d4e 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -4,9 +4,9 @@ namespace WP_CLI\Profile; class Logger { - public $time = 0; - public $query_count = 0; - public $query_time = 0; + public $time = 0; + public $query_count = 0; + public $query_time = 0; /** * @var array Array of query indices tracked during this logger's execution. */ @@ -111,6 +111,7 @@ class Logger { $this->query_offset = null; $this->cache_hit_offset = null; $this->cache_miss_offset = null; + $this->query_indices = array(); $key = array_search( $this, self::$active_loggers, true ); if ( false !== $key ) {