Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parameters:
level: 6
paths:
- src
- profile-command.php
scanDirectories:
- vendor/wp-cli/wp-cli/php
scanFiles:
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
- tests/phpstan/scan-files.php

treatPhpDocTypesAsCertain: false
35 changes: 29 additions & 6 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ class Command {
*
* @skipglobalargcheck
* @when before_wp_load
*
* @param array<string> $args
* @param array<string, mixed> $assoc_args
* @return void
*/
public function stage( $args, $assoc_args ) {
global $wpdb;
Expand Down Expand Up @@ -257,6 +261,10 @@ public function stage( $args, $assoc_args ) {
*
* @skipglobalargcheck
* @when before_wp_load
*
* @param array<string> $args
* @param array<string, mixed> $assoc_args
* @return void
*/
public function hook( $args, $assoc_args ) {

Expand Down Expand Up @@ -357,6 +365,10 @@ public function hook( $args, $assoc_args ) {
* | 0.1009s | 100% | 1 |
* +---------+-------------+---------------+
*
* @param array<string> $args
* @param array<string, mixed> $assoc_args
* @return void
*
* @subcommand eval
*/
public function eval_( $args, $assoc_args ) {
Expand Down Expand Up @@ -426,6 +438,10 @@ function () use ( $statement ) {
* | 0.1009s | 100% | 1 |
* +---------+-------------+---------------+
*
* @param array<string> $args
* @param array<string, mixed> $assoc_args
* @return void
*
* @subcommand eval-file
*/
public function eval_file( $args, $assoc_args ) {
Expand All @@ -451,6 +467,12 @@ function () use ( $file ) {

/**
* Profile an eval or eval-file statement.
*
* @param array<string, mixed> $assoc_args
* @param callable $profile_callback
* @param string $order
* @param string|null $orderby
* @return void
*/
private static function profile_eval_ish( $assoc_args, $profile_callback, $order = 'ASC', $orderby = null ) {
$hook = Utils\get_flag_value( $assoc_args, 'hook' );
Expand Down Expand Up @@ -500,6 +522,7 @@ private static function profile_eval_ish( $assoc_args, $profile_callback, $order
* Include a file without exposing it to current scope
*
* @param string $file
* @return void
*/
private static function include_file( $file ) {
include $file;
Expand All @@ -508,9 +531,9 @@ private static function include_file( $file ) {
/**
* Filter loggers with zero-ish values.
*
* @param array $loggers
* @param array $metrics
* @return array
* @param array<\WP_CLI\Profile\Logger> $loggers
* @param array<string> $metrics
* @return array<\WP_CLI\Profile\Logger>
*/
private static function shine_spotlight( $loggers, $metrics ) {

Expand Down Expand Up @@ -550,9 +573,9 @@ private static function shine_spotlight( $loggers, $metrics ) {
/**
* Filter loggers to only those whose callback name matches a pattern.
*
* @param array $loggers
* @param string $pattern
* @return array
* @param array<\WP_CLI\Profile\Logger> $loggers
* @param string $pattern
* @return array<\WP_CLI\Profile\Logger>
*/
private static function filter_by_callback( $loggers, $pattern ) {
return array_filter(
Expand Down
41 changes: 33 additions & 8 deletions src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@

class Formatter {

/**
* @var \WP_CLI\Formatter
*/
private $formatter;

/**
* @var array<string, mixed>
*/
private $args;

/**
* @var int|null
*/
private $total_cell_index;

/**
* Formatter constructor.
*
* @param array<mixed> $assoc_args
* @param array<string>|null $fields
* @param string|bool $prefix
*/
public function __construct( &$assoc_args, $fields = null, $prefix = false ) {
$format_args = array(
'format' => 'table',
Expand Down Expand Up @@ -51,7 +67,11 @@ public function __construct( &$assoc_args, $fields = null, $prefix = false ) {
/**
* Display multiple items according to the output arguments.
*
* @param array $items
* @param array<\WP_CLI\Profile\Logger> $items
* @param bool $include_total
* @param string $order
* @param string|null $orderby
* @return void
*/
public function display_items( $items, $include_total, $order, $orderby ) {
if ( 'table' === $this->args['format'] && empty( $this->args['field'] ) ) {
Expand All @@ -64,13 +84,14 @@ public function display_items( $items, $include_total, $order, $orderby ) {
/**
* Function to compare floats.
*
* @param double $a Floating number.
* @param double $b Floating number.
* @param float $a Floating number.
* @param float $b Floating number.
* @return int
*/
private function compare_float( $a, $b ) {
$a = number_format( $a, 4 );
$b = number_format( $b, 4 );
if ( 0 === $a - $b ) {
$a = round( $a, 4 );
$b = round( $b, 4 );
if ( 0.0 === $a - $b ) {
return 0;
} elseif ( $a - $b < 0 ) {
return -1;
Comment on lines +94 to 97
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The subtraction $a - $b is performed multiple times. It is more efficient to calculate the difference once and reuse it.

		$diff = $a - $b;
		if ( 0.0 === $diff ) {
			return 0;
		} elseif ( $diff < 0 ) {
			return -1;

Expand All @@ -82,8 +103,12 @@ private function compare_float( $a, $b ) {
/**
* Show items in a \cli\Table.
*
* @param array $items
* @param array $fields
* @param string $order
* @param string|null $orderby
* @param array<\WP_CLI\Profile\Logger> $items
* @param array<string> $fields
* @param bool $include_total
* @return void
*/
private function show_table( $order, $orderby, $items, $fields, $include_total ) {
$table = new \cli\Table();
Expand Down
95 changes: 79 additions & 16 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,72 @@

namespace WP_CLI\Profile;

/**
* Logger class.
*
* @property string $callback
* @property string $location
*/
class Logger {

public $time = 0;
public $query_count = 0;
public $query_time = 0;
public $cache_hits = 0;
public $cache_misses = 0;
public $cache_ratio = null;
public $hook_count = 0;
public $hook_time = 0;
public $request_count = 0;
public $request_time = 0;
private $start_time = null;
private $query_offset = null;
private $cache_hit_offset = null;
private $cache_miss_offset = null;
private $hook_start_time = null;
private $hook_depth = 0;
/** @var float */
public $time = 0;
/** @var int */
public $query_count = 0;
/** @var float */
public $query_time = 0;
/** @var int */
public $cache_hits = 0;
/** @var int */
public $cache_misses = 0;
/** @var string|null */
public $cache_ratio = null;
/** @var int */
public $hook_count = 0;
/** @var float */
public $hook_time = 0;
/** @var int */
public $request_count = 0;
/** @var float */
public $request_time = 0;
/** @var float|null */
private $start_time = null;
/** @var int|null */
private $query_offset = null;
/** @var int|null */
private $cache_hit_offset = null;
/** @var int|null */
private $cache_miss_offset = null;
/** @var float|null */
private $hook_start_time = null;
/** @var int */
private $hook_depth = 0;
/** @var float|null */
private $request_start_time = null;

/** @var array<string, mixed> */
private $definitions = array();

/** @var array<\WP_CLI\Profile\Logger> */
public static $active_loggers = array();

/**
* Logger constructor.
*
* @param array<string, mixed> $definition
*/
public function __construct( $definition = array() ) {
foreach ( $definition as $k => $v ) {
$this->definitions[ $k ] = $v;
}
}

/**
* Magic getter for definitions.
*
* @param string $key
* @return mixed
*/
public function __get( $key ) {
if ( isset( $this->definitions[ $key ] ) ) {
return $this->definitions[ $key ];
Expand All @@ -40,16 +76,31 @@ public function __get( $key ) {
return null;
}

/**
* Magic setter for definitions.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function __set( $key, $value ) {
$this->definitions[ $key ] = $value;
}

/**
* Magic isset for definitions.
*
* @param string $key
* @return bool
*/
public function __isset( $key ) {
return isset( $this->definitions[ $key ] );
}

/**
* Start this logger
*
* @return void
*/
public function start() {
global $wpdb, $wp_object_cache;
Expand All @@ -66,13 +117,17 @@ public function start() {

/**
* Whether or not the logger is running
*
* @return bool
*/
public function running() {
return ! is_null( $this->start_time );
}

/**
* Stop this logger
*
* @return void
*/
public function stop() {
global $wpdb, $wp_object_cache;
Expand Down Expand Up @@ -115,6 +170,8 @@ public function stop() {

/**
* Start this logger's hook timer
*
* @return void
*/
public function start_hook_timer() {
++$this->hook_count;
Expand All @@ -128,6 +185,8 @@ public function start_hook_timer() {

/**
* Stop this logger's hook timer
*
* @return void
*/
public function stop_hook_timer() {
if ( $this->hook_depth ) {
Expand All @@ -142,6 +201,8 @@ public function stop_hook_timer() {

/**
* Start this logger's request timer
*
* @return void
*/
public function start_request_timer() {
++$this->request_count;
Expand All @@ -150,6 +211,8 @@ public function start_request_timer() {

/**
* Stop this logger's request timer
*
* @return void
*/
public function stop_request_timer() {
if ( ! is_null( $this->request_start_time ) ) {
Expand Down
Loading
Loading