Complex classes like Benchmark 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 Benchmark, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Benchmark |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * array containing the start time for the timers |
||
22 | */ |
||
23 | private static $start_times; |
||
|
|||
24 | |||
25 | /** |
||
26 | * array containing all the timer'd times, which can be outputted via show_times() |
||
27 | */ |
||
28 | private static $times = array(); |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected static $memory_usage = array(); |
||
34 | |||
35 | |||
36 | |||
37 | /** |
||
38 | * whether to benchmark code or not |
||
39 | */ |
||
40 | public static function doNotRun() |
||
44 | |||
45 | |||
46 | |||
47 | /** |
||
48 | * resetTimes |
||
49 | */ |
||
50 | public static function resetTimes() |
||
54 | |||
55 | |||
56 | |||
57 | /** |
||
58 | * Add Benchmark::startTimer() before a block of code you want to measure the performance of |
||
59 | * |
||
60 | * @param null $timer_name |
||
61 | */ |
||
62 | public static function startTimer($timer_name = null) |
||
70 | |||
71 | |||
72 | |||
73 | /** |
||
74 | * Add Benchmark::stopTimer() after a block of code you want to measure the performance of |
||
75 | * |
||
76 | * @param string $timer_name |
||
77 | */ |
||
78 | public static function stopTimer($timer_name = '') |
||
92 | |||
93 | |||
94 | |||
95 | /** |
||
96 | * Measure the memory usage by PHP so far. |
||
97 | * |
||
98 | * @param string $label The label to show for this time eg "Start of calling Some_Class::some_function" |
||
99 | * @param boolean $output_now whether to echo now, or wait until EEH_Debug_Tools::show_times() is called |
||
100 | * @param bool $formatted |
||
101 | * @return void |
||
102 | */ |
||
103 | public static function measureMemory($label = 'memory usage', $output_now = false, $formatted = true) |
||
116 | |||
117 | |||
118 | |||
119 | /** |
||
120 | * will display the benchmarking results at shutdown |
||
121 | * |
||
122 | * @param bool $formatted |
||
123 | * @return void |
||
124 | */ |
||
125 | public static function displayResultsAtShutdown($formatted = true) |
||
134 | |||
135 | |||
136 | |||
137 | /** |
||
138 | * will display the benchmarking results at shutdown |
||
139 | * |
||
140 | * @param string $filepath |
||
141 | * @param bool $formatted |
||
142 | * @param bool $append |
||
143 | * @return void |
||
144 | */ |
||
145 | public static function writeResultsAtShutdown($filepath = '', $formatted = true, $append = true) |
||
154 | |||
155 | |||
156 | |||
157 | /** |
||
158 | * @param bool $formatted |
||
159 | * @return string |
||
160 | */ |
||
161 | private static function generateResults($formatted = true) |
||
212 | |||
213 | |||
214 | |||
215 | /** |
||
216 | * @param bool $echo |
||
217 | * @param bool $formatted |
||
218 | * @return string |
||
219 | */ |
||
220 | public static function displayResults($echo = true, $formatted = true) |
||
229 | |||
230 | |||
231 | |||
232 | /** |
||
233 | * @param string $filepath |
||
234 | * @param bool $formatted |
||
235 | * @param bool $append |
||
236 | */ |
||
237 | public static function writeResultsToFile($filepath = '', $formatted = true, $append = true) |
||
251 | |||
252 | |||
253 | |||
254 | /** |
||
255 | * Converts a measure of memory bytes into the most logical units (eg kb, mb, etc) |
||
256 | * |
||
257 | * @param int $size |
||
258 | * @return string |
||
259 | */ |
||
260 | public static function convert($size) |
||
268 | |||
269 | |||
270 | |||
271 | /** |
||
272 | * @param string $timer_name |
||
273 | * @param float $total_time |
||
274 | * @param bool $formatted |
||
275 | * @return string |
||
276 | */ |
||
277 | public static function formatTime($timer_name, $total_time, $formatted = true) |
||
317 | |||
318 | |||
319 | |||
320 | } |
||
321 |
This check marks private properties in classes that are never used. Those properties can be removed.