Complex classes like BaseCoverFishOutput 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 BaseCoverFishOutput, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | abstract class BaseCoverFishOutput |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @const MACRO_DETAIL_LINE_INDENT set line indent for detailed error message block |
||
| 28 | */ |
||
| 29 | const MACRO_CONFIG_DETAIL_LINE_INDENT = 3; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @const MACRO_SKIPPED code for skipped/coverage missing testFunctions |
||
| 33 | */ |
||
| 34 | const MACRO_SKIPPED = 0; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @const MACRO_PASS code for passed testFunctions |
||
| 38 | */ |
||
| 39 | const MACRO_PASS = 1; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @const MACRO_FAILURE code for failing testFunctions |
||
| 43 | */ |
||
| 44 | const MACRO_FAILURE = 2; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @const MACRO_FAILURE code for error/exception thrown testFunctions |
||
| 48 | */ |
||
| 49 | const MACRO_ERROR = 3; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @const MACRO_WARNING code for warning in testFunctions |
||
| 53 | */ |
||
| 54 | const MACRO_WARNING = 4; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @const FILE_PASS code for finally successfully closed single test file scan |
||
| 58 | */ |
||
| 59 | const FILE_PASS = 10; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @const FILE_PASS code for tragically failed single test file scan exit |
||
| 63 | */ |
||
| 64 | const FILE_FAILURE = 20; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var OutputInterface |
||
| 68 | */ |
||
| 69 | protected $output; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var CoverFishScanner |
||
| 73 | */ |
||
| 74 | protected $scanner; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var CoverFishHelper |
||
| 78 | */ |
||
| 79 | protected $coverFishHelper; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var CoverFishResult |
||
| 83 | */ |
||
| 84 | protected $coverFishResult; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var CoverFishPHPUnitFile |
||
| 88 | */ |
||
| 89 | protected $coverFishUnitFile; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var string |
||
| 93 | */ |
||
| 94 | protected $outputFormat; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var int |
||
| 98 | */ |
||
| 99 | protected $outputLevel; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var bool |
||
| 103 | */ |
||
| 104 | protected $preventAnsiColors = false; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var bool |
||
| 108 | */ |
||
| 109 | protected $preventEcho = false; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var bool |
||
| 113 | */ |
||
| 114 | protected $outputFormatJson = false; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var bool |
||
| 118 | */ |
||
| 119 | protected $outputFormatText = true; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var array |
||
| 123 | */ |
||
| 124 | protected $jsonResult = array(); |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var array |
||
| 128 | */ |
||
| 129 | protected $jsonResults = array(); |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var bool |
||
| 133 | */ |
||
| 134 | protected $scanFailure; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * initializer for json result set in write progress method |
||
| 138 | */ |
||
| 139 | protected function resetJsonResult() |
||
| 147 | |||
| 148 | protected function writeLine($content) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param string $content |
||
| 157 | */ |
||
| 158 | protected function write($content) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @codeCoverageIgnore |
||
| 167 | * |
||
| 168 | * @param int $count |
||
| 169 | * @param string $char |
||
| 170 | * |
||
| 171 | * @return null|string |
||
| 172 | */ |
||
| 173 | protected function setIndent($count, $char = ' ') |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @codeCoverageIgnore |
||
| 185 | * |
||
| 186 | * @param CoverFishResult $coverFishResult |
||
| 187 | * |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | protected function getScanFailPassStatistic(CoverFishResult $coverFishResult) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param string $colorCode |
||
| 214 | * @param string $statusMinimal |
||
| 215 | * @param string $statusDetailed |
||
| 216 | * @param null|string $streamMessage |
||
| 217 | * |
||
| 218 | * @return string |
||
| 219 | * |
||
| 220 | * @throws \Exception |
||
| 221 | */ |
||
| 222 | private function getFileResultTemplate($colorCode, $statusMinimal, $statusDetailed, $streamMessage = null) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * main progress output rendering function |
||
| 251 | * |
||
| 252 | * @param int $status |
||
| 253 | * @param string $message |
||
| 254 | * |
||
| 255 | * @return null |
||
| 256 | */ |
||
| 257 | protected function writeFileResult($status, $message) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param string $colorCode |
||
| 282 | * @param string $charMinimal |
||
| 283 | * @param string $charDetailed |
||
| 284 | * |
||
| 285 | * @return string |
||
| 286 | * |
||
| 287 | * @throws \Exception |
||
| 288 | */ |
||
| 289 | private function getProgressTemplate($colorCode, $charMinimal, $charDetailed) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * main progress output rendering function |
||
| 303 | * |
||
| 304 | * @param int $status |
||
| 305 | * |
||
| 306 | * @return null |
||
| 307 | */ |
||
| 308 | protected function writeProgress($status) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * write scan pass results |
||
| 355 | * |
||
| 356 | * @param CoverFishResult $coverFishResult |
||
| 357 | */ |
||
| 358 | protected function writeScanPassStatistic(CoverFishResult $coverFishResult) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * write scan fail result |
||
| 383 | * |
||
| 384 | * @param CoverFishResult $coverFishResult |
||
| 385 | * |
||
| 386 | * @throws CoverFishFailExit |
||
| 387 | */ |
||
| 388 | protected function writeScanFailStatistic(CoverFishResult $coverFishResult) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param CoverFishPHPUnitFile $coverFishUnitFile |
||
| 417 | * |
||
| 418 | * @return null |
||
| 419 | */ |
||
| 420 | protected function writeFileName(CoverFishPHPUnitFile $coverFishUnitFile) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * handle scanner output by default/parametric output format settings |
||
| 444 | * |
||
| 445 | * @param CoverFishResult $coverFishResult |
||
| 446 | * |
||
| 447 | * @return null |
||
| 448 | * |
||
| 449 | * @throws CoverFishFailExit |
||
| 450 | */ |
||
| 451 | protected function outputResult(CoverFishResult $coverFishResult) |
||
| 472 | } |
According to the PSR-2, the body of a default statement must start on the line immediately following the statement.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.