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 |
||
13 | class ResultPrinter |
||
14 | { |
||
15 | /** |
||
16 | * A collection of ExecutableTest objects. |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $suites = []; |
||
21 | |||
22 | /** |
||
23 | * @var \ParaTest\Logging\LogInterpreter |
||
24 | */ |
||
25 | protected $results; |
||
26 | |||
27 | /** |
||
28 | * The number of tests results currently printed. |
||
29 | * Used to determine when to tally current results |
||
30 | * and start a new row. |
||
31 | * |
||
32 | * @var int |
||
33 | */ |
||
34 | protected $numTestsWidth; |
||
35 | |||
36 | /** |
||
37 | * Used for formatting results to a given width. |
||
38 | * |
||
39 | * @var int |
||
40 | */ |
||
41 | protected $maxColumn; |
||
42 | |||
43 | /** |
||
44 | * The total number of cases to be run. |
||
45 | * |
||
46 | * @var int |
||
47 | */ |
||
48 | protected $totalCases = 0; |
||
49 | |||
50 | /** |
||
51 | * The current column being printed to. |
||
52 | * |
||
53 | * @var int |
||
54 | */ |
||
55 | protected $column = 0; |
||
56 | |||
57 | /** |
||
58 | * @var \PHP_Timer |
||
59 | */ |
||
60 | protected $timer; |
||
61 | |||
62 | /** |
||
63 | * The total number of cases printed so far. |
||
64 | * |
||
65 | * @var int |
||
66 | */ |
||
67 | protected $casesProcessed = 0; |
||
68 | |||
69 | /** |
||
70 | * Whether to display a red or green bar. |
||
71 | * |
||
72 | * @var bool |
||
73 | */ |
||
74 | protected $colors; |
||
75 | |||
76 | /** |
||
77 | * Warnings generated by the cases. |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | protected $warnings = []; |
||
82 | |||
83 | /** |
||
84 | * Number of columns. |
||
85 | * |
||
86 | * @var int |
||
87 | */ |
||
88 | protected $numberOfColumns = 80; |
||
89 | |||
90 | /** |
||
91 | * Number of skipped or incomplete tests. |
||
92 | * |
||
93 | * @var int |
||
94 | */ |
||
95 | protected $totalSkippedOrIncomplete = 0; |
||
96 | |||
97 | /** |
||
98 | * Do we need to try to process skipped/incompleted tests. |
||
99 | * |
||
100 | * @var bool |
||
101 | */ |
||
102 | protected $processSkipped = false; |
||
103 | |||
104 | 27 | public function __construct(LogInterpreter $results) |
|
105 | { |
||
106 | 27 | $this->results = $results; |
|
107 | 27 | $this->timer = new \PHP_Timer(); |
|
108 | 27 | } |
|
109 | |||
110 | /** |
||
111 | * Adds an ExecutableTest to the tracked results. |
||
112 | * |
||
113 | * @param ExecutableTest $suite |
||
114 | * |
||
115 | * @return $this |
||
116 | */ |
||
117 | 16 | public function addTest(ExecutableTest $suite) |
|
125 | |||
126 | /** |
||
127 | * Initializes printing constraints, prints header |
||
128 | * information and starts the test timer. |
||
129 | * |
||
130 | * @param Options $options |
||
131 | */ |
||
132 | 9 | public function start(Options $options) |
|
152 | |||
153 | /** |
||
154 | * @param string $string |
||
155 | */ |
||
156 | 2 | public function println($string = '') |
|
161 | |||
162 | /** |
||
163 | * Prints all results and removes any log files |
||
164 | * used for aggregating results. |
||
165 | */ |
||
166 | public function flush() |
||
171 | |||
172 | /** |
||
173 | * Print final results. |
||
174 | */ |
||
175 | 2 | public function printResults() |
|
183 | |||
184 | /** |
||
185 | * Prints the individual "quick" feedback for run |
||
186 | * tests, that is the ".EF" items. |
||
187 | * |
||
188 | * @param ExecutableTest $test |
||
189 | */ |
||
190 | 11 | public function printFeedback(ExecutableTest $test) |
|
208 | |||
209 | /** |
||
210 | * Returns the header containing resource usage. |
||
211 | * |
||
212 | * @return string |
||
213 | */ |
||
214 | 3 | public function getHeader() |
|
218 | |||
219 | /** |
||
220 | * Add an array of warning strings. These cause the test run to be shown |
||
221 | * as failed. |
||
222 | */ |
||
223 | public function addWarnings(array $warnings) |
||
227 | |||
228 | /** |
||
229 | * Returns warning messages as a string. |
||
230 | */ |
||
231 | 2 | public function getWarnings() |
|
235 | |||
236 | /** |
||
237 | * Whether the test run is successful and has no warnings. |
||
238 | * |
||
239 | * @return bool |
||
240 | */ |
||
241 | 4 | public function isSuccessful() |
|
245 | |||
246 | /** |
||
247 | * Return the footer information reporting success |
||
248 | * or failure. |
||
249 | * |
||
250 | * @return string |
||
251 | */ |
||
252 | 4 | public function getFooter() |
|
258 | |||
259 | /** |
||
260 | * Returns the failure messages. |
||
261 | * |
||
262 | * @return string |
||
263 | */ |
||
264 | 3 | public function getFailures() |
|
270 | |||
271 | /** |
||
272 | * Returns error messages. |
||
273 | * |
||
274 | * @return string |
||
275 | */ |
||
276 | 4 | public function getErrors() |
|
282 | |||
283 | /** |
||
284 | * Returns the total cases being printed. |
||
285 | * |
||
286 | * @return int |
||
287 | */ |
||
288 | 2 | public function getTotalCases() |
|
292 | |||
293 | /** |
||
294 | * Process reader feedback and print it. |
||
295 | * |
||
296 | * @param Reader $reader |
||
297 | * @param int $expectedTestCount |
||
298 | */ |
||
299 | 11 | protected function processReaderFeedback($reader, $expectedTestCount) |
|
318 | |||
319 | /** |
||
320 | * Prints test warnings. |
||
321 | * |
||
322 | * @param ExecutableTest $test |
||
323 | */ |
||
324 | 11 | protected function printTestWarnings($test) |
|
334 | |||
335 | /** |
||
336 | * Is skipped/incomplete amount can be properly processed. |
||
337 | * |
||
338 | * @todo Skipped/Incomplete test tracking available only in functional mode for now |
||
339 | * or in regular mode but without group/exclude-group filters. |
||
340 | * |
||
341 | * @param mixed $options |
||
342 | * |
||
343 | * @return bool |
||
344 | */ |
||
345 | 9 | protected function isSkippedIncompleTestCanBeTracked($options) |
|
350 | |||
351 | /** |
||
352 | * Process test overhead. |
||
353 | * |
||
354 | * In some situations phpunit can return more tests then we expect and in that case |
||
355 | * this method correct total amount of tests so paratest progress will be auto corrected. |
||
356 | * |
||
357 | * @todo May be we need to throw Exception here instead of silent correction. |
||
358 | * |
||
359 | * @param int $actualTestCount |
||
360 | * @param int $expectedTestCount |
||
361 | */ |
||
362 | 11 | protected function processTestOverhead($actualTestCount, $expectedTestCount) |
|
375 | |||
376 | /** |
||
377 | * Prints S for skipped and incomplete tests. |
||
378 | * |
||
379 | * If for some reason process return less tests than expected then we threat all remaining |
||
380 | * as skipped or incomplete and print them as skipped (S letter) |
||
381 | * |
||
382 | * @param int $actualTestCount |
||
383 | * @param int $expectedTestCount |
||
384 | */ |
||
385 | 4 | protected function printSkippedAndIncomplete($actualTestCount, $expectedTestCount) |
|
394 | |||
395 | /** |
||
396 | * Prints a single "quick" feedback item and increments |
||
397 | * the total number of processed cases and the column |
||
398 | * position. |
||
399 | * |
||
400 | * @param $item |
||
401 | */ |
||
402 | 11 | protected function printFeedbackItem($item) |
|
412 | |||
413 | /** |
||
414 | * Method that returns a formatted string |
||
415 | * for a collection of errors or failures. |
||
416 | * |
||
417 | * @param array $defects |
||
418 | * @param $type |
||
419 | * |
||
420 | * @return string |
||
421 | */ |
||
422 | 5 | protected function getDefects(array $defects, $type) |
|
423 | { |
||
424 | 5 | $count = count($defects); |
|
425 | 5 | if ($count === 0) { |
|
426 | 2 | return ''; |
|
427 | } |
||
428 | 5 | $output = sprintf( |
|
429 | 5 | "There %s %d %s%s:\n", |
|
430 | 5 | ($count === 1) ? 'was' : 'were', |
|
431 | 5 | $count, |
|
432 | 5 | $type, |
|
433 | 5 | ($count === 1) ? '' : 's' |
|
434 | ); |
||
435 | |||
436 | 5 | for ($i = 1; $i <= count($defects); ++$i) { |
|
437 | 5 | $output .= sprintf("\n%d) %s\n", $i, $defects[$i - 1]); |
|
438 | } |
||
439 | |||
440 | 5 | return $output; |
|
441 | } |
||
442 | |||
443 | /** |
||
444 | * Prints progress for large test collections. |
||
445 | */ |
||
446 | 9 | protected function getProgress() |
|
455 | |||
456 | /** |
||
457 | * Get the footer for a test collection that had tests with |
||
458 | * failures or errors. |
||
459 | * |
||
460 | * @return string |
||
461 | */ |
||
462 | 3 | private function getFailedFooter() |
|
463 | { |
||
464 | 3 | $formatString = "FAILURES!\nTests: %d, Assertions: %d, Failures: %d, Errors: %d.\n"; |
|
465 | |||
466 | 3 | return "\n" . $this->red( |
|
467 | 3 | sprintf( |
|
468 | 3 | $formatString, |
|
469 | 3 | $this->results->getTotalTests(), |
|
470 | 3 | $this->results->getTotalAssertions(), |
|
471 | 3 | $this->results->getTotalFailures(), |
|
472 | 3 | $this->results->getTotalErrors() |
|
473 | ) |
||
474 | ); |
||
475 | } |
||
476 | |||
477 | /** |
||
478 | * Get the footer for a test collection containing all successful |
||
479 | * tests. |
||
480 | * |
||
481 | * @return string |
||
482 | */ |
||
483 | 1 | private function getSuccessFooter() |
|
484 | { |
||
485 | 1 | $tests = $this->totalCases; |
|
486 | 1 | $asserts = $this->results->getTotalAssertions(); |
|
487 | |||
488 | 1 | if ($this->totalSkippedOrIncomplete > 0) { |
|
489 | // phpunit 4.5 produce NOT plural version for test(s) and assertion(s) in that case |
||
490 | // also it shows result in standard color scheme |
||
491 | return sprintf( |
||
492 | "OK, but incomplete, skipped, or risky tests!\n" |
||
493 | . "Tests: %d, Assertions: %d, Incomplete: %d.\n", |
||
494 | $tests, |
||
495 | $asserts, |
||
496 | $this->totalSkippedOrIncomplete |
||
497 | ); |
||
498 | } |
||
499 | // phpunit 4.5 produce plural version for test(s) and assertion(s) in that case |
||
500 | // also it shows result as black text on green background |
||
501 | 1 | return $this->green(sprintf( |
|
502 | 1 | "OK (%d test%s, %d assertion%s)\n", |
|
503 | 1 | $tests, |
|
504 | 1 | ($tests === 1) ? '' : 's', |
|
505 | 1 | $asserts, |
|
506 | 1 | ($asserts === 1) ? '' : 's' |
|
507 | )); |
||
508 | } |
||
509 | |||
510 | 1 | private function green($text) |
|
520 | |||
521 | 3 | private function red($text) |
|
531 | |||
532 | /** |
||
533 | * Deletes all the temporary log files for ExecutableTest objects |
||
534 | * being printed. |
||
535 | */ |
||
536 | private function clearLogs() |
||
542 | } |
||
543 |
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@property
annotation 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.