From 011f98370d779f66c05fc6b93df47d73a588d4e5 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 24 May 2024 10:11:00 +0200 Subject: [PATCH 1/5] Remove unused property --- src/Logger.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Logger.php b/src/Logger.php index b59e30a7..24354d1d 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -14,8 +14,6 @@ class Logger { public $hook_time = 0; public $request_count = 0; public $request_time = 0; - public $callback_count = 0; - private $start_time = null; private $query_offset = null; private $cache_hit_offset = null; From 84a7e9d73ad70318dd007c4eb62c145822470527 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 24 May 2024 10:11:14 +0200 Subject: [PATCH 2/5] Use magic methods instead of dynamic properties --- src/Logger.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Logger.php b/src/Logger.php index 24354d1d..febac244 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -22,12 +22,26 @@ class Logger { private $hook_depth = 0; private $request_start_time = null; + private $definitions = array(); + public static $active_loggers = array(); public function __construct( $definition = array() ) { foreach ( $definition as $k => $v ) { - $this->$k = $v; + $this->definitions[ $k ] = $v; + } + } + + public function __get( $key ) { + if ( isset( $this->definitions[ $key ] ) ) { + return $this->definitions[ $key ]; } + + return null; + } + + public function __isset( $key ) { + return isset( $this->definitions[ $key ] ); } /** From 128210cd2b4c74acee3b89f7ee4bb3b2feedb1e0 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 24 May 2024 10:11:24 +0200 Subject: [PATCH 3/5] Avoid passing `null` to `substr` --- src/Profiler.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Profiler.php b/src/Profiler.php index 5c8e5acb..7d376575 100644 --- a/src/Profiler.php +++ b/src/Profiler.php @@ -99,8 +99,11 @@ function () { } } ); - if ( 'hook' === $this->type - && ':before' === substr( $this->focus, -7, 7 ) ) { + if ( + 'hook' === $this->type && + $this->focus && + ':before' === substr( $this->focus, -7, 7 ) + ) { $stage_hooks = array(); foreach ( $this->stage_hooks as $hooks ) { $stage_hooks = array_merge( $stage_hooks, $hooks ); @@ -114,8 +117,11 @@ function () { WP_CLI::add_hook( 'after_wp_config_load', array( $this, 'wp_tick_profile_begin' ) ); } WP_CLI::add_wp_hook( $end_hook, array( $this, 'wp_tick_profile_end' ), -9999 ); - } elseif ( 'hook' === $this->type - && ':after' === substr( $this->focus, -6, 6 ) ) { + } elseif ( + 'hook' === $this->type && + $this->focus && + ':after' === substr( $this->focus, -6, 6 ) + ) { $start_hook = substr( $this->focus, 0, -6 ); WP_CLI::add_wp_hook( $start_hook, array( $this, 'wp_tick_profile_begin' ), 9999 ); } else { From 4f41b336e00202ec24646dc5670fac7d9610579b Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 24 May 2024 10:15:07 +0200 Subject: [PATCH 4/5] Lint fix --- src/Logger.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Logger.php b/src/Logger.php index febac244..8da153ee 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -4,16 +4,16 @@ 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; + 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; @@ -22,7 +22,7 @@ class Logger { private $hook_depth = 0; private $request_start_time = null; - private $definitions = array(); + private $definitions = array(); public static $active_loggers = array(); From 04c9da64aed83414c0523041cfa9e852aebfe8c2 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 24 May 2024 10:50:11 +0200 Subject: [PATCH 5/5] Add setter --- src/Logger.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Logger.php b/src/Logger.php index 8da153ee..0c3ab116 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -40,6 +40,10 @@ public function __get( $key ) { return null; } + public function __set( $key, $value ) { + $this->definitions[ $key ] = $value; + } + public function __isset( $key ) { return isset( $this->definitions[ $key ] ); }