Merge pull request #157 from BhargavBhandari90/issue-156

This commit is contained in:
Alain Schlesser 2020-06-04 16:17:16 +02:00 committed by GitHub
commit 9efe45c72f
8 changed files with 105 additions and 78 deletions

View file

@ -5,6 +5,8 @@
.editorconfig .editorconfig
.travis.yml .travis.yml
circle.yml circle.yml
phpcs.xml.dist
phpunit.xml.dist
bin/ bin/
features/ features/
utils/ utils/

3
.gitignore vendored
View file

@ -6,3 +6,6 @@ vendor/
*.zip *.zip
*.tar.gz *.tar.gz
*.log *.log
phpunit.xml
phpcs.xml
.phpcs.xml

View file

@ -10,7 +10,7 @@
"wp-cli/wp-cli": "^2" "wp-cli/wp-cli": "^2"
}, },
"require-dev": { "require-dev": {
"wp-cli/wp-cli-tests": "^2.0.7" "wp-cli/wp-cli-tests": "^2.1"
}, },
"config": { "config": {
"process-timeout": 7200, "process-timeout": 7200,

View file

@ -368,7 +368,8 @@ class Command {
*/ */
private static function profile_eval_ish( $assoc_args, $profile_callback, $order = 'ASC', $orderby = null ) { private static function profile_eval_ish( $assoc_args, $profile_callback, $order = 'ASC', $orderby = null ) {
$hook = Utils\get_flag_value( $assoc_args, 'hook' ); $hook = Utils\get_flag_value( $assoc_args, 'hook' );
$type = $focus = false; $focus = false;
$type = false;
$fields = array(); $fields = array();
if ( $hook ) { if ( $hook ) {
$type = 'hook'; $type = 'hook';
@ -415,7 +416,7 @@ class Command {
* @param string $file * @param string $file
*/ */
private static function include_file( $file ) { private static function include_file( $file ) {
include( $file ); include $file;
} }
/** /**

View file

@ -28,7 +28,7 @@ class Formatter {
} }
if ( 'time' !== $fields[0] ) { if ( 'time' !== $fields[0] ) {
$this->total_cell_index = array_search( $fields[0], $format_args['fields'] ); $this->total_cell_index = array_search( $fields[0], $format_args['fields'], true );
} }
$format_args['fields'] = array_map( 'trim', $format_args['fields'] ); $format_args['fields'] = array_map( 'trim', $format_args['fields'] );
@ -93,7 +93,9 @@ class Formatter {
usort( usort(
$items, $items,
function( $a, $b ) use ( $order, $orderby ) { function( $a, $b ) use ( $order, $orderby ) {
list( $first, $second ) = 'ASC' === $order ? array( $a, $b ) : array( $b, $a );
$orderby_array = 'ASC' === $order ? array( $a, $b ) : array( $b, $a );
list( $first, $second ) = $orderby_array;
if ( is_numeric( $first->$orderby ) && is_numeric( $second->$orderby ) ) { if ( is_numeric( $first->$orderby ) && is_numeric( $second->$orderby ) ) {
return $this->compare_float( $first->$orderby, $second->$orderby ); return $this->compare_float( $first->$orderby, $second->$orderby );
@ -104,7 +106,7 @@ class Formatter {
); );
} }
$location_index = array_search( 'location', $fields ); $location_index = array_search( 'location', $fields, true );
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 ) {

View file

@ -38,7 +38,9 @@ class Logger {
global $wpdb, $wp_object_cache; global $wpdb, $wp_object_cache;
$this->start_time = microtime( true ); $this->start_time = microtime( true );
$this->query_offset = ! empty( $wpdb->queries ) ? count( $wpdb->queries ) : 0; $this->query_offset = ! empty( $wpdb->queries ) ? count( $wpdb->queries ) : 0;
if ( false === ( $key = array_search( $this, self::$active_loggers ) ) ) { $key = array_search( $this, self::$active_loggers, true );
if ( false === $key ) {
self::$active_loggers[] = $this; self::$active_loggers[] = $this;
} }
$this->cache_hit_offset = ! empty( $wp_object_cache->cache_hits ) ? $wp_object_cache->cache_hits : 0; $this->cache_hit_offset = ! empty( $wp_object_cache->cache_hits ) ? $wp_object_cache->cache_hits : 0;
@ -62,7 +64,10 @@ class Logger {
$this->time += microtime( true ) - $this->start_time; $this->time += microtime( true ) - $this->start_time;
} }
if ( ! is_null( $this->query_offset ) && isset( $wpdb ) && ! empty( $wpdb->queries ) ) { if ( ! is_null( $this->query_offset ) && isset( $wpdb ) && ! empty( $wpdb->queries ) ) {
for ( $i = $this->query_offset; $i < count( $wpdb->queries ); $i++ ) {
$query_total_count = count( $wpdb->queries );
for ( $i = $this->query_offset; $i < $query_total_count; $i++ ) {
$this->query_time += $wpdb->queries[ $i ][1]; $this->query_time += $wpdb->queries[ $i ][1];
$this->query_count++; $this->query_count++;
} }
@ -84,7 +89,9 @@ 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;
if ( false !== ( $key = array_search( $this, self::$active_loggers ) ) ) { $key = array_search( $this, self::$active_loggers, true );
if ( false !== $key ) {
unset( self::$active_loggers[ $key ] ); unset( self::$active_loggers[ $key ] );
} }
} }

View file

@ -8,8 +8,8 @@ class Profiler {
private $type; private $type;
private $focus; private $focus;
private $loggers = array(); private $loggers = array();
private $stage_hooks = array( private $stage_hooks = array(
'bootstrap' => array( 'bootstrap' => array(
'muplugins_loaded', 'muplugins_loaded',
'plugins_loaded', 'plugins_loaded',
@ -34,6 +34,7 @@ class Profiler {
'wp_footer', 'wp_footer',
), ),
); );
private $current_stage_hooks = array(); private $current_stage_hooks = array();
private $running_hook = null; private $running_hook = null;
private $previous_filter = null; private $previous_filter = null;
@ -55,7 +56,8 @@ class Profiler {
public function get_loggers() { public function get_loggers() {
foreach ( $this->loggers as $i => $logger ) { foreach ( $this->loggers as $i => $logger ) {
if ( is_array( $logger ) ) { if ( is_array( $logger ) ) {
$this->loggers[ $i ] = $logger = new Logger( $logger ); $logger = new Logger( $logger );
$this->loggers[ $i ] = $logger;
} }
if ( ! isset( $logger->callback ) ) { if ( ! isset( $logger->callback ) ) {
continue; continue;
@ -78,7 +80,8 @@ class Profiler {
WP_CLI::add_wp_hook( WP_CLI::add_wp_hook(
'muplugins_loaded', 'muplugins_loaded',
function() { function() {
if ( $url = WP_CLI::get_runner()->config['url'] ) { $url = WP_CLI::get_runner()->config['url'];
if ( ! empty( $url ) ) {
WP_CLI::set_url( trailingslashit( $url ) ); WP_CLI::set_url( trailingslashit( $url ) );
} else { } else {
WP_CLI::set_url( home_url( '/' ) ); WP_CLI::set_url( home_url( '/' ) );
@ -103,7 +106,7 @@ class Profiler {
$stage_hooks = array_merge( $stage_hooks, $hooks ); $stage_hooks = array_merge( $stage_hooks, $hooks );
} }
$end_hook = substr( $this->focus, 0, -7 ); $end_hook = substr( $this->focus, 0, -7 );
$key = array_search( $end_hook, $stage_hooks ); $key = array_search( $end_hook, $stage_hooks, true );
if ( isset( $stage_hooks[ $key - 1 ] ) ) { if ( isset( $stage_hooks[ $key - 1 ] ) ) {
$start_hook = $stage_hooks[ $key - 1 ]; $start_hook = $stage_hooks[ $key - 1 ];
WP_CLI::add_wp_hook( $start_hook, array( $this, 'wp_tick_profile_begin' ), 9999 ); WP_CLI::add_wp_hook( $start_hook, array( $this, 'wp_tick_profile_begin' ), 9999 );
@ -136,17 +139,22 @@ class Profiler {
// and hide calls from the tick handler and backtraces. // and hide calls from the tick handler and backtraces.
// Copied from P3 Profiler // Copied from P3 Profiler
if ( extension_loaded( 'xcache' ) ) { if ( extension_loaded( 'xcache' ) ) {
@ini_set( 'xcache.optimizer', false ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- ini_set can be disabled on server. @ini_set( 'xcache.optimizer', false ); // phpcs:ignore
// WordPress.PHP.NoSilencedErrors.Discouraged -- ini_set can be disabled on server.
} elseif ( extension_loaded( 'apc' ) ) { } elseif ( extension_loaded( 'apc' ) ) {
@ini_set( 'apc.optimization', 0 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- ini_set can be disabled on server. @ini_set( 'apc.optimization', 0 ); // phpcs:ignore
// WordPress.PHP.NoSilencedErrors.Discouraged -- ini_set can be disabled on server.
apc_clear_cache(); apc_clear_cache();
} elseif ( extension_loaded( 'eaccelerator' ) ) { } elseif ( extension_loaded( 'eaccelerator' ) ) {
@ini_set( 'eaccelerator.optimizer', 0 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- ini_set can be disabled on server. @ini_set( 'eaccelerator.optimizer', 0 ); // phpcs:ignore
// WordPress.PHP.NoSilencedErrors.Discouraged -- ini_set can be disabled on server.
if ( function_exists( 'eaccelerator_optimizer' ) ) { if ( function_exists( 'eaccelerator_optimizer' ) ) {
@eaccelerator_optimizer( false ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- disabling eaccelerator on runtime can faild @eaccelerator_optimizer( false ); // phpcs:ignore
// WordPress.PHP.NoSilencedErrors.Discouraged -- disabling eaccelerator on runtime can faild
} }
} elseif ( extension_loaded( 'Zend Optimizer+' ) ) { } elseif ( extension_loaded( 'Zend Optimizer+' ) ) {
@ini_set( 'zend_optimizerplus.optimization_level', 0 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- ini_set can be disabled on server. @ini_set( 'zend_optimizerplus.optimization_level', 0 ); // phpcs:ignore
// WordPress.PHP.NoSilencedErrors.Discouraged -- ini_set can be disabled on server.
} }
register_tick_function( array( $this, 'handle_function_tick' ) ); register_tick_function( array( $this, 'handle_function_tick' ) );
@ -173,7 +181,7 @@ class Profiler {
} }
$current_filter = current_filter(); $current_filter = current_filter();
if ( ( 'stage' === $this->type && in_array( $current_filter, $this->current_stage_hooks ) ) if ( ( 'stage' === $this->type && in_array( $current_filter, $this->current_stage_hooks, true ) )
|| ( 'hook' === $this->type && ! $this->focus ) ) { || ( 'hook' === $this->type && ! $this->focus ) ) {
$pseudo_hook = "{$current_filter}:before"; $pseudo_hook = "{$current_filter}:before";
if ( isset( $this->loggers[ $pseudo_hook ] ) ) { if ( isset( $this->loggers[ $pseudo_hook ] ) ) {
@ -257,11 +265,11 @@ class Profiler {
} }
$current_filter = current_filter(); $current_filter = current_filter();
if ( ( 'stage' === $this->type && in_array( $current_filter, $this->current_stage_hooks ) ) if ( ( 'stage' === $this->type && in_array( $current_filter, $this->current_stage_hooks, true ) )
|| ( 'hook' === $this->type && ! $this->focus ) ) { || ( 'hook' === $this->type && ! $this->focus ) ) {
$this->loggers[ $current_filter ]->stop(); $this->loggers[ $current_filter ]->stop();
if ( 'stage' === $this->type ) { if ( 'stage' === $this->type ) {
$key = array_search( $current_filter, $this->current_stage_hooks ); $key = array_search( $current_filter, $this->current_stage_hooks, true );
if ( false !== $key && isset( $this->current_stage_hooks[ $key + 1 ] ) ) { if ( false !== $key && isset( $this->current_stage_hooks[ $key + 1 ] ) ) {
$pseudo_hook = "{$this->current_stage_hooks[$key+1]}:before"; $pseudo_hook = "{$this->current_stage_hooks[$key+1]}:before";
} else { } else {
@ -287,7 +295,7 @@ class Profiler {
if ( ! is_null( $this->tick_callback ) ) { if ( ! is_null( $this->tick_callback ) ) {
$time = microtime( true ) - $this->tick_start_time; $time = microtime( true ) - $this->tick_start_time;
$callback_hash = md5( serialize( $this->tick_callback . $this->tick_location ) ); $callback_hash = md5( serialize( $this->tick_callback . $this->tick_location ) ); // phpcs:ignore
if ( ! isset( $this->loggers[ $callback_hash ] ) ) { if ( ! isset( $this->loggers[ $callback_hash ] ) ) {
$this->loggers[ $callback_hash ] = array( $this->loggers[ $callback_hash ] = array(
'callback' => $this->tick_callback, 'callback' => $this->tick_callback,
@ -304,7 +312,8 @@ class Profiler {
$this->loggers[ $callback_hash ]['time'] += $time; $this->loggers[ $callback_hash ]['time'] += $time;
if ( isset( $wpdb ) ) { if ( isset( $wpdb ) ) {
for ( $i = $this->tick_query_offset; $i < count( $wpdb->queries ); $i++ ) { $total_queries = count( $wpdb->queries );
for ( $i = $this->tick_query_offset; $i < $total_queries; $i++ ) {
$this->loggers[ $callback_hash ]['query_time'] += $wpdb->queries[ $i ][1]; $this->loggers[ $callback_hash ]['query_time'] += $wpdb->queries[ $i ][1];
$this->loggers[ $callback_hash ]['query_count']++; $this->loggers[ $callback_hash ]['query_count']++;
} }
@ -329,8 +338,9 @@ class Profiler {
$frame = $bt[1]; $frame = $bt[1];
} }
$callback = $location = ''; $location = '';
if ( in_array( strtolower( $frame['function'] ), array( 'include', 'require', 'include_once', 'require_once' ) ) ) { $callback = '';
if ( in_array( strtolower( $frame['function'] ), array( 'include', 'require', 'include_once', 'require_once' ), true ) ) {
$callback = $frame['function'] . " '" . $frame['args'][0] . "'"; $callback = $frame['function'] . " '" . $frame['args'][0] . "'";
} elseif ( isset( $frame['object'] ) && method_exists( $frame['object'], $frame['function'] ) ) { } elseif ( isset( $frame['object'] ) && method_exists( $frame['object'], $frame['function'] ) ) {
$callback = get_class( $frame['object'] ) . '->' . $frame['function'] . '()'; $callback = get_class( $frame['object'] ) . '->' . $frame['function'] . '()';
@ -338,7 +348,7 @@ class Profiler {
$callback = $frame['class'] . '::' . $frame['function'] . '()'; $callback = $frame['class'] . '::' . $frame['function'] . '()';
} elseif ( ! empty( $frame['function'] ) && function_exists( $frame['function'] ) ) { } elseif ( ! empty( $frame['function'] ) && function_exists( $frame['function'] ) ) {
$callback = $frame['function'] . '()'; $callback = $frame['function'] . '()';
} elseif ( '__lambda_func' == $frame['function'] || '{closure}' == $frame['function'] ) { } elseif ( '__lambda_func' === $frame['function'] || '{closure}' === $frame['function'] ) {
$callback = 'function(){}'; $callback = 'function(){}';
} }
@ -447,7 +457,8 @@ class Profiler {
// Template is normally loaded in global scope, so we need to replicate // Template is normally loaded in global scope, so we need to replicate
foreach ( $GLOBALS as $key => $value ) { foreach ( $GLOBALS as $key => $value ) {
global ${$key}; // phpcs:ignore PHPCompatibility.PHP.ForbiddenGlobalVariableVariable.NonBareVariableFound -- Syntax is updated to compatible with php 5 and 7. global ${$key}; // phpcs:ignore
// PHPCompatibility.PHP.ForbiddenGlobalVariableVariable.NonBareVariableFound -- Syntax is updated to compatible with php 5 and 7.
} }
// Load the theme template. // Load the theme template.
@ -460,7 +471,7 @@ class Profiler {
} }
} }
ob_start(); ob_start();
require_once( ABSPATH . WPINC . '/template-loader.php' ); require_once ABSPATH . WPINC . '/template-loader.php';
ob_get_clean(); ob_get_clean();
if ( $this->running_hook ) { if ( $this->running_hook ) {
$this->loggers[ $this->running_hook ]->stop(); $this->loggers[ $this->running_hook ]->stop();
@ -480,7 +491,8 @@ class Profiler {
* Get a human-readable name from a callback * Get a human-readable name from a callback
*/ */
private static function get_name_location_from_callback( $callback ) { private static function get_name_location_from_callback( $callback ) {
$name = $location = ''; $location = '';
$name = '';
$reflection = false; $reflection = false;
if ( is_array( $callback ) && is_object( $callback[0] ) ) { if ( is_array( $callback ) && is_object( $callback[0] ) ) {
$reflection = new \ReflectionMethod( $callback[0], $callback[1] ); $reflection = new \ReflectionMethod( $callback[0], $callback[1] );
@ -567,13 +579,13 @@ class Profiler {
global $wp_filter; global $wp_filter;
if ( ! isset( $wp_filter[ $filter ] ) && class_exists( 'WP_Hook' ) ) { if ( ! isset( $wp_filter[ $filter ] ) && class_exists( 'WP_Hook' ) ) {
$wp_filter[ $filter ] = new \WP_Hook; $wp_filter[ $filter ] = new \WP_Hook(); // phpcs:ignore
} }
if ( is_a( $wp_filter[ $filter ], 'WP_Hook' ) ) { if ( is_a( $wp_filter[ $filter ], 'WP_Hook' ) ) {
$wp_filter[ $filter ]->callbacks = $callbacks; $wp_filter[ $filter ]->callbacks = $callbacks;
} else { } else {
$wp_filter[ $filter ] = $callbacks; $wp_filter[ $filter ] = $callbacks; // phpcs:ignore
} }
} }

View file

@ -1,61 +1,61 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<ruleset name="WP-CLI"> <ruleset name="WP-CLI-find">
<description>Custom ruleset for WP-CLI</description> <description>Custom ruleset for WP-CLI profile-command</description>
<!-- For help in understanding this file: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml --> <!--
<!-- For help in using PHPCS: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage --> #############################################################################
COMMAND LINE ARGUMENTS
For help understanding this file: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml
For help using PHPCS: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage
#############################################################################
-->
<!-- What to scan --> <!-- What to scan. -->
<file>.</file> <file>.</file>
<!-- Ignoring Files and Folders:
https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#ignoring-files-and-folders -->
<exclude-pattern>*/.git/*</exclude-pattern>
<exclude-pattern>*/ci/*</exclude-pattern>
<exclude-pattern>*/features/*</exclude-pattern>
<exclude-pattern>*/packages/*</exclude-pattern>
<exclude-pattern>*/tests/*</exclude-pattern>
<exclude-pattern>*/utils/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
<!-- How to scan --> <!-- Show progress. -->
<arg value="sp"/> <!-- Show sniff and progress --> <arg value="p"/>
<arg name="colors"/> <!-- Show results with colors -->
<arg name="extensions" value="php"/> <!-- Limit to PHP files -->
<!-- Rules: Check PHP version compatibility - see <!-- Strip the filepaths down to the relevant bit. -->
https://github.com/PHPCompatibility/PHPCompatibilityWP --> <arg name="basepath" value="./"/>
<rule ref="PHPCompatibilityWP">
<!-- Polyfill package is used so array_column() is available for PHP 5.4- -->
<exclude name="PHPCompatibility.PHP.NewFunctions.array_columnFound"/>
<!-- Both magic quotes directives set in wp-settings-cli.php to provide consistent starting point. -->
<exclude name="PHPCompatibility.PHP.DeprecatedIniDirectives.magic_quotes_runtimeDeprecatedRemoved"/>
<exclude name="PHPCompatibility.PHP.DeprecatedIniDirectives.magic_quotes_sybaseDeprecatedRemoved"/>
</rule>
<!-- For help in understanding this testVersion: <!-- Check up to 8 files simultaneously. -->
https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions --> <arg name="parallel" value="8"/>
<!--
#############################################################################
USE THE WP_CLI_CS RULESET
#############################################################################
-->
<rule ref="WP_CLI_CS"/>
<!--
#############################################################################
PROJECT SPECIFIC CONFIGURATION FOR SNIFFS
#############################################################################
-->
<!-- For help understanding the `testVersion` configuration setting:
https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
<config name="testVersion" value="5.4-"/> <config name="testVersion" value="5.4-"/>
<!-- Ignore php_uname mode issue, we're conditionally providing a callback --> <!-- Verify that everything in the global namespace is either namespaced or prefixed.
<rule ref="PHPCompatibility.PHP.NewFunctionParameters.php_uname_modeFound"> See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#naming-conventions-prefix-everything-in-the-global-namespace -->
<exclude-pattern>*/php/commands/src/CLI_Command.php</exclude-pattern> <rule ref="WordPress.NamingConventions.PrefixAllGlobals">
</rule>
<!-- Rules: WordPress Coding Standards - see
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards -->
<rule ref="WordPress-Core">
<exclude name="Squiz.PHP.DisallowMultipleAssignments.Found" />
<exclude name="WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar" />
<exclude name="WordPress.NamingConventions.ValidVariableName.MemberNotSnakeCase" />
<exclude name="WordPress.NamingConventions.ValidVariableName.NotSnakeCase" />
</rule>
<!-- For help in understanding these custom sniff properties:
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties -->
<rule ref="WordPress.Files.FileName">
<properties> <properties>
<property name="strict_class_file_names" value="false"/> <property name="strict_class_file_names" value="false"/>
<property name="prefixes" type="array">
<element value="WP_CLI\Profile"/><!-- Namespaces. -->
<element value="wpcli_profile"/><!-- Global variables and such. -->
</property>
</properties> </properties>
<exclude name="WordPress.Files.FileName.NotHyphenatedLowercase" /> <exclude name="WordPress.Files.FileName.NotHyphenatedLowercase" />
<exclude-pattern>*/inc/*.php$</exclude-pattern>
</rule> </rule>
<rule ref="WordPress.PHP.NoSilencedErrors.Discouraged">
<exclude-pattern>*/inc/class-profiler.php$</exclude-pattern>
</rule>
</ruleset> </ruleset>