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 $checkForUnexecutedCoveredCode = 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 bool |
||
62 | */ |
||
63 | private $ignoreDeprecatedCode = false; |
||
64 | |||
65 | /** |
||
66 | * @var mixed |
||
67 | */ |
||
68 | private $currentId; |
||
69 | |||
70 | /** |
||
71 | * Code coverage data. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | private $data = []; |
||
76 | |||
77 | /** |
||
78 | * @var array |
||
79 | */ |
||
80 | private $ignoredLines = []; |
||
81 | |||
82 | /** |
||
83 | * @var bool |
||
84 | */ |
||
85 | private $disableIgnoredLines = false; |
||
86 | |||
87 | /** |
||
88 | * Test data. |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | private $tests = []; |
||
93 | |||
94 | /** |
||
95 | * Constructor. |
||
96 | * |
||
97 | * @param PHP_CodeCoverage_Driver $driver |
||
98 | * @param PHP_CodeCoverage_Filter $filter |
||
99 | * @throws PHP_CodeCoverage_RuntimeException |
||
100 | */ |
||
101 | public function __construct(PHP_CodeCoverage_Driver $driver = null, PHP_CodeCoverage_Filter $filter = null) |
||
102 | { |
||
103 | if ($driver === null) { |
||
104 | $driver = $this->selectDriver(); |
||
105 | } |
||
106 | |||
107 | if ($filter === null) { |
||
108 | $filter = new PHP_CodeCoverage_Filter; |
||
109 | } |
||
110 | |||
111 | $this->driver = $driver; |
||
112 | $this->filter = $filter; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Returns the PHP_CodeCoverage_Report_Node_* object graph |
||
117 | * for this PHP_CodeCoverage object. |
||
118 | * |
||
119 | * @return PHP_CodeCoverage_Report_Node_Directory |
||
120 | * @since Method available since Release 1.1.0 |
||
121 | */ |
||
122 | public function getReport() |
||
123 | { |
||
124 | $factory = new PHP_CodeCoverage_Report_Factory; |
||
125 | |||
126 | return $factory->create($this); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Clears collected code coverage data. |
||
131 | */ |
||
132 | public function clear() |
||
133 | { |
||
134 | $this->currentId = null; |
||
135 | $this->data = []; |
||
136 | $this->tests = []; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Returns the PHP_CodeCoverage_Filter used. |
||
141 | * |
||
142 | * @return PHP_CodeCoverage_Filter |
||
143 | */ |
||
144 | public function filter() |
||
148 | |||
149 | /** |
||
150 | * Returns the collected code coverage data. |
||
151 | * Set $raw = true to bypass all filters. |
||
152 | * |
||
153 | * @param bool $raw |
||
154 | * @return array |
||
155 | * @since Method available since Release 1.1.0 |
||
156 | */ |
||
157 | public function getData($raw = false) |
||
158 | { |
||
159 | if (!$raw && $this->addUncoveredFilesFromWhitelist) { |
||
160 | $this->addUncoveredFilesFromWhitelist(); |
||
161 | } |
||
162 | |||
163 | return $this->data; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Sets the coverage data. |
||
168 | * |
||
169 | * @param array $data |
||
170 | * @since Method available since Release 2.0.0 |
||
171 | */ |
||
172 | public function setData(array $data) |
||
176 | |||
177 | /** |
||
178 | * Returns the test data. |
||
179 | * |
||
180 | * @return array |
||
181 | * @since Method available since Release 1.1.0 |
||
182 | */ |
||
183 | public function getTests() |
||
187 | |||
188 | /** |
||
189 | * Sets the test data. |
||
190 | * |
||
191 | * @param array $tests |
||
192 | * @since Method available since Release 2.0.0 |
||
193 | */ |
||
194 | public function setTests(array $tests) |
||
198 | |||
199 | /** |
||
200 | * Start collection of code coverage information. |
||
201 | * |
||
202 | * @param mixed $id |
||
203 | * @param bool $clear |
||
204 | * @throws PHP_CodeCoverage_InvalidArgumentException |
||
205 | */ |
||
206 | public function start($id, $clear = false) |
||
207 | { |
||
208 | if (!is_bool($clear)) { |
||
209 | throw PHP_CodeCoverage_InvalidArgumentException::create( |
||
210 | 1, |
||
211 | 'boolean' |
||
212 | ); |
||
213 | } |
||
214 | |||
215 | if ($clear) { |
||
216 | $this->clear(); |
||
217 | } |
||
218 | |||
219 | $this->currentId = $id; |
||
220 | |||
221 | $this->driver->start(); |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Stop collection of code coverage information. |
||
226 | * |
||
227 | * @param bool $append |
||
228 | * @param mixed $linesToBeCovered |
||
229 | * @param array $linesToBeUsed |
||
230 | * @return array |
||
231 | * @throws PHP_CodeCoverage_InvalidArgumentException |
||
232 | */ |
||
233 | public function stop($append = true, $linesToBeCovered = [], array $linesToBeUsed = []) |
||
234 | { |
||
235 | if (!is_bool($append)) { |
||
236 | throw PHP_CodeCoverage_InvalidArgumentException::create( |
||
237 | 1, |
||
238 | 'boolean' |
||
239 | ); |
||
240 | } |
||
241 | |||
242 | if (!is_array($linesToBeCovered) && $linesToBeCovered !== false) { |
||
243 | throw PHP_CodeCoverage_InvalidArgumentException::create( |
||
244 | 2, |
||
245 | 'array or false' |
||
246 | ); |
||
247 | } |
||
248 | |||
249 | $data = $this->driver->stop(); |
||
250 | $this->append($data, null, $append, $linesToBeCovered, $linesToBeUsed); |
||
251 | |||
252 | $this->currentId = null; |
||
253 | |||
254 | return $data; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Appends code coverage data. |
||
259 | * |
||
260 | * @param array $data |
||
261 | * @param mixed $id |
||
262 | * @param bool $append |
||
263 | * @param mixed $linesToBeCovered |
||
264 | * @param array $linesToBeUsed |
||
265 | * @throws PHP_CodeCoverage_RuntimeException |
||
266 | */ |
||
267 | public function append(array $data, $id = null, $append = true, $linesToBeCovered = [], array $linesToBeUsed = []) |
||
268 | { |
||
269 | if ($id === null) { |
||
270 | $id = $this->currentId; |
||
271 | } |
||
272 | |||
273 | if ($id === null) { |
||
274 | throw new PHP_CodeCoverage_RuntimeException; |
||
275 | } |
||
276 | |||
277 | $this->applyListsFilter($data); |
||
278 | $this->applyIgnoredLinesFilter($data); |
||
279 | $this->initializeFilesThatAreSeenTheFirstTime($data); |
||
280 | |||
281 | if (!$append) { |
||
282 | return; |
||
283 | } |
||
284 | |||
285 | if ($id != 'UNCOVERED_FILES_FROM_WHITELIST') { |
||
286 | $this->applyCoversAnnotationFilter( |
||
287 | $data, |
||
288 | $linesToBeCovered, |
||
289 | $linesToBeUsed |
||
290 | ); |
||
291 | } |
||
292 | |||
293 | if (empty($data)) { |
||
294 | return; |
||
295 | } |
||
296 | |||
297 | $size = 'unknown'; |
||
298 | $status = null; |
||
299 | |||
300 | if ($id instanceof PHPUnit_Framework_TestCase) { |
||
301 | $_size = $id->getSize(); |
||
302 | |||
303 | if ($_size == PHPUnit_Util_Test::SMALL) { |
||
304 | $size = 'small'; |
||
305 | } elseif ($_size == PHPUnit_Util_Test::MEDIUM) { |
||
306 | $size = 'medium'; |
||
307 | } elseif ($_size == PHPUnit_Util_Test::LARGE) { |
||
308 | $size = 'large'; |
||
309 | } |
||
310 | |||
311 | $status = $id->getStatus(); |
||
312 | $id = get_class($id) . '::' . $id->getName(); |
||
313 | } elseif ($id instanceof PHPUnit_Extensions_PhptTestCase) { |
||
314 | $size = 'large'; |
||
315 | $id = $id->getName(); |
||
316 | } |
||
317 | |||
318 | $this->tests[$id] = ['size' => $size, 'status' => $status]; |
||
319 | |||
320 | foreach ($data as $file => $lines) { |
||
321 | if (!$this->filter->isFile($file)) { |
||
322 | continue; |
||
323 | } |
||
324 | |||
325 | foreach ($lines as $k => $v) { |
||
326 | if ($v == PHP_CodeCoverage_Driver::LINE_EXECUTED) { |
||
327 | if (empty($this->data[$file][$k]) || !in_array($id, $this->data[$file][$k])) { |
||
328 | $this->data[$file][$k][] = $id; |
||
329 | } |
||
330 | } |
||
331 | } |
||
332 | } |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * Merges the data from another instance of PHP_CodeCoverage. |
||
337 | * |
||
338 | * @param PHP_CodeCoverage $that |
||
339 | */ |
||
340 | public function merge(PHP_CodeCoverage $that) |
||
341 | { |
||
342 | $this->filter->setWhitelistedFiles( |
||
343 | array_merge($this->filter->getWhitelistedFiles(), $that->filter()->getWhitelistedFiles()) |
||
344 | ); |
||
345 | |||
346 | foreach ($that->data as $file => $lines) { |
||
347 | if (!isset($this->data[$file])) { |
||
348 | if (!$this->filter->isFiltered($file)) { |
||
349 | $this->data[$file] = $lines; |
||
350 | } |
||
351 | |||
352 | continue; |
||
353 | } |
||
354 | |||
355 | foreach ($lines as $line => $data) { |
||
356 | if ($data !== null) { |
||
357 | if (!isset($this->data[$file][$line])) { |
||
358 | $this->data[$file][$line] = $data; |
||
359 | } else { |
||
360 | $this->data[$file][$line] = array_unique( |
||
361 | array_merge($this->data[$file][$line], $data) |
||
362 | ); |
||
363 | } |
||
364 | } |
||
365 | } |
||
366 | } |
||
367 | |||
368 | $this->tests = array_merge($this->tests, $that->getTests()); |
||
369 | |||
370 | } |
||
371 | |||
372 | /** |
||
373 | * @param bool $flag |
||
374 | * @throws PHP_CodeCoverage_InvalidArgumentException |
||
375 | * @since Method available since Release 1.1.0 |
||
376 | */ |
||
377 | public function setCacheTokens($flag) |
||
378 | { |
||
379 | if (!is_bool($flag)) { |
||
380 | throw PHP_CodeCoverage_InvalidArgumentException::create( |
||
381 | 1, |
||
382 | 'boolean' |
||
383 | ); |
||
384 | } |
||
385 | |||
386 | $this->cacheTokens = $flag; |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * @since Method available since Release 1.1.0 |
||
391 | */ |
||
392 | public function getCacheTokens() |
||
396 | |||
397 | /** |
||
398 | * @param bool $flag |
||
399 | * @throws PHP_CodeCoverage_InvalidArgumentException |
||
400 | * @since Method available since Release 2.0.0 |
||
401 | */ |
||
402 | public function setCheckForUnintentionallyCoveredCode($flag) |
||
403 | { |
||
404 | if (!is_bool($flag)) { |
||
405 | throw PHP_CodeCoverage_InvalidArgumentException::create( |
||
413 | |||
414 | /** |
||
415 | * @param bool $flag |
||
416 | * @throws PHP_CodeCoverage_InvalidArgumentException |
||
417 | */ |
||
418 | public function setForceCoversAnnotation($flag) |
||
429 | |||
430 | /** |
||
431 | * @param bool $flag |
||
432 | * @throws PHP_CodeCoverage_InvalidArgumentException |
||
433 | */ |
||
434 | public function setCheckForUnexecutedCoveredCode($flag) |
||
445 | |||
446 | /** |
||
447 | * @deprecated |
||
448 | * @param bool $flag |
||
449 | * @throws PHP_CodeCoverage_InvalidArgumentException |
||
450 | */ |
||
451 | public function setMapTestClassNameToCoveredClassName($flag) |
||
454 | |||
455 | /** |
||
456 | * @param bool $flag |
||
457 | * @throws PHP_CodeCoverage_InvalidArgumentException |
||
458 | */ |
||
459 | public function setAddUncoveredFilesFromWhitelist($flag) |
||
470 | |||
471 | /** |
||
472 | * @param bool $flag |
||
473 | * @throws PHP_CodeCoverage_InvalidArgumentException |
||
474 | */ |
||
475 | public function setProcessUncoveredFilesFromWhitelist($flag) |
||
486 | |||
487 | /** |
||
488 | * @param bool $flag |
||
489 | * @throws PHP_CodeCoverage_InvalidArgumentException |
||
490 | */ |
||
491 | public function setDisableIgnoredLines($flag) |
||
502 | |||
503 | /** |
||
504 | * @param bool $flag |
||
505 | * @throws PHP_CodeCoverage_InvalidArgumentException |
||
506 | * @since Method available since Release 3.0.2 |
||
507 | */ |
||
508 | public function setIgnoreDeprecatedCode($flag) |
||
519 | |||
520 | /** |
||
521 | * Applies the @covers annotation filtering. |
||
522 | * |
||
523 | * @param array $data |
||
524 | * @param mixed $linesToBeCovered |
||
525 | * @param array $linesToBeUsed |
||
526 | * @throws PHP_CodeCoverage_UnintentionallyCoveredCodeException |
||
527 | */ |
||
528 | private function applyCoversAnnotationFilter(array &$data, $linesToBeCovered, array $linesToBeUsed) |
||
564 | |||
565 | /** |
||
566 | * Applies the whitelist filtering. |
||
567 | * |
||
568 | * @param array $data |
||
569 | */ |
||
570 | private function applyListsFilter(array &$data) |
||
578 | |||
579 | /** |
||
580 | * Applies the "ignored lines" filtering. |
||
581 | * |
||
582 | * @param array $data |
||
583 | */ |
||
584 | private function applyIgnoredLinesFilter(array &$data) |
||
596 | |||
597 | /** |
||
598 | * @param array $data |
||
599 | * @since Method available since Release 1.1.0 |
||
600 | */ |
||
601 | private function initializeFilesThatAreSeenTheFirstTime(array $data) |
||
613 | |||
614 | /** |
||
615 | * Processes whitelisted files that are not covered. |
||
616 | */ |
||
617 | private function addUncoveredFilesFromWhitelist() |
||
649 | |||
650 | /** |
||
651 | * @param string $uncoveredFile |
||
652 | * @param array $data |
||
653 | * @param array $uncoveredFiles |
||
654 | */ |
||
655 | private function processUncoveredFileFromWhitelist($uncoveredFile, array &$data, array $uncoveredFiles) |
||
674 | |||
675 | /** |
||
676 | * Returns the lines of a source file that should be ignored. |
||
677 | * |
||
678 | * @param string $filename |
||
679 | * @return array |
||
680 | * @throws PHP_CodeCoverage_InvalidArgumentException |
||
681 | * @since Method available since Release 2.0.0 |
||
682 | */ |
||
683 | private function getLinesToBeIgnored($filename) |
||
848 | |||
849 | /** |
||
850 | * @param array $data |
||
851 | * @param array $linesToBeCovered |
||
852 | * @param array $linesToBeUsed |
||
853 | * @throws PHP_CodeCoverage_UnintentionallyCoveredCodeException |
||
854 | * @since Method available since Release 2.0.0 |
||
855 | */ |
||
856 | private function performUnintentionallyCoveredCodeCheck(array &$data, array $linesToBeCovered, array $linesToBeUsed) |
||
885 | |||
886 | /** |
||
887 | * @param array $data |
||
888 | * @param array $linesToBeCovered |
||
889 | * @param array $linesToBeUsed |
||
890 | * @throws PHP_CodeCoverage_CoveredCodeNotExecutedException |
||
891 | */ |
||
892 | private function performUnexecutedCoveredCodeCheck(array &$data, array $linesToBeCovered, array $linesToBeUsed) |
||
926 | |||
927 | /** |
||
928 | * @param array $linesToBeCovered |
||
929 | * @param array $linesToBeUsed |
||
930 | * @return array |
||
931 | * @since Method available since Release 2.0.0 |
||
932 | */ |
||
933 | private function getAllowedLines(array $linesToBeCovered, array $linesToBeUsed) |
||
967 | |||
968 | /** |
||
969 | * @return PHP_CodeCoverage_Driver |
||
970 | * @throws PHP_CodeCoverage_RuntimeException |
||
971 | */ |
||
972 | private function selectDriver() |
||
988 | } |
||
989 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: