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) |
||
| 24 | { |
||
| 25 | $this->_data = $profile; |
||
| 26 | if (!empty($profile['profile']) && $convert) { |
||
| 27 | $this->_process(); |
||
| 28 | } |
||
| 29 | } |
||
| 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() |
||
| 41 | { |
||
| 42 | $result = array(); |
||
| 43 | foreach ($this->_data['profile'] as $name => $values) { |
||
| 44 | list($parent, $func) = $this->splitName($name); |
||
| 45 | |||
| 46 | // Generate collapsed data. |
||
| 47 | if (isset($result[$func])) { |
||
| 48 | $result[$func] = $this->_sumKeys($result[$func], $values); |
||
| 49 | $result[$func]['parents'][] = $parent; |
||
| 50 | } else { |
||
| 51 | $result[$func] = $values; |
||
| 52 | $result[$func]['parents'] = array($parent); |
||
| 53 | } |
||
| 54 | |||
| 55 | // Build the indexed data. |
||
| 56 | if ($parent === null) { |
||
| 57 | $parent = self::NO_PARENT; |
||
| 58 | } |
||
| 59 | if (!isset($this->_indexed[$parent])) { |
||
| 60 | $this->_indexed[$parent] = array(); |
||
| 61 | } |
||
| 62 | $this->_indexed[$parent][$func] = $values; |
||
| 63 | } |
||
| 64 | $this->_collapsed = $result; |
||
| 65 | } |
||
| 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) |
||
| 75 | { |
||
| 76 | foreach ($this->_keys as $key) { |
||
| 77 | $a[$key] += $b[$key]; |
||
| 78 | } |
||
| 79 | return $a; |
||
| 80 | } |
||
| 81 | |||
| 82 | protected function _diffKeys($a, $b, $includeSelf = true) |
||
| 93 | |||
| 94 | protected function _diffPercentKeys($a, $b, $includeSelf = true) |
||
| 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() |
||
| 120 | { |
||
| 121 | return $this->_collapsed; |
||
| 122 | } |
||
| 123 | |||
| 124 | public function getId() |
||
| 125 | { |
||
| 126 | return $this->_data['_id']; |
||
| 127 | } |
||
| 128 | |||
| 129 | public function getDate() |
||
| 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) |
||
| 146 | { |
||
| 147 | $data = $this->_data['meta']; |
||
| 148 | if ($key === null) { |
||
| 149 | return $data; |
||
| 150 | } |
||
| 151 | $parts = explode('.', $key); |
||
| 152 | foreach ($parts as $key) { |
||
| 153 | if (is_array($data) && isset($data[$key])) { |
||
| 154 | $data =& $data[$key]; |
||
| 155 | } else { |
||
| 156 | return null; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | return $data; |
||
| 160 | } |
||
| 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) |
||
| 170 | { |
||
| 171 | if (!isset($this->_collapsed[$key])) { |
||
| 172 | return null; |
||
| 173 | } |
||
| 174 | if (empty($metric)) { |
||
| 175 | return $this->_collapsed[$key]; |
||
| 176 | } |
||
| 177 | if (!isset($this->_collapsed[$key][$metric])) { |
||
| 178 | return null; |
||
| 179 | } |
||
| 180 | return $this->_collapsed[$key][$metric]; |
||
| 181 | } |
||
| 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) |
||
| 191 | { |
||
| 192 | if (isset($this->_collapsed[$pattern])) { |
||
| 193 | $data = $this->_collapsed[$pattern]; |
||
| 194 | $data['function'] = $pattern; |
||
| 195 | return array($data); |
||
| 196 | } |
||
| 197 | $matches = array(); |
||
| 198 | $keys = array_keys($this->_collapsed); |
||
| 199 | foreach ($keys as $func) { |
||
| 200 | if (preg_match('`^' . $pattern . '$`', $func)) { |
||
| 201 | $data = $this->_collapsed[$func]; |
||
| 202 | $data['function'] = $func; |
||
| 203 | $matches[] = $data; |
||
| 204 | } |
||
| 205 | } |
||
| 206 | return $matches; |
||
| 207 | } |
||
| 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) |
||
| 314 | { |
||
| 315 | $profile = $this->sort($dimension, $this->_collapsed); |
||
| 316 | $slice = array_slice($profile, 0, $limit); |
||
| 317 | $extract = array(); |
||
| 318 | foreach ($slice as $func => $funcData) { |
||
| 319 | $extract[] = array( |
||
| 320 | 'name' => $func, |
||
| 321 | 'value' => $funcData[$dimension] |
||
| 322 | ); |
||
| 323 | } |
||
| 324 | return $extract; |
||
| 325 | } |
||
| 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() |
||
| 354 | { |
||
| 355 | // Init exclusive values |
||
| 356 | foreach ($this->_collapsed as &$data) { |
||
| 357 | $data['ewt'] = $data['wt']; |
||
| 358 | $data['emu'] = $data['mu']; |
||
| 359 | $data['ecpu'] = $data['cpu']; |
||
| 360 | $data['ect'] = $data['ct']; |
||
| 361 | $data['epmu'] = $data['pmu']; |
||
| 362 | } |
||
| 363 | unset($data); |
||
| 364 | |||
| 365 | // Go over each method and remove each childs metrics |
||
| 366 | // from the parent. |
||
| 367 | foreach ($this->_collapsed as $name => $data) { |
||
| 368 | $children = $this->_getChildren($name); |
||
| 369 | foreach ($children as $child) { |
||
| 370 | $this->_collapsed[$name]['ewt'] -= $child['wt']; |
||
| 371 | $this->_collapsed[$name]['emu'] -= $child['mu']; |
||
| 372 | $this->_collapsed[$name]['ecpu'] -= $child['cpu']; |
||
| 373 | $this->_collapsed[$name]['ect'] -= $child['ct']; |
||
| 374 | $this->_collapsed[$name]['epmu'] -= $child['pmu']; |
||
| 375 | } |
||
| 376 | } |
||
| 377 | return $this; |
||
| 378 | } |
||
| 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) |
||
| 388 | { |
||
| 389 | $sorter = function ($a, $b) use ($dimension) { |
||
| 390 | if ($a[$dimension] == $b[$dimension]) { |
||
| 391 | return 0; |
||
| 392 | } |
||
| 393 | return $a[$dimension] > $b[$dimension] ? -1 : 1; |
||
| 394 | }; |
||
| 395 | uasort($data, $sorter); |
||
| 396 | return $data; |
||
| 397 | } |
||
| 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) |
||
| 407 | { |
||
| 408 | $a = explode("==>", $name); |
||
| 409 | if (isset($a[1])) { |
||
| 410 | return $a; |
||
| 411 | } |
||
| 412 | return array(null, $a[0]); |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Get the total number of tracked function calls in this run. |
||
| 417 | * |
||
| 418 | * @return int |
||
| 419 | */ |
||
| 420 | public function getFunctionCount() |
||
| 421 | { |
||
| 422 | if ($this->_functionCount) { |
||
| 423 | return $this->_functionCount; |
||
| 424 | } |
||
| 425 | $total = 0; |
||
| 426 | foreach ($this->_collapsed as $data) { |
||
| 427 | $total += $data['ct']; |
||
| 428 | } |
||
| 429 | $this->_functionCount = $total; |
||
| 430 | return $this->_functionCount; |
||
| 431 | } |
||
| 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 1% 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) |
||
| 603 | { |
||
| 604 | $result = array(); |
||
| 605 | // Leaves don't have children, and don't have links/nodes to add. |
||
| 606 | if (!isset($this->_indexed[$parentName])) { |
||
| 607 | return $result; |
||
| 608 | } |
||
| 609 | |||
| 610 | $children = $this->_indexed[$parentName]; |
||
| 611 | foreach ($children as $childName => $metrics) { |
||
| 612 | $metrics = $this->_collapsed[$childName]; |
||
| 613 | if ($metrics[$metric] / $main <= $threshold) { |
||
| 614 | continue; |
||
| 615 | } |
||
| 646 | |||
| 647 | public function toArray() |
||
| 651 | } |
||
| 652 |
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.