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: 9
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
120 changes: 91 additions & 29 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,22 @@ 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;

$focus = Utils\get_flag_value( $assoc_args, 'all', isset( $args[0] ) ? $args[0] : null );
/** @var array<string, bool|string> $typed_assoc_args */
$typed_assoc_args = self::get_typed_assoc_args( $assoc_args );
$focus = Utils\get_flag_value( $typed_assoc_args, 'all', isset( $args[0] ) ? $args[0] : null );

$order = Utils\get_flag_value( $assoc_args, 'order', 'ASC' );
$orderby = Utils\get_flag_value( $assoc_args, 'orderby', null );
$order_val = Utils\get_flag_value( $typed_assoc_args, 'order', 'ASC' );
$order = is_string( $order_val ) ? $order_val : 'ASC';
$orderby_val = Utils\get_flag_value( $typed_assoc_args, 'orderby', null );
$orderby = ( is_string( $orderby_val ) || is_null( $orderby_val ) ) ? $orderby_val : null;

$valid_stages = array( 'bootstrap', 'main_query', 'template' );
if ( $focus && ( true !== $focus && ! in_array( $focus, $valid_stages, true ) ) ) {
Expand Down Expand Up @@ -179,9 +187,10 @@ public function stage( $args, $assoc_args ) {
);
}
$fields = array_merge( $base, $metrics );
$formatter = new Formatter( $assoc_args, $fields );
$formatter = new Formatter( $typed_assoc_args, $fields );
$loggers = $profiler->get_loggers();
if ( Utils\get_flag_value( $assoc_args, 'spotlight' ) ) {
/** @var array<string, bool|string> $typed_assoc_args */
if ( Utils\get_flag_value( $typed_assoc_args, 'spotlight' ) ) {
$loggers = self::shine_spotlight( $loggers, $metrics );
}

Expand Down Expand Up @@ -257,13 +266,21 @@ 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 ) {

$focus = Utils\get_flag_value( $assoc_args, 'all', isset( $args[0] ) ? $args[0] : null );
/** @var array<string, bool|string> $typed_assoc_args */
$typed_assoc_args = self::get_typed_assoc_args( $assoc_args );
$focus = Utils\get_flag_value( $typed_assoc_args, 'all', isset( $args[0] ) ? $args[0] : null );

$order = Utils\get_flag_value( $assoc_args, 'order', 'ASC' );
$orderby = Utils\get_flag_value( $assoc_args, 'orderby', null );
$order_val = Utils\get_flag_value( $typed_assoc_args, 'order', 'ASC' );
$order = is_string( $order_val ) ? $order_val : 'ASC';
$orderby_val = Utils\get_flag_value( $typed_assoc_args, 'orderby', null );
$orderby = ( is_string( $orderby_val ) || is_null( $orderby_val ) ) ? $orderby_val : null;

$profiler = new Profiler( 'hook', $focus );
$profiler->run();
Expand Down Expand Up @@ -291,13 +308,16 @@ public function hook( $args, $assoc_args ) {
'request_count',
);
$fields = array_merge( $base, $metrics );
$formatter = new Formatter( $assoc_args, $fields );
$formatter = new Formatter( $typed_assoc_args, $fields );
$loggers = $profiler->get_loggers();
if ( Utils\get_flag_value( $assoc_args, 'spotlight' ) ) {
/** @var array<string, bool|string> $typed_assoc_args */
if ( Utils\get_flag_value( $typed_assoc_args, 'spotlight' ) ) {
$loggers = self::shine_spotlight( $loggers, $metrics );
}
$search = Utils\get_flag_value( $assoc_args, 'search', false );
if ( false !== $search && '' !== $search ) {
/** @var array<string, bool|string> $typed_assoc_args */
$search_val = Utils\get_flag_value( $typed_assoc_args, 'search', '' );
$search = is_string( $search_val ) ? $search_val : '';
if ( '' !== $search ) {
if ( ! $focus ) {
WP_CLI::error( '--search requires --all or a specific hook.' );
}
Expand Down Expand Up @@ -357,16 +377,24 @@ 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 ) {
$statement = $args[0];

$order = Utils\get_flag_value( $assoc_args, 'order', 'ASC' );
$orderby = Utils\get_flag_value( $assoc_args, 'orderby', null );
/** @var array<string, bool|string> $typed_assoc_args */
$typed_assoc_args = self::get_typed_assoc_args( $assoc_args );
$order_val = Utils\get_flag_value( $typed_assoc_args, 'order', 'ASC' );
$order = is_string( $order_val ) ? $order_val : 'ASC';
$orderby_val = Utils\get_flag_value( $typed_assoc_args, 'orderby', null );
$orderby = ( is_string( $orderby_val ) || is_null( $orderby_val ) ) ? $orderby_val : null;

self::profile_eval_ish(
$assoc_args,
$typed_assoc_args,
function () use ( $statement ) {
eval( $statement ); // phpcs:ignore Squiz.PHP.Eval.Discouraged -- no other way around here
},
Expand Down Expand Up @@ -426,21 +454,30 @@ 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 ) {

$file = $args[0];

$order = Utils\get_flag_value( $assoc_args, 'order', 'ASC' );
$orderby = Utils\get_flag_value( $assoc_args, 'orderby', null );
/** @var array<string, bool|string> $typed_assoc_args */
/** @var array<string, bool|string> $typed_assoc_args */
$typed_assoc_args = self::get_typed_assoc_args( $assoc_args );
$order_val = Utils\get_flag_value( $typed_assoc_args, 'order', 'ASC' );
$order = is_string( $order_val ) ? $order_val : 'ASC';
$orderby_val = Utils\get_flag_value( $typed_assoc_args, 'orderby', null );
$orderby = ( is_string( $orderby_val ) || is_null( $orderby_val ) ) ? $orderby_val : null;

if ( ! file_exists( $file ) ) {
WP_CLI::error( "'$file' does not exist." );
}

self::profile_eval_ish(
$assoc_args,
$typed_assoc_args,
function () use ( $file ) {
self::include_file( $file );
},
Expand All @@ -451,12 +488,20 @@ 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' );
$focus = false;
$type = false;
$fields = array();
/** @var array<string, bool|string> $typed_assoc_args */
$typed_assoc_args = self::get_typed_assoc_args( $assoc_args );
$hook = Utils\get_flag_value( $typed_assoc_args, 'hook' );
$focus = false;
$type = false;
$fields = array();
if ( $hook ) {
$type = 'hook';
if ( true !== $hook ) {
Expand Down Expand Up @@ -492,14 +537,15 @@ private static function profile_eval_ish( $assoc_args, $profile_callback, $order
'request_count',
)
);
$formatter = new Formatter( $assoc_args, $fields );
$formatter = new Formatter( $typed_assoc_args, $fields );
$formatter->display_items( $loggers, false, $order, $orderby );
}

/**
* 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 +554,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 +596,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 All @@ -562,4 +608,20 @@ function ( $logger ) use ( $pattern ) {
}
);
}

/**
* Get typed assoc args for get_flag_value.
*
* @param array<string, mixed> $assoc_args
* @return array<string, bool|string>
*/
private static function get_typed_assoc_args( $assoc_args ) {
$typed = array();
foreach ( $assoc_args as $k => $v ) {
if ( is_bool( $v ) || is_string( $v ) ) {
$typed[ $k ] = $v;
}
}
return $typed;
}
}
Loading
Loading