Compare commits

..

4 commits

Author SHA1 Message Date
Pascal Birchler
cc09ca2f50
Update src/Command.php
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-10 22:47:09 +01:00
copilot-swe-agent[bot]
1f255b9f5e Address code review feedback: fix callback filtering and reset query_indices
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
2025-11-10 21:28:20 +00:00
copilot-swe-agent[bot]
28e1e4d616 Fix PHP warnings for non-numeric values in Formatter
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
2025-11-10 17:54:20 +00:00
Pascal Birchler
ec1564a4d0
Lint fix 2025-11-10 16:38:58 +01:00
3 changed files with 27 additions and 8 deletions

View file

@ -565,7 +565,11 @@ class Command {
// Set up profiler to track hooks and callbacks // Set up profiler to track hooks and callbacks
$type = null; $type = null;
$focus = 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'; $type = 'hook';
$focus = $hook; $focus = $hook;
} elseif ( $callback ) { } elseif ( $callback ) {
@ -581,17 +585,22 @@ class Command {
if ( $hook || $callback ) { if ( $hook || $callback ) {
$loggers = $profiler->get_loggers(); $loggers = $profiler->get_loggers();
foreach ( $loggers as $logger ) { 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 // Skip if filtering by callback and this isn't the right one
if ( $callback && isset( $logger->callback ) ) { if ( $callback && isset( $logger->callback ) ) {
// Normalize callback for comparison // Normalize callback for comparison
$normalized_callback = str_replace( array( '->', '::' ), '', (string) $logger->callback ); $normalized_callback = trim((string) $logger->callback);
$normalized_filter = str_replace( array( '->', '::' ), '', $callback ); $normalized_filter = trim($callback);
if ( false === stripos( $normalized_callback, $normalized_filter ) ) { if ( false === stripos( $normalized_callback, $normalized_filter ) ) {
continue; 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 ) { if ( $hook && isset( $logger->hook ) && $logger->hook !== $hook ) {
continue; continue;
} }

View file

@ -107,6 +107,7 @@ class Formatter {
} }


$location_index = array_search( 'location', $fields, true ); $location_index = array_search( 'location', $fields, true );
$non_numeric_fields = array( 'query', 'caller', 'hook', 'callback' );
foreach ( $items as $item ) { foreach ( $items as $item ) {
$values = array_values( \WP_CLI\Utils\pick_fields( $item, $fields ) ); $values = array_values( \WP_CLI\Utils\pick_fields( $item, $fields ) );
foreach ( $values as $i => $value ) { foreach ( $values as $i => $value ) {
@ -119,6 +120,11 @@ class Formatter {
continue; continue;
} }


// Ignore non-numeric fields (query, caller, hook, callback)
if ( in_array( $fields[ $i ], $non_numeric_fields, true ) ) {
continue;
}

if ( null === $totals[ $i ] ) { if ( null === $totals[ $i ] ) {
if ( stripos( $fields[ $i ], '_ratio' ) ) { if ( stripos( $fields[ $i ], '_ratio' ) ) {
$totals[ $i ] = array(); $totals[ $i ] = array();
@ -131,7 +137,10 @@ class Formatter {
$totals[ $i ][] = $value; $totals[ $i ][] = $value;
} }
} else { } 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 ] ) { if ( stripos( $fields[ $i ], '_time' ) || 'time' === $fields[ $i ] ) {
$values[ $i ] = round( $value, 4 ) . 's'; $values[ $i ] = round( $value, 4 ) . 's';

View file

@ -4,9 +4,9 @@ namespace WP_CLI\Profile;


class Logger { class Logger {


public $time = 0; public $time = 0;
public $query_count = 0; public $query_count = 0;
public $query_time = 0; public $query_time = 0;
/** /**
* @var array Array of query indices tracked during this logger's execution. * @var array Array of query indices tracked during this logger's execution.
*/ */
@ -111,6 +111,7 @@ class Logger {
$this->query_offset = null; $this->query_offset = null;
$this->cache_hit_offset = null; $this->cache_hit_offset = null;
$this->cache_miss_offset = null; $this->cache_miss_offset = null;
$this->query_indices = array();
$key = array_search( $this, self::$active_loggers, true ); $key = array_search( $this, self::$active_loggers, true );


if ( false !== $key ) { if ( false !== $key ) {