Skip to content

Commit e5aa8af

Browse files
authored
Merge pull request #157 from BhargavBhandari90/issue-156
2 parents 7196a8e + 8d75d2f commit e5aa8af

8 files changed

+107
-80
lines changed

.distignore

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
.editorconfig
66
.travis.yml
77
circle.yml
8+
phpcs.xml.dist
9+
phpunit.xml.dist
810
bin/
911
features/
1012
utils/

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ vendor/
66
*.zip
77
*.tar.gz
88
*.log
9+
phpunit.xml
10+
phpcs.xml
11+
.phpcs.xml

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"wp-cli/wp-cli": "^2"
1111
},
1212
"require-dev": {
13-
"wp-cli/wp-cli-tests": "^2.0.7"
13+
"wp-cli/wp-cli-tests": "^2.1"
1414
},
1515
"config": {
1616
"process-timeout": 7200,

inc/class-command.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,8 @@ function() use ( $file ) {
368368
*/
369369
private static function profile_eval_ish( $assoc_args, $profile_callback, $order = 'ASC', $orderby = null ) {
370370
$hook = Utils\get_flag_value( $assoc_args, 'hook' );
371-
$type = $focus = false;
371+
$focus = false;
372+
$type = false;
372373
$fields = array();
373374
if ( $hook ) {
374375
$type = 'hook';
@@ -415,7 +416,7 @@ private static function profile_eval_ish( $assoc_args, $profile_callback, $order
415416
* @param string $file
416417
*/
417418
private static function include_file( $file ) {
418-
include( $file );
419+
include $file;
419420
}
420421

421422
/**

inc/class-formatter.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct( &$assoc_args, $fields = null, $prefix = false ) {
2828
}
2929

3030
if ( 'time' !== $fields[0] ) {
31-
$this->total_cell_index = array_search( $fields[0], $format_args['fields'] );
31+
$this->total_cell_index = array_search( $fields[0], $format_args['fields'], true );
3232
}
3333

3434
$format_args['fields'] = array_map( 'trim', $format_args['fields'] );
@@ -93,7 +93,9 @@ private function show_table( $order, $orderby, $items, $fields, $include_total )
9393
usort(
9494
$items,
9595
function( $a, $b ) use ( $order, $orderby ) {
96-
list( $first, $second ) = 'ASC' === $order ? array( $a, $b ) : array( $b, $a );
96+
97+
$orderby_array = 'ASC' === $order ? array( $a, $b ) : array( $b, $a );
98+
list( $first, $second ) = $orderby_array;
9799

98100
if ( is_numeric( $first->$orderby ) && is_numeric( $second->$orderby ) ) {
99101
return $this->compare_float( $first->$orderby, $second->$orderby );
@@ -104,7 +106,7 @@ function( $a, $b ) use ( $order, $orderby ) {
104106
);
105107
}
106108

107-
$location_index = array_search( 'location', $fields );
109+
$location_index = array_search( 'location', $fields, true );
108110
foreach ( $items as $item ) {
109111
$values = array_values( \WP_CLI\Utils\pick_fields( $item, $fields ) );
110112
foreach ( $values as $i => $value ) {

inc/class-logger.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ public function start() {
3838
global $wpdb, $wp_object_cache;
3939
$this->start_time = microtime( true );
4040
$this->query_offset = ! empty( $wpdb->queries ) ? count( $wpdb->queries ) : 0;
41-
if ( false === ( $key = array_search( $this, self::$active_loggers ) ) ) {
41+
$key = array_search( $this, self::$active_loggers, true );
42+
43+
if ( false === $key ) {
4244
self::$active_loggers[] = $this;
4345
}
4446
$this->cache_hit_offset = ! empty( $wp_object_cache->cache_hits ) ? $wp_object_cache->cache_hits : 0;
@@ -62,7 +64,10 @@ public function stop() {
6264
$this->time += microtime( true ) - $this->start_time;
6365
}
6466
if ( ! is_null( $this->query_offset ) && isset( $wpdb ) && ! empty( $wpdb->queries ) ) {
65-
for ( $i = $this->query_offset; $i < count( $wpdb->queries ); $i++ ) {
67+
68+
$query_total_count = count( $wpdb->queries );
69+
70+
for ( $i = $this->query_offset; $i < $query_total_count; $i++ ) {
6671
$this->query_time += $wpdb->queries[ $i ][1];
6772
$this->query_count++;
6873
}
@@ -84,7 +89,9 @@ public function stop() {
8489
$this->query_offset = null;
8590
$this->cache_hit_offset = null;
8691
$this->cache_miss_offset = null;
87-
if ( false !== ( $key = array_search( $this, self::$active_loggers ) ) ) {
92+
$key = array_search( $this, self::$active_loggers, true );
93+
94+
if ( false !== $key ) {
8895
unset( self::$active_loggers[ $key ] );
8996
}
9097
}

inc/class-profiler.php

+35-23
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class Profiler {
88

99
private $type;
1010
private $focus;
11-
private $loggers = array();
12-
private $stage_hooks = array(
11+
private $loggers = array();
12+
private $stage_hooks = array(
1313
'bootstrap' => array(
1414
'muplugins_loaded',
1515
'plugins_loaded',
@@ -34,6 +34,7 @@ class Profiler {
3434
'wp_footer',
3535
),
3636
);
37+
3738
private $current_stage_hooks = array();
3839
private $running_hook = null;
3940
private $previous_filter = null;
@@ -55,7 +56,8 @@ public function __construct( $type, $focus ) {
5556
public function get_loggers() {
5657
foreach ( $this->loggers as $i => $logger ) {
5758
if ( is_array( $logger ) ) {
58-
$this->loggers[ $i ] = $logger = new Logger( $logger );
59+
$logger = new Logger( $logger );
60+
$this->loggers[ $i ] = $logger;
5961
}
6062
if ( ! isset( $logger->callback ) ) {
6163
continue;
@@ -78,7 +80,8 @@ public function run() {
7880
WP_CLI::add_wp_hook(
7981
'muplugins_loaded',
8082
function() {
81-
if ( $url = WP_CLI::get_runner()->config['url'] ) {
83+
$url = WP_CLI::get_runner()->config['url'];
84+
if ( ! empty( $url ) ) {
8285
WP_CLI::set_url( trailingslashit( $url ) );
8386
} else {
8487
WP_CLI::set_url( home_url( '/' ) );
@@ -103,7 +106,7 @@ function() {
103106
$stage_hooks = array_merge( $stage_hooks, $hooks );
104107
}
105108
$end_hook = substr( $this->focus, 0, -7 );
106-
$key = array_search( $end_hook, $stage_hooks );
109+
$key = array_search( $end_hook, $stage_hooks, true );
107110
if ( isset( $stage_hooks[ $key - 1 ] ) ) {
108111
$start_hook = $stage_hooks[ $key - 1 ];
109112
WP_CLI::add_wp_hook( $start_hook, array( $this, 'wp_tick_profile_begin' ), 9999 );
@@ -136,17 +139,22 @@ public function wp_tick_profile_begin( $value = null ) {
136139
// and hide calls from the tick handler and backtraces.
137140
// Copied from P3 Profiler
138141
if ( extension_loaded( 'xcache' ) ) {
139-
@ini_set( 'xcache.optimizer', false ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- ini_set can be disabled on server.
142+
@ini_set( 'xcache.optimizer', false ); // phpcs:ignore
143+
// WordPress.PHP.NoSilencedErrors.Discouraged -- ini_set can be disabled on server.
140144
} elseif ( extension_loaded( 'apc' ) ) {
141-
@ini_set( 'apc.optimization', 0 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- ini_set can be disabled on server.
145+
@ini_set( 'apc.optimization', 0 ); // phpcs:ignore
146+
// WordPress.PHP.NoSilencedErrors.Discouraged -- ini_set can be disabled on server.
142147
apc_clear_cache();
143148
} elseif ( extension_loaded( 'eaccelerator' ) ) {
144-
@ini_set( 'eaccelerator.optimizer', 0 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- ini_set can be disabled on server.
149+
@ini_set( 'eaccelerator.optimizer', 0 ); // phpcs:ignore
150+
// WordPress.PHP.NoSilencedErrors.Discouraged -- ini_set can be disabled on server.
145151
if ( function_exists( 'eaccelerator_optimizer' ) ) {
146-
@eaccelerator_optimizer( false ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- disabling eaccelerator on runtime can faild
152+
@eaccelerator_optimizer( false ); // phpcs:ignore
153+
// WordPress.PHP.NoSilencedErrors.Discouraged -- disabling eaccelerator on runtime can faild
147154
}
148155
} elseif ( extension_loaded( 'Zend Optimizer+' ) ) {
149-
@ini_set( 'zend_optimizerplus.optimization_level', 0 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- ini_set can be disabled on server.
156+
@ini_set( 'zend_optimizerplus.optimization_level', 0 ); // phpcs:ignore
157+
// WordPress.PHP.NoSilencedErrors.Discouraged -- ini_set can be disabled on server.
150158
}
151159

152160
register_tick_function( array( $this, 'handle_function_tick' ) );
@@ -173,7 +181,7 @@ public function wp_hook_begin() {
173181
}
174182

175183
$current_filter = current_filter();
176-
if ( ( 'stage' === $this->type && in_array( $current_filter, $this->current_stage_hooks ) )
184+
if ( ( 'stage' === $this->type && in_array( $current_filter, $this->current_stage_hooks, true ) )
177185
|| ( 'hook' === $this->type && ! $this->focus ) ) {
178186
$pseudo_hook = "{$current_filter}:before";
179187
if ( isset( $this->loggers[ $pseudo_hook ] ) ) {
@@ -257,11 +265,11 @@ public function wp_hook_end( $filter_value = null ) {
257265
}
258266

259267
$current_filter = current_filter();
260-
if ( ( 'stage' === $this->type && in_array( $current_filter, $this->current_stage_hooks ) )
268+
if ( ( 'stage' === $this->type && in_array( $current_filter, $this->current_stage_hooks, true ) )
261269
|| ( 'hook' === $this->type && ! $this->focus ) ) {
262270
$this->loggers[ $current_filter ]->stop();
263271
if ( 'stage' === $this->type ) {
264-
$key = array_search( $current_filter, $this->current_stage_hooks );
272+
$key = array_search( $current_filter, $this->current_stage_hooks, true );
265273
if ( false !== $key && isset( $this->current_stage_hooks[ $key + 1 ] ) ) {
266274
$pseudo_hook = "{$this->current_stage_hooks[$key+1]}:before";
267275
} else {
@@ -287,7 +295,7 @@ public function handle_function_tick() {
287295
if ( ! is_null( $this->tick_callback ) ) {
288296
$time = microtime( true ) - $this->tick_start_time;
289297

290-
$callback_hash = md5( serialize( $this->tick_callback . $this->tick_location ) );
298+
$callback_hash = md5( serialize( $this->tick_callback . $this->tick_location ) ); // phpcs:ignore
291299
if ( ! isset( $this->loggers[ $callback_hash ] ) ) {
292300
$this->loggers[ $callback_hash ] = array(
293301
'callback' => $this->tick_callback,
@@ -304,7 +312,8 @@ public function handle_function_tick() {
304312
$this->loggers[ $callback_hash ]['time'] += $time;
305313

306314
if ( isset( $wpdb ) ) {
307-
for ( $i = $this->tick_query_offset; $i < count( $wpdb->queries ); $i++ ) {
315+
$total_queries = count( $wpdb->queries );
316+
for ( $i = $this->tick_query_offset; $i < $total_queries; $i++ ) {
308317
$this->loggers[ $callback_hash ]['query_time'] += $wpdb->queries[ $i ][1];
309318
$this->loggers[ $callback_hash ]['query_count']++;
310319
}
@@ -329,16 +338,17 @@ public function handle_function_tick() {
329338
$frame = $bt[1];
330339
}
331340

332-
$callback = $location = '';
333-
if ( in_array( strtolower( $frame['function'] ), array( 'include', 'require', 'include_once', 'require_once' ) ) ) {
341+
$location = '';
342+
$callback = '';
343+
if ( in_array( strtolower( $frame['function'] ), array( 'include', 'require', 'include_once', 'require_once' ), true ) ) {
334344
$callback = $frame['function'] . " '" . $frame['args'][0] . "'";
335345
} elseif ( isset( $frame['object'] ) && method_exists( $frame['object'], $frame['function'] ) ) {
336346
$callback = get_class( $frame['object'] ) . '->' . $frame['function'] . '()';
337347
} elseif ( isset( $frame['class'] ) && method_exists( $frame['class'], $frame['function'] ) ) {
338348
$callback = $frame['class'] . '::' . $frame['function'] . '()';
339349
} elseif ( ! empty( $frame['function'] ) && function_exists( $frame['function'] ) ) {
340350
$callback = $frame['function'] . '()';
341-
} elseif ( '__lambda_func' == $frame['function'] || '{closure}' == $frame['function'] ) {
351+
} elseif ( '__lambda_func' === $frame['function'] || '{closure}' === $frame['function'] ) {
342352
$callback = 'function(){}';
343353
}
344354

@@ -447,7 +457,8 @@ private function load_wordpress_with_template() {
447457

448458
// Template is normally loaded in global scope, so we need to replicate
449459
foreach ( $GLOBALS as $key => $value ) {
450-
global ${$key}; // phpcs:ignore PHPCompatibility.PHP.ForbiddenGlobalVariableVariable.NonBareVariableFound -- Syntax is updated to compatible with php 5 and 7.
460+
global ${$key}; // phpcs:ignore
461+
// PHPCompatibility.PHP.ForbiddenGlobalVariableVariable.NonBareVariableFound -- Syntax is updated to compatible with php 5 and 7.
451462
}
452463

453464
// Load the theme template.
@@ -460,7 +471,7 @@ private function load_wordpress_with_template() {
460471
}
461472
}
462473
ob_start();
463-
require_once( ABSPATH . WPINC . '/template-loader.php' );
474+
require_once ABSPATH . WPINC . '/template-loader.php';
464475
ob_get_clean();
465476
if ( $this->running_hook ) {
466477
$this->loggers[ $this->running_hook ]->stop();
@@ -480,7 +491,8 @@ private function load_wordpress_with_template() {
480491
* Get a human-readable name from a callback
481492
*/
482493
private static function get_name_location_from_callback( $callback ) {
483-
$name = $location = '';
494+
$location = '';
495+
$name = '';
484496
$reflection = false;
485497
if ( is_array( $callback ) && is_object( $callback[0] ) ) {
486498
$reflection = new \ReflectionMethod( $callback[0], $callback[1] );
@@ -567,13 +579,13 @@ private static function set_filter_callbacks( $filter, $callbacks ) {
567579
global $wp_filter;
568580

569581
if ( ! isset( $wp_filter[ $filter ] ) && class_exists( 'WP_Hook' ) ) {
570-
$wp_filter[ $filter ] = new \WP_Hook;
582+
$wp_filter[ $filter ] = new \WP_Hook(); // phpcs:ignore
571583
}
572584

573585
if ( is_a( $wp_filter[ $filter ], 'WP_Hook' ) ) {
574586
$wp_filter[ $filter ]->callbacks = $callbacks;
575587
} else {
576-
$wp_filter[ $filter ] = $callbacks;
588+
$wp_filter[ $filter ] = $callbacks; // phpcs:ignore
577589
}
578590
}
579591

0 commit comments

Comments
 (0)