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 PHP_CodeCoverage_Report_Node_File 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 PHP_CodeCoverage_Report_Node_File, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class PHP_CodeCoverage_Report_Node_File extends PHP_CodeCoverage_Report_Node |
||
|
|||
17 | { |
||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $coverageData; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $testData; |
||
27 | |||
28 | /** |
||
29 | * @var int |
||
30 | */ |
||
31 | protected $numExecutableLines = 0; |
||
32 | |||
33 | /** |
||
34 | * @var int |
||
35 | */ |
||
36 | protected $numExecutedLines = 0; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $classes = array(); |
||
42 | |||
43 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $traits = array(); |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $functions = array(); |
||
52 | |||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $linesOfCode = array(); |
||
57 | |||
58 | /** |
||
59 | * @var int |
||
60 | */ |
||
61 | protected $numTestedTraits = 0; |
||
62 | |||
63 | /** |
||
64 | * @var int |
||
65 | */ |
||
66 | protected $numTestedClasses = 0; |
||
67 | |||
68 | /** |
||
69 | * @var int |
||
70 | */ |
||
71 | protected $numMethods = null; |
||
72 | |||
73 | /** |
||
74 | * @var int |
||
75 | */ |
||
76 | protected $numTestedMethods = null; |
||
77 | |||
78 | /** |
||
79 | * @var int |
||
80 | */ |
||
81 | protected $numTestedFunctions = null; |
||
82 | |||
83 | /** |
||
84 | * @var array |
||
85 | */ |
||
86 | protected $startLines = array(); |
||
87 | |||
88 | /** |
||
89 | * @var array |
||
90 | */ |
||
91 | protected $endLines = array(); |
||
92 | |||
93 | /** |
||
94 | * @var bool |
||
95 | */ |
||
96 | protected $cacheTokens; |
||
97 | |||
98 | /** |
||
99 | * Constructor. |
||
100 | * |
||
101 | * @param string $name |
||
102 | * @param PHP_CodeCoverage_Report_Node $parent |
||
103 | * @param array $coverageData |
||
104 | * @param array $testData |
||
105 | * @param bool $cacheTokens |
||
106 | * @throws PHP_CodeCoverage_Exception |
||
107 | */ |
||
108 | public function __construct($name, PHP_CodeCoverage_Report_Node $parent, array $coverageData, array $testData, $cacheTokens) |
||
125 | |||
126 | /** |
||
127 | * Returns the number of files in/under this node. |
||
128 | * |
||
129 | * @return int |
||
130 | */ |
||
131 | public function count() |
||
135 | |||
136 | /** |
||
137 | * Returns the code coverage data of this node. |
||
138 | * |
||
139 | * @return array |
||
140 | */ |
||
141 | public function getCoverageData() |
||
145 | |||
146 | /** |
||
147 | * Returns the test data of this node. |
||
148 | * |
||
149 | * @return array |
||
150 | */ |
||
151 | public function getTestData() |
||
155 | |||
156 | /** |
||
157 | * Returns the classes of this node. |
||
158 | * |
||
159 | * @return array |
||
160 | */ |
||
161 | public function getClasses() |
||
165 | |||
166 | /** |
||
167 | * Returns the traits of this node. |
||
168 | * |
||
169 | * @return array |
||
170 | */ |
||
171 | public function getTraits() |
||
175 | |||
176 | /** |
||
177 | * Returns the functions of this node. |
||
178 | * |
||
179 | * @return array |
||
180 | */ |
||
181 | public function getFunctions() |
||
185 | |||
186 | /** |
||
187 | * Returns the LOC/CLOC/NCLOC of this node. |
||
188 | * |
||
189 | * @return array |
||
190 | */ |
||
191 | public function getLinesOfCode() |
||
195 | |||
196 | /** |
||
197 | * Returns the number of executable lines. |
||
198 | * |
||
199 | * @return int |
||
200 | */ |
||
201 | public function getNumExecutableLines() |
||
205 | |||
206 | /** |
||
207 | * Returns the number of executed lines. |
||
208 | * |
||
209 | * @return int |
||
210 | */ |
||
211 | public function getNumExecutedLines() |
||
215 | |||
216 | /** |
||
217 | * Returns the number of classes. |
||
218 | * |
||
219 | * @return int |
||
220 | */ |
||
221 | public function getNumClasses() |
||
225 | |||
226 | /** |
||
227 | * Returns the number of tested classes. |
||
228 | * |
||
229 | * @return int |
||
230 | */ |
||
231 | public function getNumTestedClasses() |
||
235 | |||
236 | /** |
||
237 | * Returns the number of traits. |
||
238 | * |
||
239 | * @return int |
||
240 | */ |
||
241 | public function getNumTraits() |
||
245 | |||
246 | /** |
||
247 | * Returns the number of tested traits. |
||
248 | * |
||
249 | * @return int |
||
250 | */ |
||
251 | public function getNumTestedTraits() |
||
255 | |||
256 | /** |
||
257 | * Returns the number of methods. |
||
258 | * |
||
259 | * @return int |
||
260 | */ |
||
261 | public function getNumMethods() |
||
285 | |||
286 | /** |
||
287 | * Returns the number of tested methods. |
||
288 | * |
||
289 | * @return int |
||
290 | */ |
||
291 | public function getNumTestedMethods() |
||
317 | |||
318 | /** |
||
319 | * Returns the number of functions. |
||
320 | * |
||
321 | * @return int |
||
322 | */ |
||
323 | public function getNumFunctions() |
||
327 | |||
328 | /** |
||
329 | * Returns the number of tested functions. |
||
330 | * |
||
331 | * @return int |
||
332 | */ |
||
333 | public function getNumTestedFunctions() |
||
348 | |||
349 | /** |
||
350 | * Calculates coverage statistics for the file. |
||
351 | */ |
||
352 | protected function calculateStatistics() |
||
532 | |||
533 | /** |
||
534 | * @param PHP_Token_Stream $tokens |
||
535 | */ |
||
536 | View Code Duplication | protected function processClasses(PHP_Token_Stream $tokens) |
|
579 | |||
580 | /** |
||
581 | * @param PHP_Token_Stream $tokens |
||
582 | */ |
||
583 | View Code Duplication | protected function processTraits(PHP_Token_Stream $tokens) |
|
626 | |||
627 | /** |
||
628 | * @param PHP_Token_Stream $tokens |
||
629 | */ |
||
630 | protected function processFunctions(PHP_Token_Stream $tokens) |
||
654 | |||
655 | /** |
||
656 | * Calculates the Change Risk Anti-Patterns (CRAP) index for a unit of code |
||
657 | * based on its cyclomatic complexity and percentage of code coverage. |
||
658 | * |
||
659 | * @param int $ccn |
||
660 | * @param float $coverage |
||
661 | * @return string |
||
662 | * @since Method available since Release 1.2.0 |
||
663 | */ |
||
664 | protected function crap($ccn, $coverage) |
||
679 | } |
||
680 |
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.