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 | protected function _decode($profile) { |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Convert the raw data into a flatter list that is easier to use. |
||
| 53 | * |
||
| 54 | * This removes some of the parentage detail as all calls of a given |
||
| 55 | * method are aggregated. We are not able to maintain a full tree structure |
||
| 56 | * in any case, as xhprof only keeps one level of detail. |
||
| 57 | * |
||
| 58 | * @return void |
||
| 59 | */ |
||
| 60 | protected function _process() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Sum up the values in $this->_keys; |
||
| 90 | * |
||
| 91 | * @param array $a The first set of profile data |
||
| 92 | * @param array $b The second set of profile data. |
||
| 93 | * @return array Merged profile data. |
||
| 94 | */ |
||
| 95 | protected function _sumKeys($a, $b) |
||
| 105 | |||
| 106 | protected function _diffKeys($a, $b, $includeSelf = true) |
||
| 117 | |||
| 118 | protected function _diffPercentKeys($a, $b, $includeSelf = true) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get the profile run data. |
||
| 137 | * |
||
| 138 | * TODO remove this and move all the features using it into this/ |
||
| 139 | * other classes. |
||
| 140 | * |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | public function getProfile() |
||
| 147 | |||
| 148 | public function getId() |
||
| 152 | |||
| 153 | public function getDate() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get meta data about the profile. Read's a . split path |
||
| 164 | * out of the meta data in a profile. For example `SERVER.REQUEST_TIME` |
||
| 165 | * |
||
| 166 | * @param string $key The dotted key to read. |
||
| 167 | * @return null|mixed Null on failure, otherwise the stored value. |
||
| 168 | */ |
||
| 169 | public function getMeta($key = null) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Read data from the profile run. |
||
| 188 | * |
||
| 189 | * @param string $key The function key name to read. |
||
| 190 | * @param string $metric The metric to read. |
||
| 191 | * @return null|float |
||
| 192 | */ |
||
| 193 | public function get($key, $metric = null) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Find a function matching a watched function. |
||
| 209 | * |
||
| 210 | * @param string $pattern The pattern to look for. |
||
| 211 | * @return null|array An list of matching functions |
||
| 212 | * or null. |
||
| 213 | */ |
||
| 214 | public function getWatched($pattern) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Find the parent and children method/functions for a given |
||
| 235 | * symbol. |
||
| 236 | * |
||
| 237 | * The parent/children arrays will contain all the callers + callees |
||
| 238 | * of the symbol given. The current index will give the total |
||
| 239 | * inclusive values for all properties. |
||
| 240 | * |
||
| 241 | * @param string $symbol The name of the function/method to find |
||
| 242 | * relatives for. |
||
| 243 | * @param string $metric The metric to compare $threshold with. |
||
| 244 | * @param float $threshold The threshold to exclude child functions at. Any |
||
| 245 | * function that represents less than this percentage of the current metric |
||
| 246 | * will be filtered out. |
||
| 247 | * @return array List of (parent, current, children) |
||
| 248 | */ |
||
| 249 | public function getRelatives($symbol, $metric = null, $threshold = 0) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Get the parent methods for a given symbol. |
||
| 271 | * |
||
| 272 | * @param string $symbol The name of the function/method to find |
||
| 273 | * parents for. |
||
| 274 | * @return array List of parents |
||
| 275 | */ |
||
| 276 | protected function _getParents($symbol) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Find symbols that are the children of the given name. |
||
| 290 | * |
||
| 291 | * @param string $symbol The name of the function to find children of. |
||
| 292 | * @param string $metric The metric to compare $threshold with. |
||
| 293 | * @param float $threshold The threshold to exclude functions at. Any |
||
| 294 | * function that represents less than |
||
| 295 | * @return array An array of child methods. |
||
| 296 | */ |
||
| 297 | protected function _getChildren($symbol, $metric = null, $threshold = 0) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Extracts a single dimension of data |
||
| 326 | * from a profile run. |
||
| 327 | * |
||
| 328 | * Useful for creating bar/column graphs. |
||
| 329 | * The profile data will be sorted by the column |
||
| 330 | * and then the $limit records will be extracted. |
||
| 331 | * |
||
| 332 | * @param string $dimension The dimension to extract |
||
| 333 | * @param int $limit Number of elements to pull |
||
| 334 | * @return array Array of data with name = function name and |
||
| 335 | * value = the dimension. |
||
| 336 | */ |
||
| 337 | public function extractDimension($dimension, $limit) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Generate the approximate exclusive values for each metric. |
||
| 353 | * |
||
| 354 | * We get a==>b as the name, we need a key for a and b in the array |
||
| 355 | * to get exclusive values for A we need to subtract the values of B (and any other children); |
||
| 356 | * call passing in the entire profile only, should return an array of |
||
| 357 | * functions with their regular timing, and exclusive numbers inside ['exclusive'] |
||
| 358 | * |
||
| 359 | * Consider: |
||
| 360 | * /---c---d---e |
||
| 361 | * a -/----b---d---e |
||
| 362 | * |
||
| 363 | * We have c==>d and b==>d, and in both instances d invokes e, yet we will |
||
| 364 | * have but a single d==>e result. This is a known and documented limitation of XHProf |
||
| 365 | * |
||
| 366 | * We have one d==>e entry, with some values, including ct=2 |
||
| 367 | * We also have c==>d and b==>d |
||
| 368 | * |
||
| 369 | * We should determine how many ==>d options there are, and equally |
||
| 370 | * split the cost of d==>e across them since d==>e represents the sum total of all calls. |
||
| 371 | * |
||
| 372 | * Notes: |
||
| 373 | * Function names are not unique, but we're merging them |
||
| 374 | * |
||
| 375 | * @return Xhgui_Profile A new instance with exclusive data set. |
||
| 376 | */ |
||
| 377 | public function calculateSelf() |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Sort data by a dimension. |
||
| 406 | * |
||
| 407 | * @param string $dimension The dimension to sort by. |
||
| 408 | * @param array $data The data to sort. |
||
| 409 | * @return array The sorted data. |
||
| 410 | */ |
||
| 411 | public function sort($dimension, $data) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Split a key name into the parent==>child format. |
||
| 425 | * |
||
| 426 | * @param string $name The name to split. |
||
| 427 | * @return array An array of parent, child. parent will be null if there |
||
| 428 | * is no parent. |
||
| 429 | */ |
||
| 430 | public function splitName($name) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Get the total number of tracked function calls in this run. |
||
| 441 | * |
||
| 442 | * @return int |
||
| 443 | */ |
||
| 444 | public function getFunctionCount() |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Compare this run to another run. |
||
| 459 | * |
||
| 460 | * @param Xhgui_Profile $head The other run to compare with |
||
| 461 | * @return array An array of comparison data. |
||
| 462 | */ |
||
| 463 | public function compare(Xhgui_Profile $head) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Get the max value for any give metric. |
||
| 499 | * |
||
| 500 | * @param string $metric The metric to get a max value for. |
||
| 501 | */ |
||
| 502 | protected function _maxValue($metric) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Return a structured array suitable for generating callgraph visualizations. |
||
| 518 | * |
||
| 519 | * Functions whose inclusive time is less than 2% of the total time will |
||
| 520 | * be excluded from the callgraph data. |
||
| 521 | * |
||
| 522 | * @return array |
||
| 523 | */ |
||
| 524 | public function getCallgraph($metric = 'wt', $threshold = 0.01) |
||
| 550 | |||
| 551 | protected function _callgraphData($parentName, $main, $metric, $threshold, $parentIndex = null) |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Return a structured array suitable for generating flamegraph visualizations. |
||
| 600 | * |
||
| 601 | * Functions whose inclusive time is less than 1% of the total time will |
||
| 602 | * be excluded from the callgraph data. |
||
| 603 | * |
||
| 604 | * @return array |
||
| 605 | */ |
||
| 606 | public function getFlamegraph($metric = 'wt', $threshold = 0.01) |
||
| 625 | |||
| 626 | protected function _flamegraphData($parentName, $main, $metric, $threshold, $parentIndex = null) |
||
| 670 | |||
| 671 | public function toArray() |
||
| 675 | } |
||
| 676 |
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.