Complex classes like 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 Profile, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Profile |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @const Key used for methods with no parent |
||
| 17 | */ |
||
| 18 | const NO_PARENT = '__xhgui_top__'; |
||
| 19 | |||
| 20 | protected $_data; |
||
| 21 | protected $_collapsed; |
||
| 22 | protected $_indexed; |
||
| 23 | protected $_visited; |
||
| 24 | |||
| 25 | protected $_keys = ['ct', 'wt', 'cpu', 'mu', 'pmu']; |
||
| 26 | protected $_exclusiveKeys = ['ewt', 'ecpu', 'emu', 'epmu']; |
||
| 27 | protected $_functionCount; |
||
| 28 | |||
| 29 | public function __construct(array $profile, $convert = true) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Convert the raw data into a flatter list that is easier to use. |
||
| 45 | * |
||
| 46 | * This removes some of the parentage detail as all calls of a given |
||
| 47 | * method are aggregated. We are not able to maintain a full tree structure |
||
| 48 | * in any case, as xhprof only keeps one level of detail. |
||
| 49 | * |
||
| 50 | * @return void |
||
| 51 | */ |
||
| 52 | protected function _process() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Sum up the values in $this->_keys; |
||
| 81 | * |
||
| 82 | * @param array $a The first set of profile data |
||
| 83 | * @param array $b The second set of profile data. |
||
| 84 | * @return array Merged profile data. |
||
| 85 | */ |
||
| 86 | protected function _sumKeys($a, $b) |
||
| 97 | |||
| 98 | protected function _diffKeys($a, $b, $includeSelf = true) |
||
| 110 | |||
| 111 | protected function _diffPercentKeys($a, $b, $includeSelf = true) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get the profile run data. |
||
| 131 | * |
||
| 132 | * TODO remove this and move all the features using it into this/ |
||
| 133 | * other classes. |
||
| 134 | * |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | public function getProfile() |
||
| 141 | |||
| 142 | public function getId() |
||
| 146 | |||
| 147 | public function getDate() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Get meta data about the profile. Read's a . split path |
||
| 159 | * out of the meta data in a profile. For example `SERVER.REQUEST_TIME` |
||
| 160 | * |
||
| 161 | * @param string $key The dotted key to read. |
||
| 162 | * @return null|mixed Null on failure, otherwise the stored value. |
||
| 163 | */ |
||
| 164 | public function getMeta($key = null) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Read data from the profile run. |
||
| 184 | * |
||
| 185 | * @param string $key The function key name to read. |
||
| 186 | * @param string $metric The metric to read. |
||
| 187 | * @return null|float |
||
| 188 | */ |
||
| 189 | public function get($key, $metric = null) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Find a function matching a watched function. |
||
| 206 | * |
||
| 207 | * @param string $pattern The pattern to look for. |
||
| 208 | * @return null|array An list of matching functions |
||
| 209 | * or null. |
||
| 210 | */ |
||
| 211 | public function getWatched($pattern) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Find the parent and children method/functions for a given |
||
| 234 | * symbol. |
||
| 235 | * |
||
| 236 | * The parent/children arrays will contain all the callers + callees |
||
| 237 | * of the symbol given. The current index will give the total |
||
| 238 | * inclusive values for all properties. |
||
| 239 | * |
||
| 240 | * @param string $symbol The name of the function/method to find |
||
| 241 | * relatives for. |
||
| 242 | * @param string $metric The metric to compare $threshold with. |
||
| 243 | * @param float $threshold The threshold to exclude child functions at. Any |
||
| 244 | * function that represents less than this percentage of the current metric |
||
| 245 | * will be filtered out. |
||
| 246 | * @return array List of (parent, current, children) |
||
| 247 | */ |
||
| 248 | 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) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Find symbols that are the children of the given name. |
||
| 291 | * |
||
| 292 | * @param string $symbol The name of the function to find children of. |
||
| 293 | * @param string $metric The metric to compare $threshold with. |
||
| 294 | * @param float $threshold The threshold to exclude functions at. Any |
||
| 295 | * function that represents less than |
||
| 296 | * @return array An array of child methods. |
||
| 297 | */ |
||
| 298 | protected function _getChildren($symbol, $metric = null, $threshold = 0) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Extracts a single dimension of data |
||
| 328 | * from a profile run. |
||
| 329 | * |
||
| 330 | * Useful for creating bar/column graphs. |
||
| 331 | * The profile data will be sorted by the column |
||
| 332 | * and then the $limit records will be extracted. |
||
| 333 | * |
||
| 334 | * @param string $dimension The dimension to extract |
||
| 335 | * @param int $limit Number of elements to pull |
||
| 336 | * @return array Array of data with name = function name and |
||
| 337 | * value = the dimension. |
||
| 338 | */ |
||
| 339 | public function extractDimension($dimension, $limit) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Generate the approximate exclusive values for each metric. |
||
| 356 | * |
||
| 357 | * We get a==>b as the name, we need a key for a and b in the array |
||
| 358 | * to get exclusive values for A we need to subtract the values of B (and any other children); |
||
| 359 | * call passing in the entire profile only, should return an array of |
||
| 360 | * functions with their regular timing, and exclusive numbers inside ['exclusive'] |
||
| 361 | * |
||
| 362 | * Consider: |
||
| 363 | * /---c---d---e |
||
| 364 | * a -/----b---d---e |
||
| 365 | * |
||
| 366 | * We have c==>d and b==>d, and in both instances d invokes e, yet we will |
||
| 367 | * have but a single d==>e result. This is a known and documented limitation of XHProf |
||
| 368 | * |
||
| 369 | * We have one d==>e entry, with some values, including ct=2 |
||
| 370 | * We also have c==>d and b==>d |
||
| 371 | * |
||
| 372 | * We should determine how many ==>d options there are, and equally |
||
| 373 | * split the cost of d==>e across them since d==>e represents the sum total of all calls. |
||
| 374 | * |
||
| 375 | * Notes: |
||
| 376 | * Function names are not unique, but we're merging them |
||
| 377 | * |
||
| 378 | * @return Profile A new instance with exclusive data set. |
||
| 379 | */ |
||
| 380 | public function calculateSelf() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Sort data by a dimension. |
||
| 410 | * |
||
| 411 | * @param string $dimension The dimension to sort by. |
||
| 412 | * @param array $data The data to sort. |
||
| 413 | * @return array The sorted data. |
||
| 414 | */ |
||
| 415 | public function sort($dimension, $data) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @param array $profileData |
||
| 431 | * @param array $filters |
||
| 432 | * |
||
| 433 | * @return array |
||
| 434 | */ |
||
| 435 | public function filter($profileData, $filters = []) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Split a key name into the parent==>child format. |
||
| 450 | * |
||
| 451 | * @param string $name The name to split. |
||
| 452 | * @return array An array of parent, child. parent will be null if there |
||
| 453 | * is no parent. |
||
| 454 | */ |
||
| 455 | public function splitName($name) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Get the total number of tracked function calls in this run. |
||
| 467 | * |
||
| 468 | * @return int |
||
| 469 | */ |
||
| 470 | public function getFunctionCount() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Compare this run to another run. |
||
| 486 | * |
||
| 487 | * @param Profile $head The other run to compare with |
||
| 488 | * @return array An array of comparison data. |
||
| 489 | */ |
||
| 490 | public function compare(Profile $head) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Get the max value for any give metric. |
||
| 526 | * |
||
| 527 | * @param string $metric The metric to get a max value for. |
||
| 528 | */ |
||
| 529 | protected function _maxValue($metric) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Return a structured array suitable for generating callgraph visualizations. |
||
| 546 | * |
||
| 547 | * Functions whose inclusive time is less than 2% of the total time will |
||
| 548 | * be excluded from the callgraph data. |
||
| 549 | * |
||
| 550 | * @return array |
||
| 551 | */ |
||
| 552 | public function getCallgraph($metric = 'wt', $threshold = 0.01) |
||
| 579 | |||
| 580 | protected function _callgraphData($parentName, $main, $metric, $threshold, $parentIndex = null) |
||
| 626 | |||
| 627 | public function toArray() |
||
| 631 | } |
||
| 632 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.