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 | final class Section |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Holds an instance of the console output. |
||
| 27 | * |
||
| 28 | * @var ConsoleOutput |
||
| 29 | */ |
||
| 30 | private $output; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Holds an instance of the console section. |
||
| 34 | * |
||
| 35 | * @var ConsoleSectionOutput |
||
| 36 | */ |
||
| 37 | private $section; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Holds an instance of the console section. |
||
| 41 | * |
||
| 42 | * @var ConsoleSectionOutput |
||
| 43 | */ |
||
| 44 | private $footer; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * If the current testSuite is dirty. |
||
| 48 | * |
||
| 49 | * @var bool |
||
| 50 | */ |
||
| 51 | private $dirty = false; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Holds an instance of the test case. |
||
| 55 | * |
||
| 56 | * @var TestCase |
||
| 57 | */ |
||
| 58 | private $testCase; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * If the current testCase should pass. |
||
| 62 | * |
||
| 63 | * @var bool |
||
| 64 | */ |
||
| 65 | public $shouldPass = true; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Holds the content of the section. |
||
| 69 | * |
||
| 70 | * @var array<int, string> |
||
| 71 | */ |
||
| 72 | private $tests = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Section constructor. |
||
| 76 | * |
||
| 77 | * @param ConsoleOutput $output |
||
| 78 | */ |
||
| 79 | 3 | public function __construct(ConsoleOutput $output) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Runs the given test. |
||
| 90 | * |
||
| 91 | * @param TestCase $test |
||
| 92 | * |
||
| 93 | * @return void |
||
| 94 | */ |
||
| 95 | public function runs(TestCase $test): void |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Passes the current test case. |
||
| 111 | * |
||
| 112 | * @return void |
||
| 113 | */ |
||
| 114 | public function pass(): void |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Marks the current test case as failed. |
||
| 123 | * |
||
| 124 | * @return void |
||
| 125 | */ |
||
| 126 | public function fail(): void |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Marks the current test case as incomplete. |
||
| 140 | * |
||
| 141 | * @param Throwable $throwable |
||
| 142 | * |
||
| 143 | * @return void |
||
| 144 | */ |
||
| 145 | View Code Duplication | public function incomplete(Throwable $throwable): void |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Marks the current test case as risky. |
||
| 155 | * |
||
| 156 | * @return void |
||
| 157 | */ |
||
| 158 | public function risky(): void |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Marks the current test case as risky. |
||
| 168 | * |
||
| 169 | * @param Throwable $throwable |
||
| 170 | * |
||
| 171 | * @return void |
||
| 172 | */ |
||
| 173 | View Code Duplication | public function skipped(Throwable $throwable): void |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Marks the current test case as risky. |
||
| 183 | * |
||
| 184 | * @return void |
||
| 185 | */ |
||
| 186 | View Code Duplication | public function warn(Warning $warning): void |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Ends the current test suite. |
||
| 196 | * |
||
| 197 | * Here we do 3 things: |
||
| 198 | * |
||
| 199 | * 0. Remove the footer. |
||
| 200 | * 1. Display the title. |
||
| 201 | * 2. Display the tests results. |
||
| 202 | */ |
||
| 203 | public function end(): void |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Updates the console with the current state. |
||
| 221 | * |
||
| 222 | * @param string $icon |
||
| 223 | * @param string $color |
||
| 224 | * |
||
| 225 | * @param bool $create |
||
| 226 | * |
||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | private function updateTest(string $icon, string $color, bool $create = false, string $note = null): void |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Get the current test case description. |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | private function getTestCaseDescription(): string |
||
| 276 | |||
| 277 | private function footer(string $title, string $color): void |
||
| 300 | |||
| 301 | private function title(string $title, string $color): void |
||
| 321 | } |
||
| 322 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: