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 XhprofData 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 XhprofData, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class XhprofData { |
||
33 | |||
34 | /** |
||
35 | * @var array $config |
||
36 | */ |
||
37 | protected $config; |
||
38 | |||
39 | /** |
||
40 | * Hierarchical profiling data returned by xhprof. |
||
41 | * @var array $hieraData |
||
42 | */ |
||
43 | protected $hieraData; |
||
44 | |||
45 | /** |
||
46 | * Per-function inclusive data. |
||
47 | * @var array $inclusive |
||
48 | */ |
||
49 | protected $inclusive; |
||
50 | |||
51 | /** |
||
52 | * Per-function inclusive and exclusive data. |
||
53 | * @var array $complete |
||
54 | */ |
||
55 | protected $complete; |
||
56 | |||
57 | /** |
||
58 | * Configuration data can contain: |
||
59 | * - include: Array of function names to include in profiling. |
||
60 | * - sort: Key to sort per-function reports on. |
||
61 | * |
||
62 | * @param array $data Xhprof profiling data, as returned by xhprof_disable() |
||
63 | * @param array $config |
||
64 | */ |
||
65 | public function __construct( array $data, array $config = [] ) { |
||
73 | |||
74 | /** |
||
75 | * Get raw data collected by xhprof. |
||
76 | * |
||
77 | * Each key in the returned array is an edge label for the call graph in |
||
78 | * the form "caller==>callee". There is once special case edge labled |
||
79 | * simply "main()" which represents the global scope entry point of the |
||
80 | * application. |
||
81 | * |
||
82 | * XHProf will collect different data depending on the flags that are used: |
||
83 | * - ct: Number of matching events seen. |
||
84 | * - wt: Inclusive elapsed wall time for this event in microseconds. |
||
85 | * - cpu: Inclusive elapsed cpu time for this event in microseconds. |
||
86 | * (XHPROF_FLAGS_CPU) |
||
87 | * - mu: Delta of memory usage from start to end of callee in bytes. |
||
88 | * (XHPROF_FLAGS_MEMORY) |
||
89 | * - pmu: Delta of peak memory usage from start to end of callee in |
||
90 | * bytes. (XHPROF_FLAGS_MEMORY) |
||
91 | * - alloc: Delta of amount memory requested from malloc() by the callee, |
||
92 | * in bytes. (XHPROF_FLAGS_MALLOC) |
||
93 | * - free: Delta of amount of memory passed to free() by the callee, in |
||
94 | * bytes. (XHPROF_FLAGS_MALLOC) |
||
95 | * |
||
96 | * @return array |
||
97 | * @see getInclusiveMetrics() |
||
98 | * @see getCompleteMetrics() |
||
99 | */ |
||
100 | public function getRawData() { |
||
103 | |||
104 | /** |
||
105 | * Convert an xhprof data key into an array of ['parent', 'child'] |
||
106 | * function names. |
||
107 | * |
||
108 | * The resulting array is left padded with nulls, so a key |
||
109 | * with no parent (eg 'main()') will return [null, 'function']. |
||
110 | * |
||
111 | * @return array |
||
112 | */ |
||
113 | public static function splitKey( $key ) { |
||
116 | |||
117 | /** |
||
118 | * Remove data for functions that are not included in the 'include' |
||
119 | * configuration array. |
||
120 | * |
||
121 | * @param array $data Raw xhprof data |
||
122 | * @return array |
||
123 | */ |
||
124 | protected function pruneData( $data ) { |
||
141 | |||
142 | /** |
||
143 | * Get the inclusive metrics for each function call. Inclusive metrics |
||
144 | * for given function include the metrics for all functions that were |
||
145 | * called from that function during the measurement period. |
||
146 | * |
||
147 | * See getRawData() for a description of the metric that are returned for |
||
148 | * each funcition call. The values for the wt, cpu, mu and pmu metrics are |
||
149 | * arrays with these values: |
||
150 | * - total: Cumulative value |
||
151 | * - min: Minimum value |
||
152 | * - mean: Mean (average) value |
||
153 | * - max: Maximum value |
||
154 | * - variance: Variance (spread) of the values |
||
155 | * |
||
156 | * @return array |
||
157 | * @see getRawData() |
||
158 | * @see getCompleteMetrics() |
||
159 | */ |
||
160 | public function getInclusiveMetrics() { |
||
234 | |||
235 | /** |
||
236 | * Get the inclusive and exclusive metrics for each function call. |
||
237 | * |
||
238 | * In addition to the normal data contained in the inclusive metrics, the |
||
239 | * metrics have an additional 'exclusive' measurement which is the total |
||
240 | * minus the totals of all child function calls. |
||
241 | * |
||
242 | * @return array |
||
243 | * @see getRawData() |
||
244 | * @see getInclusiveMetrics() |
||
245 | */ |
||
246 | public function getCompleteMetrics() { |
||
295 | |||
296 | /** |
||
297 | * Get a list of all callers of a given function. |
||
298 | * |
||
299 | * @param string $function Function name |
||
300 | * @return array |
||
301 | * @see getEdges() |
||
302 | */ |
||
303 | View Code Duplication | public function getCallers( $function ) { |
|
311 | |||
312 | /** |
||
313 | * Get a list of all callees from a given function. |
||
314 | * |
||
315 | * @param string $function Function name |
||
316 | * @return array |
||
317 | * @see getEdges() |
||
318 | */ |
||
319 | View Code Duplication | public function getCallees( $function ) { |
|
327 | |||
328 | /** |
||
329 | * Find the critical path for the given metric. |
||
330 | * |
||
331 | * @param string $metric Metric to find critical path for |
||
332 | * @return array |
||
333 | */ |
||
334 | public function getCriticalPath( $metric = 'wt' ) { |
||
360 | |||
361 | /** |
||
362 | * Make a closure to use as a sort function. The resulting function will |
||
363 | * sort by descending numeric values (largest value first). |
||
364 | * |
||
365 | * @param string $key Data key to sort on |
||
366 | * @param string $sub Sub key to sort array values on |
||
367 | * @return Closure |
||
368 | */ |
||
369 | public static function makeSortFunction( $key, $sub ) { |
||
384 | } |
||
385 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: