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 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, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class PHP_CodeCoverage |
||
|
|||
19 | { |
||
20 | /** |
||
21 | * @var PHP_CodeCoverage_Driver |
||
22 | */ |
||
23 | private $driver; |
||
24 | |||
25 | /** |
||
26 | * @var PHP_CodeCoverage_Filter |
||
27 | */ |
||
28 | private $filter; |
||
29 | |||
30 | /** |
||
31 | * @var bool |
||
32 | */ |
||
33 | private $cacheTokens = false; |
||
34 | |||
35 | /** |
||
36 | * @var bool |
||
37 | */ |
||
38 | private $checkForUnintentionallyCoveredCode = false; |
||
39 | |||
40 | /** |
||
41 | * @var bool |
||
42 | */ |
||
43 | private $forceCoversAnnotation = false; |
||
44 | |||
45 | /** |
||
46 | * @var bool |
||
47 | */ |
||
48 | private $mapTestClassNameToCoveredClassName = false; |
||
49 | |||
50 | /** |
||
51 | * @var bool |
||
52 | */ |
||
53 | private $addUncoveredFilesFromWhitelist = true; |
||
54 | |||
55 | /** |
||
56 | * @var bool |
||
57 | */ |
||
58 | private $processUncoveredFilesFromWhitelist = false; |
||
59 | |||
60 | /** |
||
61 | * @var mixed |
||
62 | */ |
||
63 | private $currentId; |
||
64 | |||
65 | /** |
||
66 | * Code coverage data. |
||
67 | * |
||
68 | * @var array |
||
69 | */ |
||
70 | private $data = array(); |
||
71 | |||
72 | /** |
||
73 | * @var array |
||
74 | */ |
||
75 | private $ignoredLines = array(); |
||
76 | |||
77 | /** |
||
78 | * @var bool |
||
79 | */ |
||
80 | private $disableIgnoredLines = false; |
||
81 | |||
82 | /** |
||
83 | * Test data. |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | private $tests = array(); |
||
88 | |||
89 | /** |
||
90 | * Constructor. |
||
91 | * |
||
92 | * @param PHP_CodeCoverage_Driver $driver |
||
93 | * @param PHP_CodeCoverage_Filter $filter |
||
94 | * @throws PHP_CodeCoverage_Exception |
||
95 | */ |
||
96 | public function __construct(PHP_CodeCoverage_Driver $driver = null, PHP_CodeCoverage_Filter $filter = null) |
||
109 | |||
110 | /** |
||
111 | * Returns the PHP_CodeCoverage_Report_Node_* object graph |
||
112 | * for this PHP_CodeCoverage object. |
||
113 | * |
||
114 | * @return PHP_CodeCoverage_Report_Node_Directory |
||
115 | * @since Method available since Release 1.1.0 |
||
116 | */ |
||
117 | public function getReport() |
||
123 | |||
124 | /** |
||
125 | * Clears collected code coverage data. |
||
126 | */ |
||
127 | public function clear() |
||
133 | |||
134 | /** |
||
135 | * Returns the PHP_CodeCoverage_Filter used. |
||
136 | * |
||
137 | * @return PHP_CodeCoverage_Filter |
||
138 | */ |
||
139 | public function filter() |
||
143 | |||
144 | /** |
||
145 | * Returns the collected code coverage data. |
||
146 | * Set $raw = true to bypass all filters. |
||
147 | * |
||
148 | * @param bool $raw |
||
149 | * @return array |
||
150 | * @since Method available since Release 1.1.0 |
||
151 | */ |
||
152 | public function getData($raw = false) |
||
166 | |||
167 | /** |
||
168 | * Sets the coverage data. |
||
169 | * |
||
170 | * @param array $data |
||
171 | * @since Method available since Release 2.0.0 |
||
172 | */ |
||
173 | public function setData(array $data) |
||
177 | |||
178 | /** |
||
179 | * Returns the test data. |
||
180 | * |
||
181 | * @return array |
||
182 | * @since Method available since Release 1.1.0 |
||
183 | */ |
||
184 | public function getTests() |
||
188 | |||
189 | /** |
||
190 | * Sets the test data. |
||
191 | * |
||
192 | * @param array $tests |
||
193 | * @since Method available since Release 2.0.0 |
||
194 | */ |
||
195 | public function setTests(array $tests) |
||
199 | |||
200 | /** |
||
201 | * Start collection of code coverage information. |
||
202 | * |
||
203 | * @param mixed $id |
||
204 | * @param bool $clear |
||
205 | * @throws PHP_CodeCoverage_Exception |
||
206 | */ |
||
207 | public function start($id, $clear = false) |
||
224 | |||
225 | /** |
||
226 | * Stop collection of code coverage information. |
||
227 | * |
||
228 | * @param bool $append |
||
229 | * @param mixed $linesToBeCovered |
||
230 | * @param array $linesToBeUsed |
||
231 | * @return array |
||
232 | * @throws PHP_CodeCoverage_Exception |
||
233 | */ |
||
234 | public function stop($append = true, $linesToBeCovered = array(), array $linesToBeUsed = array()) |
||
257 | |||
258 | /** |
||
259 | * Appends code coverage data. |
||
260 | * |
||
261 | * @param array $data |
||
262 | * @param mixed $id |
||
263 | * @param bool $append |
||
264 | * @param mixed $linesToBeCovered |
||
265 | * @param array $linesToBeUsed |
||
266 | * @throws PHP_CodeCoverage_Exception |
||
267 | */ |
||
268 | public function append(array $data, $id = null, $append = true, $linesToBeCovered = array(), array $linesToBeUsed = array()) |
||
335 | |||
336 | /** |
||
337 | * Merges the data from another instance of PHP_CodeCoverage. |
||
338 | * |
||
339 | * @param PHP_CodeCoverage $that |
||
340 | */ |
||
341 | public function merge(PHP_CodeCoverage $that) |
||
376 | |||
377 | /** |
||
378 | * @param bool $flag |
||
379 | * @throws PHP_CodeCoverage_Exception |
||
380 | * @since Method available since Release 1.1.0 |
||
381 | */ |
||
382 | public function setCacheTokens($flag) |
||
393 | |||
394 | /** |
||
395 | * @since Method available since Release 1.1.0 |
||
396 | */ |
||
397 | public function getCacheTokens() |
||
401 | |||
402 | /** |
||
403 | * @param bool $flag |
||
404 | * @throws PHP_CodeCoverage_Exception |
||
405 | * @since Method available since Release 2.0.0 |
||
406 | */ |
||
407 | public function setCheckForUnintentionallyCoveredCode($flag) |
||
418 | |||
419 | /** |
||
420 | * @param bool $flag |
||
421 | * @throws PHP_CodeCoverage_Exception |
||
422 | */ |
||
423 | public function setForceCoversAnnotation($flag) |
||
434 | |||
435 | /** |
||
436 | * @param bool $flag |
||
437 | * @throws PHP_CodeCoverage_Exception |
||
438 | */ |
||
439 | public function setMapTestClassNameToCoveredClassName($flag) |
||
450 | |||
451 | /** |
||
452 | * @param bool $flag |
||
453 | * @throws PHP_CodeCoverage_Exception |
||
454 | */ |
||
455 | public function setAddUncoveredFilesFromWhitelist($flag) |
||
466 | |||
467 | /** |
||
468 | * @param bool $flag |
||
469 | * @throws PHP_CodeCoverage_Exception |
||
470 | */ |
||
471 | public function setProcessUncoveredFilesFromWhitelist($flag) |
||
482 | |||
483 | /** |
||
484 | * @param bool $flag |
||
485 | * @throws PHP_CodeCoverage_Exception |
||
486 | */ |
||
487 | public function setDisableIgnoredLines($flag) |
||
498 | |||
499 | /** |
||
500 | * Applies the @covers annotation filtering. |
||
501 | * |
||
502 | * @param array $data |
||
503 | * @param mixed $linesToBeCovered |
||
504 | * @param array $linesToBeUsed |
||
505 | * @throws PHP_CodeCoverage_Exception_UnintentionallyCoveredCode |
||
506 | */ |
||
507 | private function applyCoversAnnotationFilter(array &$data, $linesToBeCovered, array $linesToBeUsed) |
||
539 | |||
540 | /** |
||
541 | * Applies the blacklist/whitelist filtering. |
||
542 | * |
||
543 | * @param array $data |
||
544 | */ |
||
545 | private function applyListsFilter(array &$data) |
||
553 | |||
554 | /** |
||
555 | * Applies the "ignored lines" filtering. |
||
556 | * |
||
557 | * @param array $data |
||
558 | */ |
||
559 | private function applyIgnoredLinesFilter(array &$data) |
||
571 | |||
572 | /** |
||
573 | * @param array $data |
||
574 | * @since Method available since Release 1.1.0 |
||
575 | */ |
||
576 | private function initializeFilesThatAreSeenTheFirstTime(array $data) |
||
588 | |||
589 | /** |
||
590 | * Processes whitelisted files that are not covered. |
||
591 | */ |
||
592 | private function addUncoveredFilesFromWhitelist() |
||
624 | |||
625 | /** |
||
626 | * @param string $uncoveredFile |
||
627 | * @param array $data |
||
628 | * @param array $uncoveredFiles |
||
629 | */ |
||
630 | private function processUncoveredFileFromWhitelist($uncoveredFile, array &$data, array $uncoveredFiles) |
||
649 | |||
650 | /** |
||
651 | * Returns the lines of a source file that should be ignored. |
||
652 | * |
||
653 | * @param string $filename |
||
654 | * @return array |
||
655 | * @throws PHP_CodeCoverage_Exception |
||
656 | * @since Method available since Release 2.0.0 |
||
657 | */ |
||
658 | private function getLinesToBeIgnored($filename) |
||
821 | |||
822 | /** |
||
823 | * @param array $data |
||
824 | * @param array $linesToBeCovered |
||
825 | * @param array $linesToBeUsed |
||
826 | * @throws PHP_CodeCoverage_Exception_UnintentionallyCoveredCode |
||
827 | * @since Method available since Release 2.0.0 |
||
828 | */ |
||
829 | private function performUnintentionallyCoveredCodeCheck(array &$data, array $linesToBeCovered, array $linesToBeUsed) |
||
858 | |||
859 | /** |
||
860 | * @param array $linesToBeCovered |
||
861 | * @param array $linesToBeUsed |
||
862 | * @return array |
||
863 | * @since Method available since Release 2.0.0 |
||
864 | */ |
||
865 | private function getAllowedLines(array $linesToBeCovered, array $linesToBeUsed) |
||
899 | |||
900 | /** |
||
901 | * @return PHP_CodeCoverage_Driver |
||
902 | * @throws PHP_CodeCoverage_Exception |
||
903 | */ |
||
904 | private function selectDriver() |
||
920 | } |
||
921 |
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.