Skip to content

Commit

Permalink
Merge pull request #157 from BhargavBhandari90/issue-156
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera authored Jun 4, 2020
2 parents 7196a8e + 8d75d2f commit e5aa8af
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 80 deletions.
2 changes: 2 additions & 0 deletions .distignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
.editorconfig
.travis.yml
circle.yml
phpcs.xml.dist
phpunit.xml.dist
bin/
features/
utils/
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ vendor/
*.zip
*.tar.gz
*.log
phpunit.xml
phpcs.xml
.phpcs.xml
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"wp-cli/wp-cli": "^2"
},
"require-dev": {
"wp-cli/wp-cli-tests": "^2.0.7"
"wp-cli/wp-cli-tests": "^2.1"
},
"config": {
"process-timeout": 7200,
Expand Down
5 changes: 3 additions & 2 deletions inc/class-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,8 @@ function() use ( $file ) {
*/
private static function profile_eval_ish( $assoc_args, $profile_callback, $order = 'ASC', $orderby = null ) {
$hook = Utils\get_flag_value( $assoc_args, 'hook' );
$type = $focus = false;
$focus = false;
$type = false;
$fields = array();
if ( $hook ) {
$type = 'hook';
Expand Down Expand Up @@ -415,7 +416,7 @@ private static function profile_eval_ish( $assoc_args, $profile_callback, $order
* @param string $file
*/
private static function include_file( $file ) {
include( $file );
include $file;
}

/**
Expand Down
8 changes: 5 additions & 3 deletions inc/class-formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct( &$assoc_args, $fields = null, $prefix = false ) {
}

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'] );
Expand Down Expand Up @@ -93,7 +93,9 @@ private function show_table( $order, $orderby, $items, $fields, $include_total )
usort(
$items,
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 ) ) {
return $this->compare_float( $first->$orderby, $second->$orderby );
Expand All @@ -104,7 +106,7 @@ function( $a, $b ) use ( $order, $orderby ) {
);
}

$location_index = array_search( 'location', $fields );
$location_index = array_search( 'location', $fields, true );
foreach ( $items as $item ) {
$values = array_values( \WP_CLI\Utils\pick_fields( $item, $fields ) );
foreach ( $values as $i => $value ) {
Expand Down
13 changes: 10 additions & 3 deletions inc/class-logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public function start() {
global $wpdb, $wp_object_cache;
$this->start_time = microtime( true );
$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;
}
$this->cache_hit_offset = ! empty( $wp_object_cache->cache_hits ) ? $wp_object_cache->cache_hits : 0;
Expand All @@ -62,7 +64,10 @@ public function stop() {
$this->time += microtime( true ) - $this->start_time;
}
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_count++;
}
Expand All @@ -84,7 +89,9 @@ public function stop() {
$this->query_offset = null;
$this->cache_hit_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 ] );
}
}
Expand Down
58 changes: 35 additions & 23 deletions inc/class-profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class Profiler {

private $type;
private $focus;
private $loggers = array();
private $stage_hooks = array(
private $loggers = array();
private $stage_hooks = array(
'bootstrap' => array(
'muplugins_loaded',
'plugins_loaded',
Expand All @@ -34,6 +34,7 @@ class Profiler {
'wp_footer',
),
);

private $current_stage_hooks = array();
private $running_hook = null;
private $previous_filter = null;
Expand All @@ -55,7 +56,8 @@ public function __construct( $type, $focus ) {
public function get_loggers() {
foreach ( $this->loggers as $i => $logger ) {
if ( is_array( $logger ) ) {
$this->loggers[ $i ] = $logger = new Logger( $logger );
$logger = new Logger( $logger );
$this->loggers[ $i ] = $logger;
}
if ( ! isset( $logger->callback ) ) {
continue;
Expand All @@ -78,7 +80,8 @@ public function run() {
WP_CLI::add_wp_hook(
'muplugins_loaded',
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 ) );
} else {
WP_CLI::set_url( home_url( '/' ) );
Expand All @@ -103,7 +106,7 @@ function() {
$stage_hooks = array_merge( $stage_hooks, $hooks );
}
$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 ] ) ) {
$start_hook = $stage_hooks[ $key - 1 ];
WP_CLI::add_wp_hook( $start_hook, array( $this, 'wp_tick_profile_begin' ), 9999 );
Expand Down Expand Up @@ -136,17 +139,22 @@ public function wp_tick_profile_begin( $value = null ) {
// and hide calls from the tick handler and backtraces.
// Copied from P3 Profiler
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' ) ) {
@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();
} 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' ) ) {
@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+' ) ) {
@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' ) );
Expand All @@ -173,7 +181,7 @@ public function wp_hook_begin() {
}

$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 ) ) {
$pseudo_hook = "{$current_filter}:before";
if ( isset( $this->loggers[ $pseudo_hook ] ) ) {
Expand Down Expand Up @@ -257,11 +265,11 @@ public function wp_hook_end( $filter_value = null ) {
}

$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 ) ) {
$this->loggers[ $current_filter ]->stop();
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 ] ) ) {
$pseudo_hook = "{$this->current_stage_hooks[$key+1]}:before";
} else {
Expand All @@ -287,7 +295,7 @@ public function handle_function_tick() {
if ( ! is_null( $this->tick_callback ) ) {
$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 ] ) ) {
$this->loggers[ $callback_hash ] = array(
'callback' => $this->tick_callback,
Expand All @@ -304,7 +312,8 @@ public function handle_function_tick() {
$this->loggers[ $callback_hash ]['time'] += $time;

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_count']++;
}
Expand All @@ -329,16 +338,17 @@ public function handle_function_tick() {
$frame = $bt[1];
}

$callback = $location = '';
if ( in_array( strtolower( $frame['function'] ), array( 'include', 'require', 'include_once', 'require_once' ) ) ) {
$location = '';
$callback = '';
if ( in_array( strtolower( $frame['function'] ), array( 'include', 'require', 'include_once', 'require_once' ), true ) ) {
$callback = $frame['function'] . " '" . $frame['args'][0] . "'";
} elseif ( isset( $frame['object'] ) && method_exists( $frame['object'], $frame['function'] ) ) {
$callback = get_class( $frame['object'] ) . '->' . $frame['function'] . '()';
} elseif ( isset( $frame['class'] ) && method_exists( $frame['class'], $frame['function'] ) ) {
$callback = $frame['class'] . '::' . $frame['function'] . '()';
} elseif ( ! empty( $frame['function'] ) && function_exists( $frame['function'] ) ) {
$callback = $frame['function'] . '()';
} elseif ( '__lambda_func' == $frame['function'] || '{closure}' == $frame['function'] ) {
} elseif ( '__lambda_func' === $frame['function'] || '{closure}' === $frame['function'] ) {
$callback = 'function(){}';
}

Expand Down Expand Up @@ -447,7 +457,8 @@ private function load_wordpress_with_template() {

// Template is normally loaded in global scope, so we need to replicate
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.
Expand All @@ -460,7 +471,7 @@ private function load_wordpress_with_template() {
}
}
ob_start();
require_once( ABSPATH . WPINC . '/template-loader.php' );
require_once ABSPATH . WPINC . '/template-loader.php';
ob_get_clean();
if ( $this->running_hook ) {
$this->loggers[ $this->running_hook ]->stop();
Expand All @@ -480,7 +491,8 @@ private function load_wordpress_with_template() {
* Get a human-readable name from a callback
*/
private static function get_name_location_from_callback( $callback ) {
$name = $location = '';
$location = '';
$name = '';
$reflection = false;
if ( is_array( $callback ) && is_object( $callback[0] ) ) {
$reflection = new \ReflectionMethod( $callback[0], $callback[1] );
Expand Down Expand Up @@ -567,13 +579,13 @@ private static function set_filter_callbacks( $filter, $callbacks ) {
global $wp_filter;

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' ) ) {
$wp_filter[ $filter ]->callbacks = $callbacks;
} else {
$wp_filter[ $filter ] = $callbacks;
$wp_filter[ $filter ] = $callbacks; // phpcs:ignore
}
}

Expand Down
Loading

0 comments on commit e5aa8af

Please sign in to comment.