Complex classes like TestResult 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 TestResult, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class TestResult implements Countable |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $passed = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $errors = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $failures = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $warnings = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $notImplemented = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $risky = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $skipped = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $listeners = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var int |
||
| 78 | */ |
||
| 79 | protected $runTests = 0; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var float |
||
| 83 | */ |
||
| 84 | protected $time = 0; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var TestSuite |
||
| 88 | */ |
||
| 89 | protected $topTestSuite; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Code Coverage information. |
||
| 93 | * |
||
| 94 | * @var CodeCoverage |
||
| 95 | */ |
||
| 96 | protected $codeCoverage; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var bool |
||
| 100 | */ |
||
| 101 | protected $convertErrorsToExceptions = true; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var bool |
||
| 105 | */ |
||
| 106 | protected $stop = false; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var bool |
||
| 110 | */ |
||
| 111 | protected $stopOnError = false; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var bool |
||
| 115 | */ |
||
| 116 | protected $stopOnFailure = false; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var bool |
||
| 120 | */ |
||
| 121 | protected $stopOnWarning = false; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var bool |
||
| 125 | */ |
||
| 126 | protected $beStrictAboutTestsThatDoNotTestAnything = true; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var bool |
||
| 130 | */ |
||
| 131 | protected $beStrictAboutOutputDuringTests = false; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var bool |
||
| 135 | */ |
||
| 136 | protected $beStrictAboutTodoAnnotatedTests = false; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var bool |
||
| 140 | */ |
||
| 141 | protected $beStrictAboutResourceUsageDuringSmallTests = false; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var bool |
||
| 145 | */ |
||
| 146 | protected $enforceTimeLimit = false; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var int |
||
| 150 | */ |
||
| 151 | protected $timeoutForSmallTests = 1; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var int |
||
| 155 | */ |
||
| 156 | protected $timeoutForMediumTests = 10; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @var int |
||
| 160 | */ |
||
| 161 | protected $timeoutForLargeTests = 60; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @var bool |
||
| 165 | */ |
||
| 166 | protected $stopOnRisky = false; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @var bool |
||
| 170 | */ |
||
| 171 | protected $stopOnIncomplete = false; |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @var bool |
||
| 175 | */ |
||
| 176 | protected $stopOnSkipped = false; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @var bool |
||
| 180 | */ |
||
| 181 | protected $lastTestFailed = false; |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @var bool |
||
| 185 | */ |
||
| 186 | private $registerMockObjectsFromTestArgumentsRecursively = false; |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Registers a TestListener. |
||
| 190 | * |
||
| 191 | * @param TestListener $listener |
||
| 192 | */ |
||
| 193 | public function addListener(TestListener $listener) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Unregisters a TestListener. |
||
| 200 | * |
||
| 201 | * @param TestListener $listener |
||
| 202 | */ |
||
| 203 | public function removeListener(TestListener $listener) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Flushes all flushable TestListeners. |
||
| 214 | */ |
||
| 215 | public function flushListeners() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Adds an error to the list of errors. |
||
| 226 | * |
||
| 227 | * @param Test $test |
||
| 228 | * @param Throwable $t |
||
| 229 | * @param float $time |
||
| 230 | */ |
||
| 231 | public function addError(Test $test, Throwable $t, $time) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Adds a warning to the list of warnings. |
||
| 282 | * The passed in exception caused the warning. |
||
| 283 | * |
||
| 284 | * @param Test $test |
||
| 285 | * @param Warning $e |
||
| 286 | * @param float $time |
||
| 287 | */ |
||
| 288 | public function addWarning(Test $test, Warning $e, $time) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Adds a failure to the list of failures. |
||
| 305 | * The passed in exception caused the failure. |
||
| 306 | * |
||
| 307 | * @param Test $test |
||
| 308 | * @param AssertionFailedError $e |
||
| 309 | * @param float $time |
||
| 310 | */ |
||
| 311 | public function addFailure(Test $test, AssertionFailedError $e, $time) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Informs the result that a testsuite will be started. |
||
| 357 | * |
||
| 358 | * @param TestSuite $suite |
||
| 359 | */ |
||
| 360 | public function startTestSuite(TestSuite $suite) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Informs the result that a testsuite was completed. |
||
| 373 | * |
||
| 374 | * @param TestSuite $suite |
||
| 375 | */ |
||
| 376 | public function endTestSuite(TestSuite $suite) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Informs the result that a test will be started. |
||
| 385 | * |
||
| 386 | * @param Test $test |
||
| 387 | */ |
||
| 388 | public function startTest(Test $test) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Informs the result that a test was completed. |
||
| 400 | * |
||
| 401 | * @param Test $test |
||
| 402 | * @param float $time |
||
| 403 | */ |
||
| 404 | public function endTest(Test $test, $time) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Returns true if no risky test occurred. |
||
| 428 | * |
||
| 429 | * @return bool |
||
| 430 | */ |
||
| 431 | public function allHarmless() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Gets the number of risky tests. |
||
| 438 | * |
||
| 439 | * @return int |
||
| 440 | */ |
||
| 441 | public function riskyCount() |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Returns true if no incomplete test occurred. |
||
| 448 | * |
||
| 449 | * @return bool |
||
| 450 | */ |
||
| 451 | public function allCompletelyImplemented() |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Gets the number of incomplete tests. |
||
| 458 | * |
||
| 459 | * @return int |
||
| 460 | */ |
||
| 461 | public function notImplementedCount() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Returns an Enumeration for the risky tests. |
||
| 468 | * |
||
| 469 | * @return array |
||
| 470 | */ |
||
| 471 | public function risky() |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Returns an Enumeration for the incomplete tests. |
||
| 478 | * |
||
| 479 | * @return array |
||
| 480 | */ |
||
| 481 | public function notImplemented() |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Returns true if no test has been skipped. |
||
| 488 | * |
||
| 489 | * @return bool |
||
| 490 | */ |
||
| 491 | public function noneSkipped() |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Gets the number of skipped tests. |
||
| 498 | * |
||
| 499 | * @return int |
||
| 500 | */ |
||
| 501 | public function skippedCount() |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Returns an Enumeration for the skipped tests. |
||
| 508 | * |
||
| 509 | * @return array |
||
| 510 | */ |
||
| 511 | public function skipped() |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Gets the number of detected errors. |
||
| 518 | * |
||
| 519 | * @return int |
||
| 520 | */ |
||
| 521 | public function errorCount() |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Returns an Enumeration for the errors. |
||
| 528 | * |
||
| 529 | * @return array |
||
| 530 | */ |
||
| 531 | public function errors() |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Gets the number of detected failures. |
||
| 538 | * |
||
| 539 | * @return int |
||
| 540 | */ |
||
| 541 | public function failureCount() |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Returns an Enumeration for the failures. |
||
| 548 | * |
||
| 549 | * @return array |
||
| 550 | */ |
||
| 551 | public function failures() |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Gets the number of detected warnings. |
||
| 558 | * |
||
| 559 | * @return int |
||
| 560 | */ |
||
| 561 | public function warningCount() |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Returns an Enumeration for the warnings. |
||
| 568 | * |
||
| 569 | * @return array |
||
| 570 | */ |
||
| 571 | public function warnings() |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Returns the names of the tests that have passed. |
||
| 578 | * |
||
| 579 | * @return array |
||
| 580 | */ |
||
| 581 | public function passed() |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Returns the (top) test suite. |
||
| 588 | * |
||
| 589 | * @return TestSuite |
||
| 590 | */ |
||
| 591 | public function topTestSuite() |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Returns whether code coverage information should be collected. |
||
| 598 | * |
||
| 599 | * @return bool If code coverage should be collected |
||
| 600 | */ |
||
| 601 | public function getCollectCodeCoverageInformation() |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Runs a TestCase. |
||
| 608 | * |
||
| 609 | * @param Test $test |
||
| 610 | */ |
||
| 611 | public function run(Test $test) |
||
| 893 | |||
| 894 | /** |
||
| 895 | * Gets the number of run tests. |
||
| 896 | * |
||
| 897 | * @return int |
||
| 898 | */ |
||
| 899 | public function count() |
||
| 903 | |||
| 904 | /** |
||
| 905 | * Checks whether the test run should stop. |
||
| 906 | * |
||
| 907 | * @return bool |
||
| 908 | */ |
||
| 909 | public function shouldStop() |
||
| 913 | |||
| 914 | /** |
||
| 915 | * Marks that the test run should stop. |
||
| 916 | */ |
||
| 917 | public function stop() |
||
| 921 | |||
| 922 | /** |
||
| 923 | * Returns the code coverage object. |
||
| 924 | * |
||
| 925 | * @return CodeCoverage |
||
| 926 | */ |
||
| 927 | public function getCodeCoverage() |
||
| 931 | |||
| 932 | /** |
||
| 933 | * Sets the code coverage object. |
||
| 934 | * |
||
| 935 | * @param CodeCoverage $codeCoverage |
||
| 936 | */ |
||
| 937 | public function setCodeCoverage(CodeCoverage $codeCoverage) |
||
| 941 | |||
| 942 | /** |
||
| 943 | * Enables or disables the error-to-exception conversion. |
||
| 944 | * |
||
| 945 | * @param bool $flag |
||
| 946 | * |
||
| 947 | * @throws Exception |
||
| 948 | */ |
||
| 949 | public function convertErrorsToExceptions($flag) |
||
| 957 | |||
| 958 | /** |
||
| 959 | * Returns the error-to-exception conversion setting. |
||
| 960 | * |
||
| 961 | * @return bool |
||
| 962 | */ |
||
| 963 | public function getConvertErrorsToExceptions() |
||
| 967 | |||
| 968 | /** |
||
| 969 | * Enables or disables the stopping when an error occurs. |
||
| 970 | * |
||
| 971 | * @param bool $flag |
||
| 972 | * |
||
| 973 | * @throws Exception |
||
| 974 | */ |
||
| 975 | public function stopOnError($flag) |
||
| 983 | |||
| 984 | /** |
||
| 985 | * Enables or disables the stopping when a failure occurs. |
||
| 986 | * |
||
| 987 | * @param bool $flag |
||
| 988 | * |
||
| 989 | * @throws Exception |
||
| 990 | */ |
||
| 991 | public function stopOnFailure($flag) |
||
| 999 | |||
| 1000 | /** |
||
| 1001 | * Enables or disables the stopping when a warning occurs. |
||
| 1002 | * |
||
| 1003 | * @param bool $flag |
||
| 1004 | * |
||
| 1005 | * @throws Exception |
||
| 1006 | */ |
||
| 1007 | public function stopOnWarning($flag) |
||
| 1015 | |||
| 1016 | /** |
||
| 1017 | * @param bool $flag |
||
| 1018 | * |
||
| 1019 | * @throws Exception |
||
| 1020 | */ |
||
| 1021 | public function beStrictAboutTestsThatDoNotTestAnything($flag) |
||
| 1029 | |||
| 1030 | /** |
||
| 1031 | * @return bool |
||
| 1032 | */ |
||
| 1033 | public function isStrictAboutTestsThatDoNotTestAnything() |
||
| 1037 | |||
| 1038 | /** |
||
| 1039 | * @param bool $flag |
||
| 1040 | * |
||
| 1041 | * @throws Exception |
||
| 1042 | */ |
||
| 1043 | public function beStrictAboutOutputDuringTests($flag) |
||
| 1051 | |||
| 1052 | /** |
||
| 1053 | * @return bool |
||
| 1054 | */ |
||
| 1055 | public function isStrictAboutOutputDuringTests() |
||
| 1059 | |||
| 1060 | /** |
||
| 1061 | * @param bool $flag |
||
| 1062 | * |
||
| 1063 | * @throws Exception |
||
| 1064 | */ |
||
| 1065 | public function beStrictAboutResourceUsageDuringSmallTests($flag) |
||
| 1073 | |||
| 1074 | /** |
||
| 1075 | * @return bool |
||
| 1076 | */ |
||
| 1077 | public function isStrictAboutResourceUsageDuringSmallTests() |
||
| 1081 | |||
| 1082 | /** |
||
| 1083 | * @param bool $flag |
||
| 1084 | * |
||
| 1085 | * @throws Exception |
||
| 1086 | */ |
||
| 1087 | public function enforceTimeLimit($flag) |
||
| 1095 | |||
| 1096 | /** |
||
| 1097 | * @return bool |
||
| 1098 | */ |
||
| 1099 | public function enforcesTimeLimit() |
||
| 1103 | |||
| 1104 | /** |
||
| 1105 | * @param bool $flag |
||
| 1106 | * |
||
| 1107 | * @throws Exception |
||
| 1108 | */ |
||
| 1109 | public function beStrictAboutTodoAnnotatedTests($flag) |
||
| 1117 | |||
| 1118 | /** |
||
| 1119 | * @return bool |
||
| 1120 | */ |
||
| 1121 | public function isStrictAboutTodoAnnotatedTests() |
||
| 1125 | |||
| 1126 | /** |
||
| 1127 | * Enables or disables the stopping for risky tests. |
||
| 1128 | * |
||
| 1129 | * @param bool $flag |
||
| 1130 | * |
||
| 1131 | * @throws Exception |
||
| 1132 | */ |
||
| 1133 | public function stopOnRisky($flag) |
||
| 1141 | |||
| 1142 | /** |
||
| 1143 | * Enables or disables the stopping for incomplete tests. |
||
| 1144 | * |
||
| 1145 | * @param bool $flag |
||
| 1146 | * |
||
| 1147 | * @throws Exception |
||
| 1148 | */ |
||
| 1149 | public function stopOnIncomplete($flag) |
||
| 1157 | |||
| 1158 | /** |
||
| 1159 | * Enables or disables the stopping for skipped tests. |
||
| 1160 | * |
||
| 1161 | * @param bool $flag |
||
| 1162 | * |
||
| 1163 | * @throws Exception |
||
| 1164 | */ |
||
| 1165 | public function stopOnSkipped($flag) |
||
| 1173 | |||
| 1174 | /** |
||
| 1175 | * Returns the time spent running the tests. |
||
| 1176 | * |
||
| 1177 | * @return float |
||
| 1178 | */ |
||
| 1179 | public function time() |
||
| 1183 | |||
| 1184 | /** |
||
| 1185 | * Returns whether the entire test was successful or not. |
||
| 1186 | * |
||
| 1187 | * @return bool |
||
| 1188 | */ |
||
| 1189 | public function wasSuccessful() |
||
| 1193 | |||
| 1194 | /** |
||
| 1195 | * Sets the timeout for small tests. |
||
| 1196 | * |
||
| 1197 | * @param int $timeout |
||
| 1198 | * |
||
| 1199 | * @throws Exception |
||
| 1200 | */ |
||
| 1201 | public function setTimeoutForSmallTests($timeout) |
||
| 1209 | |||
| 1210 | /** |
||
| 1211 | * Sets the timeout for medium tests. |
||
| 1212 | * |
||
| 1213 | * @param int $timeout |
||
| 1214 | * |
||
| 1215 | * @throws Exception |
||
| 1216 | */ |
||
| 1217 | public function setTimeoutForMediumTests($timeout) |
||
| 1225 | |||
| 1226 | /** |
||
| 1227 | * Sets the timeout for large tests. |
||
| 1228 | * |
||
| 1229 | * @param int $timeout |
||
| 1230 | * |
||
| 1231 | * @throws Exception |
||
| 1232 | */ |
||
| 1233 | public function setTimeoutForLargeTests($timeout) |
||
| 1241 | |||
| 1242 | /** |
||
| 1243 | * Returns the set timeout for large tests. |
||
| 1244 | * |
||
| 1245 | * @return int |
||
| 1246 | */ |
||
| 1247 | public function getTimeoutForLargeTests() |
||
| 1251 | |||
| 1252 | /** |
||
| 1253 | * @param bool $flag |
||
| 1254 | */ |
||
| 1255 | public function setRegisterMockObjectsFromTestArgumentsRecursively($flag) |
||
| 1263 | |||
| 1264 | /** |
||
| 1265 | * Returns the class hierarchy for a given class. |
||
| 1266 | * |
||
| 1267 | * @param string $className |
||
| 1268 | * @param bool $asReflectionObjects |
||
| 1269 | * |
||
| 1270 | * @return array |
||
| 1271 | */ |
||
| 1272 | protected function getHierarchy($className, $asReflectionObjects = false) |
||
| 1306 | } |
||
| 1307 |
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 implementation 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 interface: