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 Xhgui_Profile 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 Xhgui_Profile, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class Xhgui_Profile |
||
|
|
|||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @const Key used for methods with no parent |
||
| 11 | */ |
||
| 12 | const NO_PARENT = '__xhgui_top__'; |
||
| 13 | |||
| 14 | protected $_data; |
||
| 15 | protected $_collapsed; |
||
| 16 | protected $_indexed; |
||
| 17 | protected $_visited; |
||
| 18 | |||
| 19 | protected $_keys = array('ct', 'wt', 'cpu', 'mu', 'pmu'); |
||
| 20 | protected $_exclusiveKeys = array('ewt', 'ecpu', 'emu', 'epmu'); |
||
| 21 | protected $_functionCount; |
||
| 22 | |||
| 23 | public function __construct($profile, $convert = true) |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Convert the raw data into a flatter list that is easier to use. |
||
| 33 | * |
||
| 34 | * This removes some of the parentage detail as all calls of a given |
||
| 35 | * method are aggregated. We are not able to maintain a full tree structure |
||
| 36 | * in any case, as xhprof only keeps one level of detail. |
||
| 37 | * |
||
| 38 | * @return void |
||
| 39 | */ |
||
| 40 | protected function _process() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Sum up the values in $this->_keys; |
||
| 69 | * |
||
| 70 | * @param array $a The first set of profile data |
||
| 71 | * @param array $b The second set of profile data. |
||
| 72 | * @return array Merged profile data. |
||
| 73 | */ |
||
| 74 | protected function _sumKeys($a, $b) |
||
| 81 | |||
| 82 | protected function _diffKeys($a, $b, $includeSelf = true) |
||
| 83 | { |
||
| 84 | $keys = $this->_keys; |
||
| 85 | if ($includeSelf) { |
||
| 86 | $keys = array_merge($keys, $this->_exclusiveKeys); |
||
| 87 | } |
||
| 88 | foreach ($keys as $key) { |
||
| 89 | $a[$key] -= $b[$key]; |
||
| 90 | } |
||
| 91 | return $a; |
||
| 92 | } |
||
| 93 | |||
| 94 | protected function _diffPercentKeys($a, $b, $includeSelf = true) |
||
| 95 | { |
||
| 96 | $out = array(); |
||
| 97 | $keys = $this->_keys; |
||
| 98 | if ($includeSelf) { |
||
| 99 | $keys = array_merge($keys, $this->_exclusiveKeys); |
||
| 100 | } |
||
| 101 | foreach ($keys as $key) { |
||
| 102 | if ($b[$key] != 0) { |
||
| 103 | $out[$key] = $a[$key] / $b[$key]; |
||
| 104 | } else { |
||
| 105 | $out[$key] = -1; |
||
| 106 | } |
||
| 107 | } |
||
| 108 | return $out; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get the profile run data. |
||
| 113 | * |
||
| 114 | * TODO remove this and move all the features using it into this/ |
||
| 115 | * other classes. |
||
| 116 | * |
||
| 117 | * @return array |
||
| 118 | */ |
||
| 119 | public function getProfile() |
||
| 123 | |||
| 124 | public function getId() |
||
| 128 | |||
| 129 | public function getDate() |
||
| 130 | { |
||
| 131 | $date = $this->getMeta('SERVER.REQUEST_TIME'); |
||
| 132 | if ($date) { |
||
| 133 | return new DateTime('@' . $date); |
||
| 134 | } |
||
| 135 | return new DateTime('now'); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get meta data about the profile. Read's a . split path |
||
| 140 | * out of the meta data in a profile. For example `SERVER.REQUEST_TIME` |
||
| 141 | * |
||
| 142 | * @param string $key The dotted key to read. |
||
| 143 | * @return null|mixed Null on failure, otherwise the stored value. |
||
| 144 | */ |
||
| 145 | public function getMeta($key = null) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Read data from the profile run. |
||
| 164 | * |
||
| 165 | * @param string $key The function key name to read. |
||
| 166 | * @param string $metric The metric to read. |
||
| 167 | * @return null|float |
||
| 168 | */ |
||
| 169 | public function get($key, $metric = null) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Find a function matching a watched function. |
||
| 185 | * |
||
| 186 | * @param string $pattern The pattern to look for. |
||
| 187 | * @return null|array An list of matching functions |
||
| 188 | * or null. |
||
| 189 | */ |
||
| 190 | public function getWatched($pattern) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Find the parent and children method/functions for a given |
||
| 211 | * symbol. |
||
| 212 | * |
||
| 213 | * The parent/children arrays will contain all the callers + callees |
||
| 214 | * of the symbol given. The current index will give the total |
||
| 215 | * inclusive values for all properties. |
||
| 216 | * |
||
| 217 | * @param string $symbol The name of the function/method to find |
||
| 218 | * relatives for. |
||
| 219 | * @param string $metric The metric to compare $threshold with. |
||
| 220 | * @param float $threshold The threshold to exclude child functions at. Any |
||
| 221 | * function that represents less than this percentage of the current metric |
||
| 222 | * will be filtered out. |
||
| 223 | * @return array List of (parent, current, children) |
||
| 224 | */ |
||
| 225 | public function getRelatives($symbol, $metric = null, $threshold = 0) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get the parent methods for a given symbol. |
||
| 247 | * |
||
| 248 | * @param string $symbol The name of the function/method to find |
||
| 249 | * parents for. |
||
| 250 | * @return array List of parents |
||
| 251 | */ |
||
| 252 | protected function _getParents($symbol) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Find symbols that are the children of the given name. |
||
| 266 | * |
||
| 267 | * @param string $symbol The name of the function to find children of. |
||
| 268 | * @param string $metric The metric to compare $threshold with. |
||
| 269 | * @param float $threshold The threshold to exclude functions at. Any |
||
| 270 | * function that represents less than |
||
| 271 | * @return array An array of child methods. |
||
| 272 | */ |
||
| 273 | protected function _getChildren($symbol, $metric = null, $threshold = 0) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Extracts a single dimension of data |
||
| 302 | * from a profile run. |
||
| 303 | * |
||
| 304 | * Useful for creating bar/column graphs. |
||
| 305 | * The profile data will be sorted by the column |
||
| 306 | * and then the $limit records will be extracted. |
||
| 307 | * |
||
| 308 | * @param string $dimension The dimension to extract |
||
| 309 | * @param int $limit Number of elements to pull |
||
| 310 | * @return array Array of data with name = function name and |
||
| 311 | * value = the dimension. |
||
| 312 | */ |
||
| 313 | public function extractDimension($dimension, $limit) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Generate the approximate exclusive values for each metric. |
||
| 329 | * |
||
| 330 | * We get a==>b as the name, we need a key for a and b in the array |
||
| 331 | * to get exclusive values for A we need to subtract the values of B (and any other children); |
||
| 332 | * call passing in the entire profile only, should return an array of |
||
| 333 | * functions with their regular timing, and exclusive numbers inside ['exclusive'] |
||
| 334 | * |
||
| 335 | * Consider: |
||
| 336 | * /---c---d---e |
||
| 337 | * a -/----b---d---e |
||
| 338 | * |
||
| 339 | * We have c==>d and b==>d, and in both instances d invokes e, yet we will |
||
| 340 | * have but a single d==>e result. This is a known and documented limitation of XHProf |
||
| 341 | * |
||
| 342 | * We have one d==>e entry, with some values, including ct=2 |
||
| 343 | * We also have c==>d and b==>d |
||
| 344 | * |
||
| 345 | * We should determine how many ==>d options there are, and equally |
||
| 346 | * split the cost of d==>e across them since d==>e represents the sum total of all calls. |
||
| 347 | * |
||
| 348 | * Notes: |
||
| 349 | * Function names are not unique, but we're merging them |
||
| 350 | * |
||
| 351 | * @return Xhgui_Profile A new instance with exclusive data set. |
||
| 352 | */ |
||
| 353 | public function calculateSelf() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Sort data by a dimension. |
||
| 382 | * |
||
| 383 | * @param string $dimension The dimension to sort by. |
||
| 384 | * @param array $data The data to sort. |
||
| 385 | * @return array The sorted data. |
||
| 386 | */ |
||
| 387 | public function sort($dimension, $data) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Split a key name into the parent==>child format. |
||
| 401 | * |
||
| 402 | * @param string $name The name to split. |
||
| 403 | * @return array An array of parent, child. parent will be null if there |
||
| 404 | * is no parent. |
||
| 405 | */ |
||
| 406 | public function splitName($name) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Get the total number of tracked function calls in this run. |
||
| 417 | * |
||
| 418 | * @return int |
||
| 419 | */ |
||
| 420 | public function getFunctionCount() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Compare this run to another run. |
||
| 435 | * |
||
| 436 | * @param Xhgui_Profile $head The other run to compare with |
||
| 437 | * @return array An array of comparison data. |
||
| 438 | */ |
||
| 439 | public function compare(Xhgui_Profile $head) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Get the max value for any give metric. |
||
| 475 | * |
||
| 476 | * @param string $metric The metric to get a max value for. |
||
| 477 | */ |
||
| 478 | protected function _maxValue($metric) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Return a structured array suitable for generating callgraph visualizations. |
||
| 494 | * |
||
| 495 | * Functions whose inclusive time is less than 2% of the total time will |
||
| 496 | * be excluded from the callgraph data. |
||
| 497 | * |
||
| 498 | * @return array |
||
| 499 | */ |
||
| 500 | public function getCallgraph($metric = 'wt', $threshold = 0.01) |
||
| 526 | |||
| 527 | protected function _callgraphData($parentName, $main, $metric, $threshold, $parentIndex = null) |
||
| 528 | { |
||
| 529 | // Leaves don't have children, and don't have links/nodes to add. |
||
| 530 | if (!isset($this->_indexed[$parentName])) { |
||
| 531 | return; |
||
| 532 | } |
||
| 533 | |||
| 534 | $children = $this->_indexed[$parentName]; |
||
| 535 | foreach ($children as $childName => $metrics) { |
||
| 536 | $metrics = $this->_collapsed[$childName]; |
||
| 537 | if ($metrics[$metric] / $main <= $threshold) { |
||
| 538 | continue; |
||
| 539 | } |
||
| 540 | $revisit = false; |
||
| 541 | |||
| 542 | // Keep track of which nodes we've visited and their position |
||
| 543 | // in the node list. |
||
| 544 | if (!isset($this->_visited[$childName])) { |
||
| 545 | $index = count($this->_nodes); |
||
| 546 | $this->_visited[$childName] = $index; |
||
| 547 | |||
| 548 | $this->_nodes[] = array( |
||
| 549 | 'name' => $childName, |
||
| 550 | 'callCount' => $metrics['ct'], |
||
| 551 | 'value' => $metrics[$metric], |
||
| 552 | ); |
||
| 553 | } else { |
||
| 554 | $revisit = true; |
||
| 555 | $index = $this->_visited[$childName]; |
||
| 556 | } |
||
| 557 | |||
| 558 | if ($parentIndex !== null) { |
||
| 559 | $this->_links[] = array( |
||
| 560 | 'source' => $parentName, |
||
| 561 | 'target' => $childName, |
||
| 562 | 'callCount' => $metrics['ct'], |
||
| 563 | ); |
||
| 564 | } |
||
| 565 | |||
| 566 | // If the current function has more children, |
||
| 567 | // walk that call subgraph. |
||
| 568 | if (isset($this->_indexed[$childName]) && !$revisit) { |
||
| 569 | $this->_callgraphData($childName, $main, $metric, $threshold, $index); |
||
| 570 | } |
||
| 571 | } |
||
| 572 | } |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Return a structured array suitable for generating flamegraph visualizations. |
||
| 576 | * |
||
| 577 | * Functions whose inclusive time is less than 2% of the total time will |
||
| 578 | * be excluded from the callgraph data. |
||
| 579 | * |
||
| 580 | * @return array |
||
| 581 | */ |
||
| 582 | public function getFlamegraph($metric = 'wt', $threshold = 0.01) |
||
| 601 | |||
| 602 | protected function _flamegraphData($parentName, $main, $metric, $threshold, $parentIndex = null) |
||
| 647 | |||
| 648 | public function toArray() |
||
| 652 | } |
||
| 653 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.