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 | * 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() |
||
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) |
||
81 | |||
82 | protected function _diffKeys($a, $b, $includeSelf = true) |
||
83 | { |
||
84 | $keys = $this->_keys; |
||
85 | if ($includeSelf) { |
||
86 | $keys = array_merge($keys, $this->_exclusiveKeys); |
||
87 | } |
||
88 | foreach ($keys as $key) { |
||
89 | $a[$key] -= $b[$key]; |
||
90 | } |
||
91 | return $a; |
||
92 | } |
||
93 | |||
94 | protected function _diffPercentKeys($a, $b, $includeSelf = true) |
||
95 | { |
||
96 | $out = array(); |
||
97 | $keys = $this->_keys; |
||
98 | if ($includeSelf) { |
||
99 | $keys = array_merge($keys, $this->_exclusiveKeys); |
||
100 | } |
||
101 | foreach ($keys as $key) { |
||
102 | if ($b[$key] != 0) { |
||
103 | $out[$key] = $a[$key] / $b[$key]; |
||
104 | } else { |
||
105 | $out[$key] = -1; |
||
106 | } |
||
107 | } |
||
108 | return $out; |
||
109 | } |
||
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() |
||
123 | |||
124 | public function getId() |
||
128 | |||
129 | public function getDate() |
||
130 | { |
||
131 | $date = $this->getMeta('SERVER.REQUEST_TIME'); |
||
132 | if ($date) { |
||
133 | return new DateTime('@' . $date); |
||
134 | } |
||
135 | return new DateTime('now'); |
||
136 | } |
||
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) |
||
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) |
||
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) |
||
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) |
||
226 | { |
||
227 | $parents = array(); |
||
228 | |||
229 | // If the function doesn't exist, it won't have parents/children |
||
230 | if (empty($this->_collapsed[$symbol])) { |
||
231 | return array( |
||
232 | array(), |
||
233 | array(), |
||
234 | array(), |
||
235 | ); |
||
236 | } |
||
237 | $current = $this->_collapsed[$symbol]; |
||
238 | $current['function'] = $symbol; |
||
239 | |||
240 | $parents = $this->_getParents($symbol); |
||
241 | $children = $this->_getChildren($symbol, $metric, $threshold); |
||
242 | return array($parents, $current, $children); |
||
243 | } |
||
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) { |
||
253 | $parents = array(); |
||
254 | $current = $this->_collapsed[$symbol]; |
||
255 | foreach ($current['parents'] as $parent) { |
||
256 | if (isset($this->_collapsed[$parent])) { |
||
257 | $parents[] = array('function' => $parent) + $this->_collapsed[$parent]; |
||
258 | } |
||
259 | } |
||
260 | return $parents; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Find symbols that are the children of the given name. |
||
265 | * |
||
266 | * @param string $symbol The name of the function to find children of. |
||
267 | * @param string $metric The metric to compare $threshold with. |
||
268 | * @param float $threshold The threshold to exclude functions at. Any |
||
269 | * function that represents less than |
||
270 | * @return array An array of child methods. |
||
271 | */ |
||
272 | protected function _getChildren($symbol, $metric = null, $threshold = 0) { |
||
273 | $children = array(); |
||
274 | if (!isset($this->_indexed[$symbol])) { |
||
275 | return $children; |
||
276 | } |
||
277 | |||
278 | $total = 0; |
||
279 | if (isset($metric)) { |
||
280 | $top = $this->_indexed[self::NO_PARENT]; |
||
281 | // Not always 'main()' |
||
282 | $mainFunc = current($top); |
||
283 | $total = $mainFunc[$metric]; |
||
284 | } |
||
285 | |||
286 | foreach ($this->_indexed[$symbol] as $name => $data) { |
||
287 | if ( |
||
288 | $metric && $total > 0 && $threshold > 0 && |
||
289 | ($this->_collapsed[$name][$metric] / $total) < $threshold |
||
290 | ) { |
||
291 | continue; |
||
292 | } |
||
293 | $children[] = $data + array('function' => $name); |
||
294 | } |
||
295 | return $children; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Extracts a single dimension of data |
||
300 | * from a profile run. |
||
301 | * |
||
302 | * Useful for creating bar/column graphs. |
||
303 | * The profile data will be sorted by the column |
||
304 | * and then the $limit records will be extracted. |
||
305 | * |
||
306 | * @param string $dimension The dimension to extract |
||
307 | * @param int $limit Number of elements to pull |
||
308 | * @return array Array of data with name = function name and |
||
309 | * value = the dimension. |
||
310 | */ |
||
311 | public function extractDimension($dimension, $limit) |
||
324 | |||
325 | /** |
||
326 | * Generate the approximate exclusive values for each metric. |
||
327 | * |
||
328 | * We get a==>b as the name, we need a key for a and b in the array |
||
329 | * to get exclusive values for A we need to subtract the values of B (and any other children); |
||
330 | * call passing in the entire profile only, should return an array of |
||
331 | * functions with their regular timing, and exclusive numbers inside ['exclusive'] |
||
332 | * |
||
333 | * Consider: |
||
334 | * /---c---d---e |
||
335 | * a -/----b---d---e |
||
336 | * |
||
337 | * We have c==>d and b==>d, and in both instances d invokes e, yet we will |
||
338 | * have but a single d==>e result. This is a known and documented limitation of XHProf |
||
339 | * |
||
340 | * We have one d==>e entry, with some values, including ct=2 |
||
341 | * We also have c==>d and b==>d |
||
342 | * |
||
343 | * We should determine how many ==>d options there are, and equally |
||
344 | * split the cost of d==>e across them since d==>e represents the sum total of all calls. |
||
345 | * |
||
346 | * Notes: |
||
347 | * Function names are not unique, but we're merging them |
||
348 | * |
||
349 | * @return Xhgui_Profile A new instance with exclusive data set. |
||
350 | */ |
||
351 | public function calculateSelf() |
||
377 | |||
378 | /** |
||
379 | * Sort data by a dimension. |
||
380 | * |
||
381 | * @param string $dimension The dimension to sort by. |
||
382 | * @param array $data The data to sort. |
||
383 | * @return array The sorted data. |
||
384 | */ |
||
385 | public function sort($dimension, $data) |
||
396 | |||
397 | /** |
||
398 | * Split a key name into the parent==>child format. |
||
399 | * |
||
400 | * @param string $name The name to split. |
||
401 | * @return array An array of parent, child. parent will be null if there |
||
402 | * is no parent. |
||
403 | */ |
||
404 | public function splitName($name) |
||
412 | |||
413 | /** |
||
414 | * Get the total number of tracked function calls in this run. |
||
415 | * |
||
416 | * @return int |
||
417 | */ |
||
418 | public function getFunctionCount() |
||
430 | |||
431 | /** |
||
432 | * Compare this run to another run. |
||
433 | * |
||
434 | * @param Xhgui_Profile $head The other run to compare with |
||
435 | * @return array An array of comparison data. |
||
436 | */ |
||
437 | public function compare(Xhgui_Profile $head) { |
||
469 | |||
470 | /** |
||
471 | * Get the max value for any give metric. |
||
472 | * |
||
473 | * @param string $metric The metric to get a max value for. |
||
474 | */ |
||
475 | protected function _maxValue($metric) |
||
488 | |||
489 | /** |
||
490 | * Return a structured array suitable for generating callgraph visualizations. |
||
491 | * |
||
492 | * Functions whose inclusive time is less than 2% of the total time will |
||
493 | * be excluded from the callgraph data. |
||
494 | * |
||
495 | * @return array |
||
496 | */ |
||
497 | public function getCallgraph($metric = 'wt', $threshold = 0.01) |
||
523 | |||
524 | protected function _callgraphData($parentName, $main, $metric, $threshold, $parentIndex = null) |
||
570 | |||
571 | /** |
||
572 | * Return a structured array suitable for generating flamegraph visualizations. |
||
573 | * |
||
574 | * Functions whose inclusive time is less than 2% of the total time will |
||
575 | * be excluded from the callgraph data. |
||
576 | * |
||
577 | * @return array |
||
578 | */ |
||
579 | public function getFlamegraph($metric = 'wt', $threshold = 0.01) |
||
598 | |||
599 | protected function _flamegraphData($parentName, $main, $metric, $threshold, $parentIndex = null) |
||
645 | |||
646 | public function toArray() |
||
650 | |||
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.