Complex classes like ResultPrinter 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 ResultPrinter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class ResultPrinter |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * A collection of ExecutableTest objects. |
||
| 19 | * |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $suites = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var \ParaTest\Logging\LogInterpreter |
||
| 26 | */ |
||
| 27 | protected $results; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The number of tests results currently printed. |
||
| 31 | * Used to determine when to tally current results |
||
| 32 | * and start a new row. |
||
| 33 | * |
||
| 34 | * @var int |
||
| 35 | */ |
||
| 36 | protected $numTestsWidth; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Used for formatting results to a given width. |
||
| 40 | * |
||
| 41 | * @var int |
||
| 42 | */ |
||
| 43 | protected $maxColumn; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The total number of cases to be run. |
||
| 47 | * |
||
| 48 | * @var int |
||
| 49 | */ |
||
| 50 | protected $totalCases = 0; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The current column being printed to. |
||
| 54 | * |
||
| 55 | * @var int |
||
| 56 | */ |
||
| 57 | protected $column = 0; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var \PHP_Timer |
||
| 61 | */ |
||
| 62 | protected $timer; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The total number of cases printed so far. |
||
| 66 | * |
||
| 67 | * @var int |
||
| 68 | */ |
||
| 69 | protected $casesProcessed = 0; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Whether to display a red or green bar. |
||
| 73 | * |
||
| 74 | * @var bool |
||
| 75 | */ |
||
| 76 | protected $colors; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Warnings generated by the cases. |
||
| 80 | * |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | protected $warnings = []; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Number of columns. |
||
| 87 | * |
||
| 88 | * @var int |
||
| 89 | */ |
||
| 90 | protected $numberOfColumns = 80; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Number of skipped or incomplete tests. |
||
| 94 | * |
||
| 95 | * @var int |
||
| 96 | */ |
||
| 97 | protected $totalSkippedOrIncomplete = 0; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Do we need to try to process skipped/incompleted tests. |
||
| 101 | * |
||
| 102 | * @var bool |
||
| 103 | */ |
||
| 104 | protected $processSkipped = false; |
||
| 105 | |||
| 106 | 27 | public function __construct(LogInterpreter $results) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Adds an ExecutableTest to the tracked results. |
||
| 114 | * |
||
| 115 | * @param ExecutableTest $suite |
||
| 116 | * |
||
| 117 | * @return $this |
||
| 118 | */ |
||
| 119 | 16 | public function addTest(ExecutableTest $suite): self |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Initializes printing constraints, prints header |
||
| 130 | * information and starts the test timer. |
||
| 131 | * |
||
| 132 | * @param Options $options |
||
| 133 | */ |
||
| 134 | 9 | public function start(Options $options) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * @param string $string |
||
| 157 | */ |
||
| 158 | 2 | public function println(string $string = '') |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Prints all results and removes any log files |
||
| 166 | * used for aggregating results. |
||
| 167 | */ |
||
| 168 | public function flush() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Print final results. |
||
| 176 | */ |
||
| 177 | 2 | public function printResults() |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Prints the individual "quick" feedback for run |
||
| 188 | * tests, that is the ".EF" items. |
||
| 189 | * |
||
| 190 | * @param ExecutableTest $test |
||
| 191 | */ |
||
| 192 | 11 | public function printFeedback(ExecutableTest $test) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Returns the header containing resource usage. |
||
| 213 | * |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | 3 | public function getHeader(): string |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Add an array of warning strings. These cause the test run to be shown |
||
| 223 | * as failed. |
||
| 224 | */ |
||
| 225 | public function addWarnings(array $warnings) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Returns warning messages as a string. |
||
| 232 | */ |
||
| 233 | 2 | public function getWarnings(): string |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Whether the test run is successful and has no warnings. |
||
| 240 | * |
||
| 241 | * @return bool |
||
| 242 | */ |
||
| 243 | 4 | public function isSuccessful(): bool |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Return the footer information reporting success |
||
| 250 | * or failure. |
||
| 251 | * |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | 4 | public function getFooter(): string |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Returns the failure messages. |
||
| 263 | * |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | 3 | public function getFailures(): string |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Returns error messages. |
||
| 275 | * |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | 4 | public function getErrors(): string |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Returns the total cases being printed. |
||
| 287 | * |
||
| 288 | * @return int |
||
| 289 | */ |
||
| 290 | 2 | public function getTotalCases(): int |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Process reader feedback and print it. |
||
| 297 | * |
||
| 298 | * @param Reader $reader |
||
| 299 | * @param int $expectedTestCount |
||
| 300 | */ |
||
| 301 | 11 | protected function processReaderFeedback(Reader $reader, int $expectedTestCount) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Prints test warnings. |
||
| 323 | * |
||
| 324 | * @param ExecutableTest $test |
||
| 325 | */ |
||
| 326 | 11 | protected function printTestWarnings(ExecutableTest $test) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Is skipped/incomplete amount can be properly processed. |
||
| 339 | * |
||
| 340 | * @todo Skipped/Incomplete test tracking available only in functional mode for now |
||
| 341 | * or in regular mode but without group/exclude-group filters. |
||
| 342 | * |
||
| 343 | * @param mixed $options |
||
| 344 | * |
||
| 345 | * @return bool |
||
| 346 | */ |
||
| 347 | 9 | protected function isSkippedIncompleTestCanBeTracked(Options $options): bool |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Process test overhead. |
||
| 355 | * |
||
| 356 | * In some situations phpunit can return more tests then we expect and in that case |
||
| 357 | * this method correct total amount of tests so paratest progress will be auto corrected. |
||
| 358 | * |
||
| 359 | * @todo May be we need to throw Exception here instead of silent correction. |
||
| 360 | * |
||
| 361 | * @param int $actualTestCount |
||
| 362 | * @param int $expectedTestCount |
||
| 363 | */ |
||
| 364 | 11 | protected function processTestOverhead(int $actualTestCount, int $expectedTestCount) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Prints S for skipped and incomplete tests. |
||
| 380 | * |
||
| 381 | * If for some reason process return less tests than expected then we threat all remaining |
||
| 382 | * as skipped or incomplete and print them as skipped (S letter) |
||
| 383 | * |
||
| 384 | * @param int $actualTestCount |
||
| 385 | * @param int $expectedTestCount |
||
| 386 | */ |
||
| 387 | 4 | protected function printSkippedAndIncomplete(int $actualTestCount, int $expectedTestCount) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Prints a single "quick" feedback item and increments |
||
| 399 | * the total number of processed cases and the column |
||
| 400 | * position. |
||
| 401 | * |
||
| 402 | * @param $item |
||
| 403 | */ |
||
| 404 | 11 | protected function printFeedbackItem(string $item) |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Method that returns a formatted string |
||
| 417 | * for a collection of errors or failures. |
||
| 418 | * |
||
| 419 | * @param array $defects |
||
| 420 | * @param $type |
||
| 421 | * |
||
| 422 | * @return string |
||
| 423 | */ |
||
| 424 | 5 | protected function getDefects(array $defects, string $type): string |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Prints progress for large test collections. |
||
| 447 | */ |
||
| 448 | 9 | protected function getProgress(): string |
|
| 457 | |||
| 458 | /** |
||
| 459 | * Get the footer for a test collection that had tests with |
||
| 460 | * failures or errors. |
||
| 461 | * |
||
| 462 | * @return string |
||
| 463 | */ |
||
| 464 | 3 | private function getFailedFooter(): string |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Get the footer for a test collection containing all successful |
||
| 481 | * tests. |
||
| 482 | * |
||
| 483 | * @return string |
||
| 484 | */ |
||
| 485 | 1 | private function getSuccessFooter(): string |
|
| 512 | |||
| 513 | 1 | private function green(string $text): string |
|
| 523 | |||
| 524 | 3 | private function red(string $text): string |
|
| 534 | |||
| 535 | /** |
||
| 536 | * Deletes all the temporary log files for ExecutableTest objects |
||
| 537 | * being printed. |
||
| 538 | */ |
||
| 539 | private function clearLogs() |
||
| 545 | } |
||
| 546 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.