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 | * Decodes the profile array by removing unicode replacements. |
||
| 33 | * |
||
| 34 | * @param array $profile |
||
| 35 | * |
||
| 36 | * @return array |
||
| 37 | */ |
||
| 38 | protected function decode($profile) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Convert the raw data into a flatter list that is easier to use. |
||
| 61 | * |
||
| 62 | * This removes some of the parentage detail as all calls of a given |
||
| 63 | * method are aggregated. We are not able to maintain a full tree structure |
||
| 64 | * in any case, as xhprof only keeps one level of detail. |
||
| 65 | * |
||
| 66 | * @return void |
||
| 67 | */ |
||
| 68 | protected function _process() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Sum up the values in $this->_keys; |
||
| 98 | * |
||
| 99 | * @param array $a The first set of profile data |
||
| 100 | * @param array $b The second set of profile data. |
||
| 101 | * @return array Merged profile data. |
||
| 102 | */ |
||
| 103 | protected function _sumKeys($a, $b) |
||
| 113 | |||
| 114 | protected function _diffKeys($a, $b, $includeSelf = true) |
||
| 125 | |||
| 126 | protected function _diffPercentKeys($a, $b, $includeSelf = true) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get the profile run data. |
||
| 145 | * |
||
| 146 | * TODO remove this and move all the features using it into this/ |
||
| 147 | * other classes. |
||
| 148 | * |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | public function getProfile() |
||
| 155 | |||
| 156 | public function getId() |
||
| 160 | |||
| 161 | public function getDate() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Get meta data about the profile. Read's a . split path |
||
| 172 | * out of the meta data in a profile. For example `SERVER.REQUEST_TIME` |
||
| 173 | * |
||
| 174 | * @param string $key The dotted key to read. |
||
| 175 | * @return null|mixed Null on failure, otherwise the stored value. |
||
| 176 | */ |
||
| 177 | public function getMeta($key = null) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Read data from the profile run. |
||
| 196 | * |
||
| 197 | * @param string $key The function key name to read. |
||
| 198 | * @param string $metric The metric to read. |
||
| 199 | * @return null|float |
||
| 200 | */ |
||
| 201 | public function get($key, $metric = null) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Find a function matching a watched function. |
||
| 217 | * |
||
| 218 | * @param string $pattern The pattern to look for. |
||
| 219 | * @return null|array An list of matching functions |
||
| 220 | * or null. |
||
| 221 | */ |
||
| 222 | public function getWatched($pattern) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Find the parent and children method/functions for a given |
||
| 243 | * symbol. |
||
| 244 | * |
||
| 245 | * The parent/children arrays will contain all the callers + callees |
||
| 246 | * of the symbol given. The current index will give the total |
||
| 247 | * inclusive values for all properties. |
||
| 248 | * |
||
| 249 | * @param string $symbol The name of the function/method to find |
||
| 250 | * relatives for. |
||
| 251 | * @param string $metric The metric to compare $threshold with. |
||
| 252 | * @param float $threshold The threshold to exclude child functions at. Any |
||
| 253 | * function that represents less than this percentage of the current metric |
||
| 254 | * will be filtered out. |
||
| 255 | * @return array List of (parent, current, children) |
||
| 256 | */ |
||
| 257 | public function getRelatives($symbol, $metric = null, $threshold = 0) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Get the parent methods for a given symbol. |
||
| 279 | * |
||
| 280 | * @param string $symbol The name of the function/method to find |
||
| 281 | * parents for. |
||
| 282 | * @return array List of parents |
||
| 283 | */ |
||
| 284 | protected function _getParents($symbol) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Find symbols that are the children of the given name. |
||
| 298 | * |
||
| 299 | * @param string $symbol The name of the function to find children of. |
||
| 300 | * @param string $metric The metric to compare $threshold with. |
||
| 301 | * @param float $threshold The threshold to exclude functions at. Any |
||
| 302 | * function that represents less than |
||
| 303 | * @return array An array of child methods. |
||
| 304 | */ |
||
| 305 | protected function _getChildren($symbol, $metric = null, $threshold = 0) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Extracts a single dimension of data |
||
| 334 | * from a profile run. |
||
| 335 | * |
||
| 336 | * Useful for creating bar/column graphs. |
||
| 337 | * The profile data will be sorted by the column |
||
| 338 | * and then the $limit records will be extracted. |
||
| 339 | * |
||
| 340 | * @param string $dimension The dimension to extract |
||
| 341 | * @param int $limit Number of elements to pull |
||
| 342 | * @return array Array of data with name = function name and |
||
| 343 | * value = the dimension. |
||
| 344 | */ |
||
| 345 | public function extractDimension($dimension, $limit) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Generate the approximate exclusive values for each metric. |
||
| 361 | * |
||
| 362 | * We get a==>b as the name, we need a key for a and b in the array |
||
| 363 | * to get exclusive values for A we need to subtract the values of B (and any other children); |
||
| 364 | * call passing in the entire profile only, should return an array of |
||
| 365 | * functions with their regular timing, and exclusive numbers inside ['exclusive'] |
||
| 366 | * |
||
| 367 | * Consider: |
||
| 368 | * /---c---d---e |
||
| 369 | * a -/----b---d---e |
||
| 370 | * |
||
| 371 | * We have c==>d and b==>d, and in both instances d invokes e, yet we will |
||
| 372 | * have but a single d==>e result. This is a known and documented limitation of XHProf |
||
| 373 | * |
||
| 374 | * We have one d==>e entry, with some values, including ct=2 |
||
| 375 | * We also have c==>d and b==>d |
||
| 376 | * |
||
| 377 | * We should determine how many ==>d options there are, and equally |
||
| 378 | * split the cost of d==>e across them since d==>e represents the sum total of all calls. |
||
| 379 | * |
||
| 380 | * Notes: |
||
| 381 | * Function names are not unique, but we're merging them |
||
| 382 | * |
||
| 383 | * @return Xhgui_Profile A new instance with exclusive data set. |
||
| 384 | */ |
||
| 385 | public function calculateSelf() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Sort data by a dimension. |
||
| 414 | * |
||
| 415 | * @param string $dimension The dimension to sort by. |
||
| 416 | * @param array $data The data to sort. |
||
| 417 | * @return array The sorted data. |
||
| 418 | */ |
||
| 419 | public function sort($dimension, $data) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Split a key name into the parent==>child format. |
||
| 433 | * |
||
| 434 | * @param string $name The name to split. |
||
| 435 | * @return array An array of parent, child. parent will be null if there |
||
| 436 | * is no parent. |
||
| 437 | */ |
||
| 438 | public function splitName($name) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Get the total number of tracked function calls in this run. |
||
| 449 | * |
||
| 450 | * @return int |
||
| 451 | */ |
||
| 452 | public function getFunctionCount() |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Compare this run to another run. |
||
| 467 | * |
||
| 468 | * @param Xhgui_Profile $head The other run to compare with |
||
| 469 | * @return array An array of comparison data. |
||
| 470 | */ |
||
| 471 | public function compare(Xhgui_Profile $head) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Get the max value for any give metric. |
||
| 507 | * |
||
| 508 | * @param string $metric The metric to get a max value for. |
||
| 509 | */ |
||
| 510 | protected function _maxValue($metric) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Return a structured array suitable for generating callgraph visualizations. |
||
| 526 | * |
||
| 527 | * Functions whose inclusive time is less than 2% of the total time will |
||
| 528 | * be excluded from the callgraph data. |
||
| 529 | * |
||
| 530 | * @return array |
||
| 531 | */ |
||
| 532 | public function getCallgraph($metric = 'wt', $threshold = 0.01) |
||
| 558 | |||
| 559 | protected function _callgraphData($parentName, $main, $metric, $threshold, $parentIndex = null) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Return a structured array suitable for generating flamegraph visualizations. |
||
| 608 | * |
||
| 609 | * Functions whose inclusive time is less than 1% of the total time will |
||
| 610 | * be excluded from the callgraph data. |
||
| 611 | * |
||
| 612 | * @return array |
||
| 613 | */ |
||
| 614 | public function getFlamegraph($metric = 'wt', $threshold = 0.01) |
||
| 633 | |||
| 634 | protected function _flamegraphData($parentName, $main, $metric, $threshold, $parentIndex = null) |
||
| 678 | |||
| 679 | public function toArray() |
||
| 683 | } |
||
| 684 |
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.