Complex classes like Timer 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 Timer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Timer { |
||
| 10 | /** |
||
| 11 | * @var array |
||
| 12 | */ |
||
| 13 | public static $collection = []; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var boolean|string |
||
| 17 | */ |
||
| 18 | public static $currentItem = false; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | public static $runningItems = []; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Force the unit to display elapsed times in (MS|S|M|H|D|W) |
||
| 27 | * |
||
| 28 | * @var null|string |
||
| 29 | */ |
||
| 30 | public static $forceDisplayUnit = null; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Color threshold for output (5 => 'red', all items with values of 5 or higher will be red) |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | public static $colorThreshold = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * |
||
| 41 | */ |
||
| 42 | 6 | public static function reset() { |
|
| 47 | |||
| 48 | /** |
||
| 49 | * @param string|null $key |
||
| 50 | * @param array $data |
||
| 51 | * |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | 6 | public static function add($key = null, $data = []) { |
|
| 55 | // If no key is given |
||
| 56 | 6 | if (is_null($key)) { |
|
| 57 | // Set key to file and line |
||
| 58 | $key = Debugger::getCalledFrom(2); |
||
| 59 | } |
||
| 60 | |||
| 61 | // If key is allready in use |
||
| 62 | 6 | if (isset(self::$collection[$key])) { |
|
| 63 | // Get original item |
||
| 64 | 1 | $item = self::$collection[$key]; |
|
| 65 | |||
| 66 | // Set new item count |
||
| 67 | 1 | $itemCount = (isset($item['count']) ? ($item['count'] + 1) : 2); |
|
| 68 | |||
| 69 | // Set correct key for the original item |
||
| 70 | 1 | if (strpos($item['key'], '#') === false) { |
|
| 71 | 1 | self::$collection[$key] = array_merge($item, [ |
|
| 72 | 1 | 'key' => $key . ' #1', |
|
| 73 | 1 | 'count' => $itemCount, |
|
| 74 | 1 | ]); |
|
| 75 | 1 | } else { |
|
| 76 | self::$collection[$key] = array_merge($item, [ |
||
| 77 | 'count' => $itemCount, |
||
| 78 | ]); |
||
| 79 | } |
||
| 80 | |||
| 81 | // Set new key |
||
| 82 | 1 | $key = $key . ' #' . $itemCount; |
|
| 83 | 1 | } |
|
| 84 | |||
| 85 | // Make sure various options are set |
||
| 86 | 6 | if (!isset($data['key'])) { |
|
| 87 | 6 | $data['key'] = $key; |
|
| 88 | 6 | } |
|
| 89 | 6 | if (!isset($data['parent'])) { |
|
| 90 | 6 | $data['parent'] = self::$currentItem; |
|
| 91 | 6 | } |
|
| 92 | 6 | if (!isset($data['level'])) { |
|
| 93 | 6 | $data['level'] = 0; |
|
| 94 | 6 | if (isset($data['parent']) && isset(self::$collection[$data['parent']])) { |
|
| 95 | 1 | $data['level'] = (self::$collection[$data['parent']]['level'] + 1); |
|
| 96 | 1 | } |
|
| 97 | 6 | } |
|
| 98 | |||
| 99 | // Add item to collection |
||
| 100 | 6 | self::$collection[$key] = $data; |
|
| 101 | |||
| 102 | 6 | return $key; |
|
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param string|null $key |
||
| 107 | * |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | 3 | public static function start($key = null) { |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @param string|null $key |
||
| 127 | * |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | 2 | public static function stop($key = null) { |
|
| 171 | |||
| 172 | /** |
||
| 173 | * @param string|null $key |
||
| 174 | * @param int|float|null $start |
||
| 175 | * @param int|float|null $stop |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | 2 | public static function custom($key = null, $start = null, $stop = null) { |
|
| 180 | // Add new item |
||
| 181 | 2 | self::add($key, [ |
|
| 182 | 2 | 'start' => $start, |
|
| 183 | 2 | 'stop' => $stop, |
|
| 184 | 2 | ]); |
|
| 185 | |||
| 186 | // If no stop value is given |
||
| 187 | 2 | if (is_null($stop)) { |
|
| 188 | // Set current item |
||
| 189 | 1 | self::$currentItem = $key; |
|
| 190 | |||
| 191 | // Add to running items |
||
| 192 | 1 | self::$runningItems[$key] = true; |
|
| 193 | 1 | } |
|
| 194 | |||
| 195 | 2 | return $key; |
|
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param string|null $key |
||
| 200 | * @param string|array|\Closure $callback |
||
| 201 | * |
||
| 202 | * @return mixed |
||
| 203 | */ |
||
| 204 | 1 | public static function callback($key = null, $callback) { |
|
| 284 | |||
| 285 | /** |
||
| 286 | * @param string|null $key |
||
| 287 | * @param array $options |
||
| 288 | * |
||
| 289 | * @codeCoverageIgnore |
||
| 290 | */ |
||
| 291 | public static function show($key = null, $options = []) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param array $options |
||
| 301 | * |
||
| 302 | * @codeCoverageIgnore |
||
| 303 | */ |
||
| 304 | public static function showAll($options = []) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param string|null $key |
||
| 349 | * @param array $options |
||
| 350 | * |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | 1 | public static function getStats($key, $options = []) { |
|
| 407 | |||
| 408 | /** |
||
| 409 | * @param int|float $number |
||
| 410 | * @param int $precision |
||
| 411 | * @param null|string $forceUnit |
||
| 412 | * |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | 1 | public static function formatMiliseconds($number = 0, $precision = 2, $forceUnit = null) { |
|
| 452 | } |
||
| 453 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.