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 |
||
14 | class ResultPrinter |
||
15 | { |
||
16 | /** |
||
17 | * A collection of ExecutableTest objects |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $suites = array(); |
||
22 | |||
23 | /** |
||
24 | * @var \ParaTest\Logging\LogInterpreter |
||
25 | */ |
||
26 | protected $results; |
||
27 | |||
28 | /** |
||
29 | * The number of tests results currently printed. |
||
30 | * Used to determine when to tally current results |
||
31 | * and start a new row |
||
32 | * |
||
33 | * @var int |
||
34 | */ |
||
35 | protected $numTestsWidth; |
||
36 | |||
37 | /** |
||
38 | * Used for formatting results to a given width |
||
39 | * |
||
40 | * @var int |
||
41 | */ |
||
42 | protected $maxColumn; |
||
43 | |||
44 | /** |
||
45 | * The total number of cases to be run |
||
46 | * |
||
47 | * @var int |
||
48 | */ |
||
49 | protected $totalCases = 0; |
||
50 | |||
51 | /** |
||
52 | * The current column being printed to |
||
53 | * |
||
54 | * @var int |
||
55 | */ |
||
56 | protected $column = 0; |
||
57 | |||
58 | /** |
||
59 | * @var \PHP_Timer |
||
60 | */ |
||
61 | protected $timer; |
||
62 | |||
63 | /** |
||
64 | * The total number of cases printed so far |
||
65 | * |
||
66 | * @var int |
||
67 | */ |
||
68 | protected $casesProcessed = 0; |
||
69 | |||
70 | /** |
||
71 | * Whether to display a red or green bar |
||
72 | * |
||
73 | * @var bool |
||
74 | */ |
||
75 | protected $colors; |
||
76 | |||
77 | /** |
||
78 | * Warnings generated by the cases |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | protected $warnings = array(); |
||
83 | |||
84 | /** |
||
85 | * Number of columns |
||
86 | * |
||
87 | * @var integer |
||
88 | */ |
||
89 | protected $numberOfColumns = 80; |
||
90 | |||
91 | /** |
||
92 | * Number of skipped or incomplete tests |
||
93 | * |
||
94 | * @var integer |
||
95 | */ |
||
96 | protected $totalSkippedOrIncomplete = 0; |
||
97 | |||
98 | /** |
||
99 | * Do we need to try to process skipped/incompleted tests. |
||
100 | * |
||
101 | * @var boolean |
||
102 | */ |
||
103 | protected $processSkipped = false; |
||
104 | |||
105 | 27 | public function __construct(LogInterpreter $results) |
|
106 | { |
||
107 | 27 | $this->results = $results; |
|
108 | 27 | $this->timer = new \PHP_Timer(); |
|
109 | 27 | } |
|
110 | |||
111 | /** |
||
112 | * Adds an ExecutableTest to the tracked results |
||
113 | * |
||
114 | * @param ExecutableTest $suite |
||
115 | * @return $this |
||
116 | */ |
||
117 | 16 | public function addTest(ExecutableTest $suite) |
|
118 | { |
||
119 | 16 | $this->suites[] = $suite; |
|
120 | 16 | $increment = $suite->getTestCount(); |
|
121 | 16 | $this->totalCases = $this->totalCases + $increment; |
|
122 | |||
123 | 16 | return $this; |
|
124 | } |
||
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) |
|
133 | { |
||
134 | 9 | $this->numTestsWidth = strlen((string) $this->totalCases); |
|
135 | 9 | $this->maxColumn = $this->numberOfColumns |
|
136 | 9 | + (DIRECTORY_SEPARATOR == "\\" ? -1 : 0) // fix windows blank lines |
|
137 | 9 | - strlen($this->getProgress()); |
|
138 | 9 | printf( |
|
139 | 9 | "\nRunning phpunit in %d process%s with %s%s\n\n", |
|
140 | 9 | $options->processes, |
|
|
|||
141 | 9 | $options->processes > 1 ? 'es' : '', |
|
142 | 9 | $options->phpunit, |
|
143 | 9 | $options->functional ? '. Functional mode is ON.' : '' |
|
144 | 9 | ); |
|
145 | 9 | if (isset($options->filtered['configuration'])) { |
|
146 | 9 | printf("Configuration read from %s\n\n", $options->filtered['configuration']->getPath()); |
|
147 | 9 | } |
|
148 | 9 | $this->timer->start(); |
|
149 | 9 | $this->colors = $options->colors; |
|
150 | 9 | $this->processSkipped = $this->isSkippedIncompleTestCanBeTracked($options); |
|
151 | 9 | } |
|
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) |
|
315 | |||
316 | /** |
||
317 | * Prints test warnings. |
||
318 | * |
||
319 | * @param ExecutableTest $test |
||
320 | */ |
||
321 | 11 | protected function printTestWarnings($test) |
|
331 | |||
332 | /** |
||
333 | * Is skipped/incomplete amount can be properly processed. |
||
334 | * |
||
335 | * @todo Skipped/Incomplete test tracking available only in functional mode for now |
||
336 | * or in regular mode but without group/exclude-group filters. |
||
337 | * |
||
338 | * @return boolean |
||
339 | */ |
||
340 | 9 | protected function isSkippedIncompleTestCanBeTracked($options) |
|
345 | |||
346 | /** |
||
347 | * Process test overhead. |
||
348 | * |
||
349 | * In some situations phpunit can return more tests then we expect and in that case |
||
350 | * this method correct total amount of tests so paratest progress will be auto corrected. |
||
351 | * |
||
352 | * @todo May be we need to throw Exception here instead of silent correction. |
||
353 | * |
||
354 | * @param int $actualTestCount |
||
355 | * @param int $expectedTestCount |
||
356 | */ |
||
357 | 11 | protected function processTestOverhead($actualTestCount, $expectedTestCount) |
|
370 | |||
371 | /** |
||
372 | * Prints S for skipped and incomplete tests. |
||
373 | * |
||
374 | * If for some reason process return less tests than expected then we threat all remaining |
||
375 | * as skipped or incomplete and print them as skipped (S letter) |
||
376 | * |
||
377 | * @param int $actualTestCount |
||
378 | * @param int $expectedTestCount |
||
379 | */ |
||
380 | 4 | protected function printSkippedAndIncomplete($actualTestCount, $expectedTestCount) |
|
389 | |||
390 | /** |
||
391 | * Prints a single "quick" feedback item and increments |
||
392 | * the total number of processed cases and the column |
||
393 | * position |
||
394 | * |
||
395 | * @param $item |
||
396 | */ |
||
397 | 11 | protected function printFeedbackItem($item) |
|
407 | |||
408 | /** |
||
409 | * Method that returns a formatted string |
||
410 | * for a collection of errors or failures |
||
411 | * |
||
412 | * @param array $defects |
||
413 | * @param $type |
||
414 | * @return string |
||
415 | */ |
||
416 | 5 | protected function getDefects(array $defects, $type) |
|
436 | |||
437 | /** |
||
438 | * Prints progress for large test collections |
||
439 | */ |
||
440 | 9 | protected function getProgress() |
|
449 | |||
450 | /** |
||
451 | * Get the footer for a test collection that had tests with |
||
452 | * failures or errors |
||
453 | * |
||
454 | * @return string |
||
455 | */ |
||
456 | 3 | private function getFailedFooter() |
|
470 | |||
471 | /** |
||
472 | * Get the footer for a test collection containing all successful |
||
473 | * tests |
||
474 | * |
||
475 | * @return string |
||
476 | */ |
||
477 | 1 | private function getSuccessFooter() |
|
504 | |||
505 | 1 | private function green($text) |
|
514 | |||
515 | 3 | private function red($text) |
|
524 | |||
525 | /** |
||
526 | * Deletes all the temporary log files for ExecutableTest objects |
||
527 | * being printed |
||
528 | */ |
||
529 | private function clearLogs() |
||
535 | } |
||
536 |
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.