Complex classes like 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 CodeCoverage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class CodeCoverage |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var Driver |
||
| 31 | */ |
||
| 32 | private $driver; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var Filter |
||
| 36 | */ |
||
| 37 | private $filter; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var Wizard |
||
| 41 | */ |
||
| 42 | private $wizard; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | private $cacheTokens = false; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var bool |
||
| 51 | */ |
||
| 52 | private $checkForUnintentionallyCoveredCode = false; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var bool |
||
| 56 | */ |
||
| 57 | private $forceCoversAnnotation = false; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var bool |
||
| 61 | */ |
||
| 62 | private $checkForUnexecutedCoveredCode = false; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var bool |
||
| 66 | */ |
||
| 67 | private $checkForMissingCoversAnnotation = false; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var bool |
||
| 71 | */ |
||
| 72 | private $addUncoveredFilesFromWhitelist = true; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var bool |
||
| 76 | */ |
||
| 77 | private $processUncoveredFilesFromWhitelist = false; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var bool |
||
| 81 | */ |
||
| 82 | private $ignoreDeprecatedCode = false; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var mixed |
||
| 86 | */ |
||
| 87 | private $currentId; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Code coverage data. |
||
| 91 | * |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | private $data = []; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var array |
||
| 98 | */ |
||
| 99 | private $ignoredLines = []; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var bool |
||
| 103 | */ |
||
| 104 | private $disableIgnoredLines = false; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Test data. |
||
| 108 | * |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | private $tests = []; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var string[] |
||
| 115 | */ |
||
| 116 | private $unintentionallyCoveredSubclassesWhitelist = []; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Determine if the data has been initialized or not |
||
| 120 | * |
||
| 121 | * @var bool |
||
| 122 | */ |
||
| 123 | private $isInitialized = false; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Determine whether we need to check for dead and unused code on each test |
||
| 127 | * |
||
| 128 | * @var bool |
||
| 129 | */ |
||
| 130 | private $shouldCheckForDeadAndUnused = true; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var Directory |
||
| 134 | */ |
||
| 135 | private $report; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Constructor. |
||
| 139 | * |
||
| 140 | * @param Driver $driver |
||
| 141 | * @param Filter $filter |
||
| 142 | * |
||
| 143 | * @throws RuntimeException |
||
| 144 | */ |
||
| 145 | public function __construct(Driver $driver = null, Filter $filter = null) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Returns the code coverage information as a graph of node objects. |
||
| 163 | * |
||
| 164 | * @return Directory |
||
| 165 | */ |
||
| 166 | public function getReport() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Clears collected code coverage data. |
||
| 179 | */ |
||
| 180 | public function clear() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Returns the filter object used. |
||
| 191 | * |
||
| 192 | * @return Filter |
||
| 193 | */ |
||
| 194 | public function filter() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Returns the collected code coverage data. |
||
| 201 | * Set $raw = true to bypass all filters. |
||
| 202 | * |
||
| 203 | * @param bool $raw |
||
| 204 | * |
||
| 205 | * @return array |
||
| 206 | */ |
||
| 207 | public function getData($raw = false) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Sets the coverage data. |
||
| 218 | * |
||
| 219 | * @param array $data |
||
| 220 | */ |
||
| 221 | public function setData(array $data) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Returns the test data. |
||
| 229 | * |
||
| 230 | * @return array |
||
| 231 | */ |
||
| 232 | public function getTests() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Sets the test data. |
||
| 239 | * |
||
| 240 | * @param array $tests |
||
| 241 | */ |
||
| 242 | public function setTests(array $tests) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Start collection of code coverage information. |
||
| 249 | * |
||
| 250 | * @param mixed $id |
||
| 251 | * @param bool $clear |
||
| 252 | * |
||
| 253 | * @throws InvalidArgumentException |
||
| 254 | */ |
||
| 255 | public function start($id, $clear = false) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Stop collection of code coverage information. |
||
| 279 | * |
||
| 280 | * @param bool $append |
||
| 281 | * @param mixed $linesToBeCovered |
||
| 282 | * @param array $linesToBeUsed |
||
| 283 | * |
||
| 284 | * @return array |
||
| 285 | * |
||
| 286 | * @throws InvalidArgumentException |
||
| 287 | */ |
||
| 288 | public function stop($append = true, $linesToBeCovered = [], array $linesToBeUsed = []) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Appends code coverage data. |
||
| 314 | * |
||
| 315 | * @param array $data |
||
| 316 | * @param mixed $id |
||
| 317 | * @param bool $append |
||
| 318 | * @param mixed $linesToBeCovered |
||
| 319 | * @param array $linesToBeUsed |
||
| 320 | * |
||
| 321 | * @throws RuntimeException |
||
| 322 | */ |
||
| 323 | public function append(array $data, $id = null, $append = true, $linesToBeCovered = [], array $linesToBeUsed = []) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Merges the data from another instance. |
||
| 395 | * |
||
| 396 | * @param CodeCoverage $that |
||
| 397 | */ |
||
| 398 | public function merge(CodeCoverage $that) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @param bool $flag |
||
| 432 | * |
||
| 433 | * @throws InvalidArgumentException |
||
| 434 | */ |
||
| 435 | public function setCacheTokens($flag) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @return bool |
||
| 449 | */ |
||
| 450 | public function getCacheTokens() |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @param bool $flag |
||
| 457 | * |
||
| 458 | * @throws InvalidArgumentException |
||
| 459 | */ |
||
| 460 | public function setCheckForUnintentionallyCoveredCode($flag) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @param bool $flag |
||
| 474 | * |
||
| 475 | * @throws InvalidArgumentException |
||
| 476 | */ |
||
| 477 | public function setForceCoversAnnotation($flag) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @param bool $flag |
||
| 491 | * |
||
| 492 | * @throws InvalidArgumentException |
||
| 493 | */ |
||
| 494 | public function setCheckForMissingCoversAnnotation($flag) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @param bool $flag |
||
| 508 | * |
||
| 509 | * @throws InvalidArgumentException |
||
| 510 | */ |
||
| 511 | public function setCheckForUnexecutedCoveredCode($flag) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @deprecated |
||
| 525 | * |
||
| 526 | * @param bool $flag |
||
| 527 | * |
||
| 528 | * @throws InvalidArgumentException |
||
| 529 | */ |
||
| 530 | public function setMapTestClassNameToCoveredClassName($flag) |
||
| 533 | |||
| 534 | /** |
||
| 535 | * @param bool $flag |
||
| 536 | * |
||
| 537 | * @throws InvalidArgumentException |
||
| 538 | */ |
||
| 539 | public function setAddUncoveredFilesFromWhitelist($flag) |
||
| 550 | |||
| 551 | /** |
||
| 552 | * @param bool $flag |
||
| 553 | * |
||
| 554 | * @throws InvalidArgumentException |
||
| 555 | */ |
||
| 556 | public function setProcessUncoveredFilesFromWhitelist($flag) |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @param bool $flag |
||
| 570 | * |
||
| 571 | * @throws InvalidArgumentException |
||
| 572 | */ |
||
| 573 | public function setDisableIgnoredLines($flag) |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @param bool $flag |
||
| 587 | * |
||
| 588 | * @throws InvalidArgumentException |
||
| 589 | */ |
||
| 590 | public function setIgnoreDeprecatedCode($flag) |
||
| 601 | |||
| 602 | /** |
||
| 603 | * @param array $whitelist |
||
| 604 | */ |
||
| 605 | public function setUnintentionallyCoveredSubclassesWhitelist(array $whitelist) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Applies the @covers annotation filtering. |
||
| 612 | * |
||
| 613 | * @param array $data |
||
| 614 | * @param mixed $linesToBeCovered |
||
| 615 | * @param array $linesToBeUsed |
||
| 616 | * |
||
| 617 | * @throws MissingCoversAnnotationException |
||
| 618 | * @throws UnintentionallyCoveredCodeException |
||
| 619 | */ |
||
| 620 | private function applyCoversAnnotationFilter(array &$data, $linesToBeCovered, array $linesToBeUsed) |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Applies the whitelist filtering. |
||
| 665 | * |
||
| 666 | * @param array $data |
||
| 667 | */ |
||
| 668 | private function applyListsFilter(array &$data) |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Applies the "ignored lines" filtering. |
||
| 679 | * |
||
| 680 | * @param array $data |
||
| 681 | */ |
||
| 682 | private function applyIgnoredLinesFilter(array &$data) |
||
| 694 | |||
| 695 | /** |
||
| 696 | * @param array $data |
||
| 697 | */ |
||
| 698 | private function initializeFilesThatAreSeenTheFirstTime(array $data) |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Processes whitelisted files that are not covered. |
||
| 713 | */ |
||
| 714 | private function addUncoveredFilesFromWhitelist() |
||
| 740 | |||
| 741 | /** |
||
| 742 | * Returns the lines of a source file that should be ignored. |
||
| 743 | * |
||
| 744 | * @param string $filename |
||
| 745 | * |
||
| 746 | * @return array |
||
| 747 | * |
||
| 748 | * @throws InvalidArgumentException |
||
| 749 | */ |
||
| 750 | private function getLinesToBeIgnored($filename) |
||
| 920 | |||
| 921 | /** |
||
| 922 | * @param array $data |
||
| 923 | * @param array $linesToBeCovered |
||
| 924 | * @param array $linesToBeUsed |
||
| 925 | * |
||
| 926 | * @throws UnintentionallyCoveredCodeException |
||
| 927 | */ |
||
| 928 | private function performUnintentionallyCoveredCodeCheck(array &$data, array $linesToBeCovered, array $linesToBeUsed) |
||
| 953 | |||
| 954 | /** |
||
| 955 | * @param array $data |
||
| 956 | * @param array $linesToBeCovered |
||
| 957 | * @param array $linesToBeUsed |
||
| 958 | * |
||
| 959 | * @throws CoveredCodeNotExecutedException |
||
| 960 | */ |
||
| 961 | private function performUnexecutedCoveredCodeCheck(array &$data, array $linesToBeCovered, array $linesToBeUsed) |
||
| 988 | |||
| 989 | /** |
||
| 990 | * @param array $linesToBeCovered |
||
| 991 | * @param array $linesToBeUsed |
||
| 992 | * |
||
| 993 | * @return array |
||
| 994 | */ |
||
| 995 | private function getAllowedLines(array $linesToBeCovered, array $linesToBeUsed) |
||
| 1029 | |||
| 1030 | /** |
||
| 1031 | * @return Driver |
||
| 1032 | * |
||
| 1033 | * @throws RuntimeException |
||
| 1034 | */ |
||
| 1035 | private function selectDriver() |
||
| 1051 | |||
| 1052 | /** |
||
| 1053 | * @param array $unintentionallyCoveredUnits |
||
| 1054 | * |
||
| 1055 | * @return array |
||
| 1056 | */ |
||
| 1057 | private function processUnintentionallyCoveredUnits(array $unintentionallyCoveredUnits) |
||
| 1081 | |||
| 1082 | /** |
||
| 1083 | * If we are processing uncovered files from whitelist, |
||
| 1084 | * we can initialize the data before we start to speed up the tests |
||
| 1085 | */ |
||
| 1086 | protected function initializeData() |
||
| 1087 | { |
||
| 1088 | $this->isInitialized = true; |
||
| 1089 | |||
| 1090 | if ($this->processUncoveredFilesFromWhitelist) { |
||
| 1091 | $this->shouldCheckForDeadAndUnused = false; |
||
| 1092 | |||
| 1093 | $this->driver->start(true); |
||
| 1094 | |||
| 1095 | foreach ($this->filter->getWhitelist() as $file) { |
||
| 1096 | if ($this->filter->isFile($file)) { |
||
| 1097 | include_once($file); |
||
| 1098 | } |
||
| 1099 | } |
||
| 1100 | |||
| 1101 | $data = []; |
||
| 1102 | $coverage = $this->driver->stop(); |
||
| 1103 | |||
| 1104 | foreach ($coverage as $file => $fileCoverage) { |
||
| 1105 | if ($this->filter->isFiltered($file)) { |
||
| 1106 | continue; |
||
| 1107 | } |
||
| 1108 | |||
| 1109 | foreach (array_keys($fileCoverage) as $key) { |
||
| 1110 | if ($fileCoverage[$key] == Driver::LINE_EXECUTED) { |
||
| 1111 | $fileCoverage[$key] = Driver::LINE_NOT_EXECUTED; |
||
| 1112 | } |
||
| 1113 | } |
||
| 1114 | |||
| 1115 | $data[$file] = $fileCoverage; |
||
| 1116 | } |
||
| 1117 | |||
| 1118 | $this->append($data, 'UNCOVERED_FILES_FROM_WHITELIST'); |
||
| 1119 | } |
||
| 1120 | } |
||
| 1121 | |||
| 1122 | /** |
||
| 1123 | * @param array $data |
||
| 1124 | * |
||
| 1125 | * @return array |
||
| 1126 | */ |
||
| 1127 | private function coverageToCodeUnits(array $data) |
||
| 1141 | |||
| 1142 | /** |
||
| 1143 | * @param array $data |
||
| 1144 | * |
||
| 1145 | * @return array |
||
| 1146 | */ |
||
| 1147 | private function linesToCodeUnits(array $data) |
||
| 1159 | } |
||
| 1160 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: