Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EEH_Debug_Tools often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EEH_Debug_Tools, and based on these observations, apply Extract Interface, too.
1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) {exit('No direct script access allowed');} |
||
11 | class EEH_Debug_Tools{ |
||
12 | |||
13 | /** |
||
14 | * instance of the EEH_Autoloader object |
||
15 | * @var $_instance |
||
16 | * @access private |
||
17 | */ |
||
18 | private static $_instance; |
||
19 | |||
20 | /** |
||
21 | * array containing the start time for the timers |
||
22 | */ |
||
23 | private $_start_times; |
||
24 | /** |
||
25 | * array containing all the timer'd times, which can be outputted via show_times() |
||
26 | */ |
||
27 | private $_times = array(); |
||
28 | |||
29 | /** |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $_memory_usage_points = array(); |
||
34 | |||
35 | |||
36 | |||
37 | /** |
||
38 | * @singleton method used to instantiate class object |
||
39 | * @access public |
||
40 | * @return EEH_Debug_Tools |
||
41 | */ |
||
42 | public static function instance() { |
||
43 | // check if class object is instantiated, and instantiated properly |
||
44 | if ( ! self::$_instance instanceof EEH_Debug_Tools ) { |
||
45 | self::$_instance = new self(); |
||
46 | } |
||
47 | return self::$_instance; |
||
48 | } |
||
49 | |||
50 | |||
51 | |||
52 | /** |
||
53 | * class constructor |
||
54 | * |
||
55 | * @access private |
||
56 | * @return \EEH_Debug_Tools |
||
|
|||
57 | */ |
||
58 | private function __construct() { |
||
59 | // load Kint PHP debugging library |
||
60 | if ( ! class_exists( 'Kint' ) && file_exists( EE_PLUGIN_DIR_PATH . 'tests' . DS . 'kint' . DS . 'Kint.class.php' )){ |
||
61 | // despite EE4 having a check for an existing copy of the Kint debugging class, |
||
62 | // if another plugin was loaded AFTER EE4 and they did NOT perform a similar check, |
||
63 | // then hilarity would ensue as PHP throws a "Cannot redeclare class Kint" error |
||
64 | // so we've moved it to our test folder so that it is not included with production releases |
||
65 | // plz use https://wordpress.org/plugins/kint-debugger/ if testing production versions of EE |
||
66 | require_once( EE_PLUGIN_DIR_PATH . 'tests' . DS . 'kint' . DS . 'Kint.class.php' ); |
||
67 | } |
||
68 | // if ( ! defined('DOING_AJAX') || $_REQUEST['noheader'] !== 'true' || ! isset( $_REQUEST['noheader'], $_REQUEST['TB_iframe'] ) ) { |
||
69 | //add_action( 'shutdown', array($this,'espresso_session_footer_dump') ); |
||
70 | // } |
||
71 | $plugin = basename( EE_PLUGIN_DIR_PATH ); |
||
72 | add_action( "activate_{$plugin}", array( 'EEH_Debug_Tools', 'ee_plugin_activation_errors' )); |
||
73 | add_action( 'activated_plugin', array( 'EEH_Debug_Tools', 'ee_plugin_activation_errors' )); |
||
74 | add_action( 'shutdown', array( 'EEH_Debug_Tools', 'show_db_name' )); |
||
75 | } |
||
76 | |||
77 | |||
78 | |||
79 | /** |
||
80 | * show_db_name |
||
81 | * |
||
82 | * @return void |
||
83 | */ |
||
84 | public static function show_db_name() { |
||
89 | |||
90 | |||
91 | |||
92 | /** |
||
93 | * dump EE_Session object at bottom of page after everything else has happened |
||
94 | * |
||
95 | * @return void |
||
96 | */ |
||
97 | public function espresso_session_footer_dump() { |
||
113 | |||
114 | |||
115 | |||
116 | /** |
||
117 | * List All Hooked Functions |
||
118 | * to list all functions for a specific hook, add ee_list_hooks={hook-name} to URL |
||
119 | * http://wp.smashingmagazine.com/2009/08/18/10-useful-wordpress-hook-hacks/ |
||
120 | * |
||
121 | * @param string $tag |
||
122 | * @return void |
||
123 | */ |
||
124 | public function espresso_list_hooked_functions( $tag='' ){ |
||
150 | |||
151 | |||
152 | |||
153 | /** |
||
154 | * registered_filter_callbacks |
||
155 | * |
||
156 | * @param string $hook_name |
||
157 | * @return array |
||
158 | */ |
||
159 | public static function registered_filter_callbacks( $hook_name = '' ) { |
||
173 | |||
174 | |||
175 | |||
176 | /** |
||
177 | * reset_times |
||
178 | */ |
||
179 | public function reset_times(){ |
||
182 | |||
183 | |||
184 | |||
185 | /** |
||
186 | * start_timer |
||
187 | * @param null $timer_name |
||
188 | */ |
||
189 | public function start_timer( $timer_name = NULL ){ |
||
192 | |||
193 | |||
194 | |||
195 | /** |
||
196 | * stop_timer |
||
197 | * @param string $timer_name |
||
198 | */ |
||
199 | public function stop_timer($timer_name = 'default'){ |
||
235 | /** |
||
236 | * Measure the memory usage by PHP so far. |
||
237 | * @param string $label The label to show for this time eg "Start of calling Some_Class::some_function" |
||
238 | * @param boolean $output_now whether to echo now, or wait until EEH_Debug_Tools::show_times() is called |
||
239 | * @return void |
||
240 | */ |
||
241 | public function measure_memory( $label, $output_now = false ) { |
||
248 | |||
249 | /** |
||
250 | * Converts a measure of memory bytes into the most logical units (eg kb, mb, etc) |
||
251 | * @param int $size |
||
252 | * @return string |
||
253 | */ |
||
254 | public function convert( $size ) { |
||
258 | |||
259 | |||
260 | |||
261 | /** |
||
262 | * show_times |
||
263 | * @param bool $output_now |
||
264 | * @return string |
||
265 | */ |
||
266 | public function show_times($output_now=true){ |
||
274 | |||
275 | |||
276 | |||
277 | /** |
||
278 | * captures plugin activation errors for debugging |
||
279 | * |
||
280 | * @return void |
||
281 | */ |
||
282 | public static function ee_plugin_activation_errors() { |
||
304 | |||
305 | |||
306 | |||
307 | /** |
||
308 | * This basically mimics the WordPress _doing_it_wrong() function except adds our own messaging etc. Very useful for providing helpful messages to developers when the method of doing something has been deprecated, or we want to make sure they use something the right way. |
||
309 | * |
||
310 | * @access public |
||
311 | * @param string $function The function that was called |
||
312 | * @param string $message A message explaining what has been done incorrectly |
||
313 | * @param string $version The version of Event Espresso where the error was added |
||
314 | * @param int $error_type |
||
315 | * @uses trigger_error() |
||
316 | */ |
||
317 | public function doing_it_wrong( $function, $message, $version, $error_type = E_USER_NOTICE ) { |
||
335 | |||
336 | |||
337 | |||
338 | |||
339 | /** |
||
340 | * Logger helpers |
||
341 | */ |
||
342 | |||
343 | /** |
||
344 | * debug |
||
345 | * |
||
346 | * @param string $class |
||
347 | * @param string $func |
||
348 | * @param string $line |
||
349 | * @param array $info |
||
350 | * @param bool $display_request |
||
351 | * @param string $debug_index |
||
352 | * @param string $debug_key |
||
353 | */ |
||
354 | public static function log( $class='', $func = '', $line = '', $info = array(), $display_request = false, $debug_index = '', $debug_key = 'EE_DEBUG_SPCO' ) { |
||
372 | |||
373 | |||
374 | |||
375 | /** |
||
376 | * strip_objects |
||
377 | * |
||
378 | * @param array $info |
||
379 | * @return array |
||
380 | */ |
||
381 | public static function strip_objects( $info = array() ) { |
||
402 | |||
403 | |||
404 | |||
405 | /** |
||
406 | * @param mixed $var |
||
407 | * @param string $var_name |
||
408 | * @param string $file |
||
409 | * @param int $line |
||
410 | * @param int $header |
||
411 | * @param bool $die |
||
412 | */ |
||
413 | public static function printv( $var, $var_name = '', $file = __FILE__, $line = __LINE__, $header = 5, $die = false ) { |
||
433 | |||
434 | |||
435 | /** |
||
436 | * @param mixed $var |
||
437 | * @param string $var_name |
||
438 | * @param string $file |
||
439 | * @param int $line |
||
440 | * @param int $header |
||
441 | * @param bool $die |
||
442 | */ |
||
443 | public static function printr( $var, $var_name = '', $file = __FILE__, $line = __LINE__, $header = 5, $die = false ) { |
||
476 | |||
477 | |||
478 | |||
479 | |||
480 | } |
||
481 | |||
517 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.