Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
31 | class LogPrinter extends Util\Printer implements TestListener |
||
32 | { |
||
33 | const STATUS_ERROR = 'error'; |
||
34 | const STATUS_WARNING = 'warning'; |
||
35 | const STATUS_FAIL = 'fail'; |
||
36 | const STATUS_PASS = 'pass'; |
||
37 | |||
38 | const MESSAGE_INCOMPLETE_TEST = 'Incomplete Test: '; |
||
39 | const MESSAGE_RISKY_TEST = 'Risky Test: '; |
||
40 | const MESSAGE_SKIPPED_TEST = 'Skipped Test: '; |
||
41 | |||
42 | /** @var resource */ |
||
43 | private $logFile; |
||
44 | |||
45 | /** @var string */ |
||
46 | private $currentTestSuiteName; |
||
47 | |||
48 | /** @var string */ |
||
49 | private $currentTestName; |
||
50 | |||
51 | /** @var bool */ |
||
52 | private $currentTestPass; |
||
53 | |||
54 | 9 | public function __construct() |
|
63 | |||
64 | /** |
||
65 | * An error occurred. |
||
66 | * |
||
67 | * @param Test $test |
||
68 | * @param \Throwable $exception |
||
69 | * @param float $time |
||
70 | */ |
||
71 | 1 | public function addError(Test $test, \Throwable $exception, float $time): void |
|
83 | |||
84 | /** |
||
85 | * A warning occurred. |
||
86 | * |
||
87 | * @param Test $test |
||
88 | * @param Warning $warning |
||
89 | * @param float $time |
||
90 | */ |
||
91 | 1 | public function addWarning(Test $test, Warning $warning, float $time): void |
|
103 | |||
104 | /** |
||
105 | * A failure occurred. |
||
106 | * |
||
107 | * @param Test $test |
||
108 | * @param AssertionFailedError $error |
||
109 | * @param float $time |
||
110 | */ |
||
111 | 1 | public function addFailure(Test $test, AssertionFailedError $error, float $time): void |
|
123 | |||
124 | /** |
||
125 | * Incomplete test. |
||
126 | * |
||
127 | * @param Test $test |
||
128 | * @param \Throwable $error |
||
129 | * @param float $time |
||
130 | */ |
||
131 | 1 | View Code Duplication | public function addIncompleteTest(Test $test, \Throwable $error, float $time): void |
143 | |||
144 | /** |
||
145 | * Risky test. |
||
146 | * |
||
147 | * @param Test $test |
||
148 | * @param \Throwable $exception |
||
149 | * @param float $time |
||
150 | */ |
||
151 | 1 | View Code Duplication | public function addRiskyTest(Test $test, \Throwable $exception, float $time): void |
163 | |||
164 | /** |
||
165 | * Skipped test. |
||
166 | * |
||
167 | * @param Test $test |
||
168 | * @param \Throwable $exception |
||
169 | * @param float $time |
||
170 | */ |
||
171 | 1 | View Code Duplication | public function addSkippedTest(Test $test, \Throwable $exception, float $time): void |
183 | |||
184 | /** |
||
185 | * A testsuite started. |
||
186 | * |
||
187 | * @param TestSuite $suite |
||
188 | * @throws \RuntimeException |
||
189 | */ |
||
190 | 9 | View Code Duplication | public function startTestSuite(TestSuite $suite): void |
201 | |||
202 | 1 | public function endTestSuite(TestSuite $suite): void |
|
207 | |||
208 | 7 | View Code Duplication | public function startTest(Test $test): void |
219 | |||
220 | /** |
||
221 | * A test ended. |
||
222 | * |
||
223 | * @param Test $test |
||
224 | * @param float $time |
||
225 | */ |
||
226 | 1 | public function endTest(Test $test, float $time): void |
|
232 | |||
233 | /** |
||
234 | * @param string $status |
||
235 | * @param float $time |
||
236 | * @param string $trace |
||
237 | * @param string $message |
||
238 | * @param Test|TestCase|null $test |
||
239 | */ |
||
240 | 7 | View Code Duplication | private function writeCase(string $status, float $time, string $trace, $message = '', $test = null) |
258 | |||
259 | /** |
||
260 | * @param array $buffer |
||
261 | */ |
||
262 | 9 | View Code Duplication | private function writeArray($buffer) |
272 | |||
273 | 9 | View Code Duplication | private function writeToLog($buffer) |
281 | |||
282 | /** |
||
283 | * @return string |
||
284 | * @throws \RuntimeException |
||
285 | * @throws \InvalidArgumentException |
||
286 | */ |
||
287 | 9 | View Code Duplication | private function getLogFilename(): string |
301 | |||
302 | /** |
||
303 | * @return string |
||
304 | * @throws \InvalidArgumentException |
||
305 | */ |
||
306 | 9 | View Code Duplication | private function getLogDirectory(): string |
320 | |||
321 | 9 | private function convertToUtf8($string): string |
|
329 | |||
330 | 6 | private function getStackTrace($error): string |
|
334 | } |
||
335 | } |
||
336 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.