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 |
||
| 23 | class LogPrinter extends Util\Printer implements TestListener |
||
| 24 | { |
||
| 25 | /** @var resource */ |
||
| 26 | private $logFile; |
||
| 27 | |||
| 28 | /** @var string */ |
||
| 29 | private $currentTestSuiteName; |
||
| 30 | |||
| 31 | /** @var string */ |
||
| 32 | private $currentTestName; |
||
| 33 | |||
| 34 | /** @var bool */ |
||
| 35 | private $currentTestPass; |
||
| 36 | |||
| 37 | 9 | public function __construct() |
|
| 42 | |||
| 43 | /** |
||
| 44 | * An error occurred. |
||
| 45 | * |
||
| 46 | * @param Test $test |
||
| 47 | * @param \Exception $e |
||
| 48 | * @param float $time |
||
| 49 | */ |
||
| 50 | 1 | public function addError(Test $test, \Exception $e, $time) |
|
| 51 | { |
||
| 52 | 1 | $this->writeCase( |
|
| 53 | 1 | 'error', |
|
| 54 | 1 | $time, |
|
| 55 | 1 | Util\Filter::getFilteredStacktrace($e, false), |
|
| 56 | 1 | TestFailure::exceptionToString($e), |
|
| 57 | 1 | $test |
|
| 58 | ); |
||
| 59 | |||
| 60 | 1 | $this->currentTestPass = false; |
|
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * A warning occurred. |
||
| 65 | * |
||
| 66 | * @param Test $test |
||
| 67 | * @param Warning $e |
||
| 68 | * @param float $time |
||
| 69 | */ |
||
| 70 | 1 | View Code Duplication | public function addWarning(Test $test, Warning $e, $time) |
| 82 | |||
| 83 | /** |
||
| 84 | * A failure occurred. |
||
| 85 | * |
||
| 86 | * @param Test $test |
||
| 87 | * @param AssertionFailedError $e |
||
| 88 | * @param float $time |
||
| 89 | */ |
||
| 90 | 1 | View Code Duplication | public function addFailure(Test $test, AssertionFailedError $e, $time) |
| 102 | |||
| 103 | /** |
||
| 104 | * Incomplete test. |
||
| 105 | * |
||
| 106 | * @param Test $test |
||
| 107 | * @param \Exception $e |
||
| 108 | * @param float $time |
||
| 109 | */ |
||
| 110 | 1 | View Code Duplication | public function addIncompleteTest(Test $test, \Exception $e, $time) |
| 122 | |||
| 123 | /** |
||
| 124 | * Risky test. |
||
| 125 | * |
||
| 126 | * @param Test $test |
||
| 127 | * @param \Exception $e |
||
| 128 | * @param float $time |
||
| 129 | */ |
||
| 130 | 1 | View Code Duplication | public function addRiskyTest(Test $test, \Exception $e, $time) |
| 142 | |||
| 143 | /** |
||
| 144 | * Skipped test. |
||
| 145 | * |
||
| 146 | * @param Test $test |
||
| 147 | * @param \Exception $e |
||
| 148 | * @param float $time |
||
| 149 | */ |
||
| 150 | 1 | View Code Duplication | public function addSkippedTest(Test $test, \Exception $e, $time) |
| 162 | |||
| 163 | /** |
||
| 164 | * A testsuite started. |
||
| 165 | * |
||
| 166 | * @param TestSuite $suite |
||
| 167 | * @throws \RuntimeException |
||
| 168 | */ |
||
| 169 | 9 | public function startTestSuite(TestSuite $suite) |
|
| 180 | |||
| 181 | 1 | public function endTestSuite(TestSuite $suite) |
|
| 186 | |||
| 187 | 7 | public function startTest(Test $test) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * A test ended. |
||
| 201 | * |
||
| 202 | * @param Test $test |
||
| 203 | * @param float $time |
||
| 204 | */ |
||
| 205 | 1 | public function endTest(Test $test, $time) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @param string $status |
||
| 214 | * @param float $time |
||
| 215 | * @param array $trace |
||
| 216 | * @param string $message |
||
| 217 | * @param TestCase|null $test |
||
| 218 | */ |
||
| 219 | 7 | private function writeCase($status, $time, array $trace = [], $message = '', $test = null) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * @param array $buffer |
||
| 240 | */ |
||
| 241 | private function writeArray($buffer) |
||
| 251 | |||
| 252 | 9 | private function writeToLog($buffer) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * @return string |
||
| 263 | * @throws \RuntimeException |
||
| 264 | * @throws \InvalidArgumentException |
||
| 265 | */ |
||
| 266 | 9 | private function getLogFilename(): string |
|
| 280 | |||
| 281 | /** |
||
| 282 | * @return string |
||
| 283 | * @throws \InvalidArgumentException |
||
| 284 | */ |
||
| 285 | 9 | private function getLogDirectory(): string |
|
| 299 | |||
| 300 | 9 | private function convertToUtf8($string): string |
|
| 308 | } |
||
| 309 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.