Complex classes like Benchmark_Profiler 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_Profiler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 79 | class Benchmark_Profiler extends PEAR { |
||
|
|
|||
| 80 | /** |
||
| 81 | * Contains the total ex. time of each section |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | * @access private |
||
| 85 | */ |
||
| 86 | var $_sections = array(); |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Calling stack |
||
| 90 | * |
||
| 91 | * @var array |
||
| 92 | * @access private |
||
| 93 | */ |
||
| 94 | var $_stack = array(); |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Notes how often a section was entered |
||
| 98 | * |
||
| 99 | * @var array |
||
| 100 | * @access private |
||
| 101 | */ |
||
| 102 | var $_numberOfCalls = array(); |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Notes for each section how much time is spend in sub-sections |
||
| 106 | * |
||
| 107 | * @var array |
||
| 108 | * @access private |
||
| 109 | */ |
||
| 110 | var $_subSectionsTime = array(); |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Notes for each section how often it calls which section |
||
| 114 | * |
||
| 115 | * @var array |
||
| 116 | * @access private |
||
| 117 | */ |
||
| 118 | var $_calls = array(); |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Notes for each section how often it was called by which section |
||
| 122 | * |
||
| 123 | * @var array |
||
| 124 | * @access private |
||
| 125 | */ |
||
| 126 | var $_callers = array(); |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Auto-starts and stops profiler |
||
| 130 | * |
||
| 131 | * @var boolean |
||
| 132 | * @access private |
||
| 133 | */ |
||
| 134 | var $_auto = FALSE; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Max marker name length for non-html output |
||
| 138 | * |
||
| 139 | * @var integer |
||
| 140 | * @access private |
||
| 141 | */ |
||
| 142 | var $_maxStringLength = 0; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Constructor, starts profiling recording |
||
| 146 | * |
||
| 147 | * @access public |
||
| 148 | */ |
||
| 149 | function Benchmark_Profiler($auto = FALSE) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Destructor, stops profiling recording |
||
| 161 | * |
||
| 162 | * @access private |
||
| 163 | */ |
||
| 164 | function _Benchmark_Profiler() { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Returns profiling informations for a given section. |
||
| 173 | * |
||
| 174 | * @param string $section |
||
| 175 | * @return array |
||
| 176 | * @access public |
||
| 177 | */ |
||
| 178 | function getSectionInformations($section = 'Global') { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Returns profiling informations for all sections. |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | * @access public |
||
| 221 | */ |
||
| 222 | function getAllSectionsInformations() { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Returns formatted profiling information. |
||
| 234 | * |
||
| 235 | * @param string output format (auto, plain or html), default auto |
||
| 236 | * @see display() |
||
| 237 | * @access private |
||
| 238 | */ |
||
| 239 | function _getOutput($format) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Returns formatted profiling information. |
||
| 334 | * |
||
| 335 | * @param string output format (auto, plain or html), default auto |
||
| 336 | * @access public |
||
| 337 | */ |
||
| 338 | function display($format = 'auto') { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Enters "Global" section. |
||
| 344 | * |
||
| 345 | * @see enterSection(), stop() |
||
| 346 | * @access public |
||
| 347 | */ |
||
| 348 | function start() { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Leaves "Global" section. |
||
| 354 | * |
||
| 355 | * @see leaveSection(), start() |
||
| 356 | * @access public |
||
| 357 | */ |
||
| 358 | function stop() { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Enters code section. |
||
| 364 | * |
||
| 365 | * @param string name of the code section |
||
| 366 | * @see start(), leaveSection() |
||
| 367 | * @access public |
||
| 368 | */ |
||
| 369 | function enterSection($name) { |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Leaves code section. |
||
| 399 | * |
||
| 400 | * @param string name of the marker to be set |
||
| 401 | * @see stop(), enterSection() |
||
| 402 | * @access public |
||
| 403 | */ |
||
| 404 | function leaveSection($name) { |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Wrapper for microtime(). |
||
| 438 | * |
||
| 439 | * @return float |
||
| 440 | * @access private |
||
| 441 | * @since 1.3.0 |
||
| 442 | */ |
||
| 443 | function _getMicrotime() { |
||
| 447 | } |
||
| 448 |
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider.