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 |
||
15 | class Profile |
||
16 | { |
||
17 | /** |
||
18 | * @const Key used for methods with no parent |
||
19 | */ |
||
20 | private const NO_PARENT = '__xhgui_top__'; |
||
21 | |||
22 | private $data; |
||
23 | private $collapsed; |
||
24 | private $indexed; |
||
25 | private $visited; |
||
26 | private $links; |
||
27 | private $nodes; |
||
28 | |||
29 | private $keys = ['ct', 'wt', 'cpu', 'mu', 'pmu']; |
||
30 | private $exclusiveKeys = ['ewt', 'ecpu', 'emu', 'epmu']; |
||
31 | private $functionCount; |
||
32 | |||
33 | public function __construct(array $profile, $convert = true) |
||
46 | |||
47 | /** |
||
48 | * Convert the raw data into a flatter list that is easier to use. |
||
49 | * |
||
50 | * This removes some of the parentage detail as all calls of a given |
||
51 | * method are aggregated. We are not able to maintain a full tree structure |
||
52 | * in any case, as xhprof only keeps one level of detail. |
||
53 | */ |
||
54 | private function process(): void |
||
55 | { |
||
56 | $result = []; |
||
57 | foreach ($this->data['profile'] as $name => $values) { |
||
58 | [$parent, $func] = $this->splitName($name); |
||
59 | // normalize, fill all missing keys |
||
60 | $values += [ |
||
61 | 'ct' => 0, |
||
62 | 'wt' => 0, |
||
63 | 'cpu' => 0, |
||
64 | 'mu' => 0, |
||
65 | 'pmu' => 0, |
||
66 | ]; |
||
67 | |||
68 | // Generate collapsed data. |
||
69 | if (isset($result[$func])) { |
||
70 | $result[$func] = $this->_sumKeys($result[$func], $values); |
||
71 | $result[$func]['parents'][] = $parent; |
||
72 | } else { |
||
73 | $result[$func] = $values; |
||
74 | $result[$func]['parents'] = [$parent]; |
||
75 | } |
||
76 | |||
77 | // Build the indexed data. |
||
78 | if ($parent === null) { |
||
79 | $parent = self::NO_PARENT; |
||
80 | } |
||
81 | if (!isset($this->indexed[$parent])) { |
||
82 | $this->indexed[$parent] = []; |
||
83 | } |
||
84 | $this->indexed[$parent][$func] = $values; |
||
85 | } |
||
86 | $this->collapsed = $result; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Sum up the values in $this->_keys; |
||
91 | * |
||
92 | * @param array $a The first set of profile data |
||
93 | * @param array $b the second set of profile data |
||
94 | * @return array merged profile data |
||
95 | */ |
||
96 | protected function _sumKeys($a, $b) |
||
107 | |||
108 | protected function _diffKeys($a, $b, $includeSelf = true) |
||
120 | |||
121 | protected function _diffPercentKeys($a, $b, $includeSelf = true) |
||
138 | |||
139 | /** |
||
140 | * Get the profile run data. |
||
141 | * |
||
142 | * TODO remove this and move all the features using it into this/ |
||
143 | * other classes. |
||
144 | * |
||
145 | * @return array |
||
146 | */ |
||
147 | public function getProfile() |
||
151 | |||
152 | public function getId() |
||
156 | |||
157 | public function getDate() |
||
166 | |||
167 | /** |
||
168 | * Get meta data about the profile. Read's a . split path |
||
169 | * out of the meta data in a profile. For example `SERVER.REQUEST_TIME` |
||
170 | * |
||
171 | * @param string $key the dotted key to read |
||
172 | * @return mixed|null null on failure, otherwise the stored value |
||
173 | */ |
||
174 | public function getMeta($key = null) |
||
191 | |||
192 | /** |
||
193 | * Read data from the profile run. |
||
194 | * |
||
195 | * @param string $key the function key name to read |
||
196 | * @param string $metric the metric to read |
||
197 | * @return float|null |
||
198 | */ |
||
199 | public function get($key, $metric = null) |
||
213 | |||
214 | /** |
||
215 | * Find a function matching a watched function. |
||
216 | * |
||
217 | * @param string $pattern the pattern to look for |
||
218 | * @return array|null an list of matching functions |
||
219 | * or null |
||
220 | */ |
||
221 | public function getWatched($pattern) |
||
241 | |||
242 | /** |
||
243 | * Find the parent and children method/functions for a given |
||
244 | * symbol. |
||
245 | * |
||
246 | * The parent/children arrays will contain all the callers + callees |
||
247 | * of the symbol given. The current index will give the total |
||
248 | * inclusive values for all properties. |
||
249 | * |
||
250 | * @param string $symbol the name of the function/method to find |
||
251 | * relatives for |
||
252 | * @param string $metric the metric to compare $threshold with |
||
253 | * @param float $threshold The threshold to exclude child functions at. Any |
||
254 | * function that represents less than this percentage of the current metric |
||
255 | * will be filtered out. |
||
256 | * @return array List of (parent, current, children) |
||
257 | */ |
||
258 | public function getRelatives($symbol, $metric = null, $threshold = 0) |
||
278 | |||
279 | /** |
||
280 | * Get the parent methods for a given symbol. |
||
281 | * |
||
282 | * @param string $symbol the name of the function/method to find |
||
283 | * parents for |
||
284 | * @return array List of parents |
||
285 | */ |
||
286 | protected function _getParents($symbol) |
||
298 | |||
299 | /** |
||
300 | * Find symbols that are the children of the given name. |
||
301 | * |
||
302 | * @param string $symbol the name of the function to find children of |
||
303 | * @param string $metric the metric to compare $threshold with |
||
304 | * @param float $threshold The threshold to exclude functions at. Any |
||
305 | * function that represents less than |
||
306 | * @return array an array of child methods |
||
307 | */ |
||
308 | protected function _getChildren($symbol, $metric = null, $threshold = 0) |
||
335 | |||
336 | /** |
||
337 | * Extracts a single dimension of data |
||
338 | * from a profile run. |
||
339 | * |
||
340 | * Useful for creating bar/column graphs. |
||
341 | * The profile data will be sorted by the column |
||
342 | * and then the $limit records will be extracted. |
||
343 | * |
||
344 | * @param string $dimension The dimension to extract |
||
345 | * @param int $limit Number of elements to pull |
||
346 | * @return array array of data with name = function name and |
||
347 | * value = the dimension |
||
348 | */ |
||
349 | public function extractDimension($dimension, $limit) |
||
363 | |||
364 | /** |
||
365 | * Generate the approximate exclusive values for each metric. |
||
366 | * |
||
367 | * We get a==>b as the name, we need a key for a and b in the array |
||
368 | * to get exclusive values for A we need to subtract the values of B (and any other children); |
||
369 | * call passing in the entire profile only, should return an array of |
||
370 | * functions with their regular timing, and exclusive numbers inside ['exclusive'] |
||
371 | * |
||
372 | * Consider: |
||
373 | * /---c---d---e |
||
374 | * a -/----b---d---e |
||
375 | * |
||
376 | * We have c==>d and b==>d, and in both instances d invokes e, yet we will |
||
377 | * have but a single d==>e result. This is a known and documented limitation of XHProf |
||
378 | * |
||
379 | * We have one d==>e entry, with some values, including ct=2 |
||
380 | * We also have c==>d and b==>d |
||
381 | * |
||
382 | * We should determine how many ==>d options there are, and equally |
||
383 | * split the cost of d==>e across them since d==>e represents the sum total of all calls. |
||
384 | * |
||
385 | * Notes: |
||
386 | * Function names are not unique, but we're merging them |
||
387 | * |
||
388 | * @return Profile a new instance with exclusive data set |
||
389 | */ |
||
390 | public function calculateSelf() |
||
417 | |||
418 | /** |
||
419 | * Sort data by a dimension. |
||
420 | * |
||
421 | * @param string $dimension the dimension to sort by |
||
422 | * @param array $data the data to sort |
||
423 | * @return array the sorted data |
||
424 | */ |
||
425 | public function sort($dimension, $data) |
||
438 | |||
439 | /** |
||
440 | * @param array $profileData |
||
441 | * @param array $filters |
||
442 | * |
||
443 | * @return array |
||
444 | */ |
||
445 | public function filter($profileData, $filters = []) |
||
457 | |||
458 | /** |
||
459 | * Split a key name into the parent==>child format. |
||
460 | * |
||
461 | * @param string $name the name to split |
||
462 | * @return array An array of parent, child. parent will be null if there |
||
463 | * is no parent. |
||
464 | */ |
||
465 | public function splitName($name) |
||
474 | |||
475 | /** |
||
476 | * Get the total number of tracked function calls in this run. |
||
477 | * |
||
478 | * @return int |
||
479 | */ |
||
480 | public function getFunctionCount() |
||
493 | |||
494 | /** |
||
495 | * Compare this run to another run. |
||
496 | * |
||
497 | * @param Profile $head The other run to compare with |
||
498 | * @return array an array of comparison data |
||
499 | */ |
||
500 | public function compare(self $head) |
||
533 | |||
534 | /** |
||
535 | * Get the max value for any give metric. |
||
536 | * |
||
537 | * @param string $metric the metric to get a max value for |
||
538 | */ |
||
539 | protected function _maxValue($metric) |
||
553 | |||
554 | /** |
||
555 | * Return a structured array suitable for generating callgraph visualizations. |
||
556 | * |
||
557 | * Functions whose inclusive time is less than 2% of the total time will |
||
558 | * be excluded from the callgraph data. |
||
559 | * |
||
560 | * @return array |
||
561 | */ |
||
562 | public function getCallgraph($metric = 'wt', $threshold = 0.01) |
||
589 | |||
590 | private function callgraphData($parentName, $main, $metric, $threshold, $parentIndex = null): void |
||
591 | { |
||
592 | // Leaves don't have children, and don't have links/nodes to add. |
||
593 | if (!isset($this->indexed[$parentName])) { |
||
594 | return; |
||
595 | } |
||
596 | |||
597 | $children = $this->indexed[$parentName]; |
||
598 | foreach ($children as $childName => $metrics) { |
||
599 | $metrics = $this->collapsed[$childName]; |
||
600 | if ($metrics[$metric] / $main <= $threshold) { |
||
601 | continue; |
||
602 | } |
||
603 | $revisit = false; |
||
604 | |||
605 | // Keep track of which nodes we've visited and their position |
||
606 | // in the node list. |
||
607 | if (!isset($this->visited[$childName])) { |
||
608 | $index = count($this->nodes); |
||
609 | $this->visited[$childName] = $index; |
||
610 | |||
611 | $this->nodes[] = [ |
||
612 | 'name' => $childName, |
||
613 | 'callCount' => $metrics['ct'], |
||
614 | 'value' => $metrics[$metric], |
||
615 | ]; |
||
616 | } else { |
||
617 | $revisit = true; |
||
618 | $index = $this->visited[$childName]; |
||
619 | } |
||
620 | |||
621 | if ($parentIndex !== null) { |
||
622 | $this->links[] = [ |
||
623 | 'source' => $parentName, |
||
624 | 'target' => $childName, |
||
625 | 'callCount' => $metrics['ct'], |
||
626 | ]; |
||
627 | } |
||
628 | |||
629 | // If the current function has more children, |
||
630 | // walk that call subgraph. |
||
631 | if (isset($this->indexed[$childName]) && !$revisit) { |
||
632 | $this->callgraphData($childName, $main, $metric, $threshold, $index); |
||
633 | } |
||
634 | } |
||
635 | } |
||
636 | |||
637 | public function toArray() |
||
641 | } |
||
642 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: