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  | 
            ||
| 24 | final class Section  | 
            ||
| 25 | { | 
            ||
| 26 | /**  | 
            ||
| 27 | * Holds an instance of the section.  | 
            ||
| 28 | *  | 
            ||
| 29 | * @var ConsoleSectionOutput  | 
            ||
| 30 | */  | 
            ||
| 31 | private $section;  | 
            ||
| 32 | |||
| 33 | /**  | 
            ||
| 34 | * Holds an instance of the test suite.  | 
            ||
| 35 | *  | 
            ||
| 36 | * @var TestSuite  | 
            ||
| 37 | */  | 
            ||
| 38 | private $testSuite;  | 
            ||
| 39 | |||
| 40 | /**  | 
            ||
| 41 | * If the current testSuite is dirty  | 
            ||
| 42 | *  | 
            ||
| 43 | * @var bool  | 
            ||
| 44 | */  | 
            ||
| 45 | private $dirty = false;  | 
            ||
| 46 | |||
| 47 | /**  | 
            ||
| 48 | * Holds an instance of the test case.  | 
            ||
| 49 | *  | 
            ||
| 50 | * @var TestCase  | 
            ||
| 51 | */  | 
            ||
| 52 | private $testCase;  | 
            ||
| 53 | |||
| 54 | /**  | 
            ||
| 55 | * If the current testCase should pass.  | 
            ||
| 56 | *  | 
            ||
| 57 | * @var bool  | 
            ||
| 58 | */  | 
            ||
| 59 | private $shouldPass = true;  | 
            ||
| 60 | |||
| 61 | /**  | 
            ||
| 62 | * Holds the content of the section.  | 
            ||
| 63 | *  | 
            ||
| 64 | * @var array<int, string>  | 
            ||
| 65 | */  | 
            ||
| 66 | private $tests = [];  | 
            ||
| 67 | |||
| 68 | /**  | 
            ||
| 69 | * Section constructor.  | 
            ||
| 70 | *  | 
            ||
| 71 | * @param ConsoleSectionOutput $section  | 
            ||
| 72 | * @param TestSuite $testSuite  | 
            ||
| 73 | */  | 
            ||
| 74 | 3 | public function __construct(ConsoleSectionOutput $section, TestSuite $testSuite)  | 
            |
| 81 | |||
| 82 | /**  | 
            ||
| 83 | * @param ConsoleOutput $output  | 
            ||
| 84 | * @param TestSuite $testSuite  | 
            ||
| 85 | *  | 
            ||
| 86 | * @return Section  | 
            ||
| 87 | */  | 
            ||
| 88 | 3 | public static function create(ConsoleOutput $output, TestSuite $testSuite): Section  | 
            |
| 92 | |||
| 93 | /**  | 
            ||
| 94 | * Runs the given test.  | 
            ||
| 95 | *  | 
            ||
| 96 | * @param TestCase $test  | 
            ||
| 97 | *  | 
            ||
| 98 | * @return void  | 
            ||
| 99 | */  | 
            ||
| 100 | public function runs(TestCase $test): void  | 
            ||
| 111 | |||
| 112 | |||
| 113 | /**  | 
            ||
| 114 | * Passes the current test case.  | 
            ||
| 115 | *  | 
            ||
| 116 | * @return void  | 
            ||
| 117 | */  | 
            ||
| 118 | public function pass(): void  | 
            ||
| 124 | |||
| 125 | /**  | 
            ||
| 126 | * Marks the current test case as failed.  | 
            ||
| 127 | *  | 
            ||
| 128 | * @return void  | 
            ||
| 129 | */  | 
            ||
| 130 | View Code Duplication | public function fail(): void  | 
            |
| 138 | |||
| 139 | /**  | 
            ||
| 140 | * Marks the current test case as incomplete.  | 
            ||
| 141 | *  | 
            ||
| 142 | * @param Throwable $throwable  | 
            ||
| 143 | *  | 
            ||
| 144 | * @return void  | 
            ||
| 145 | */  | 
            ||
| 146 | View Code Duplication | public function incomplete(Throwable $throwable): void  | 
            |
| 154 | |||
| 155 | /**  | 
            ||
| 156 | * Marks the current test case as risky.  | 
            ||
| 157 | *  | 
            ||
| 158 | * @return void  | 
            ||
| 159 | */  | 
            ||
| 160 | View Code Duplication | public function risky(): void  | 
            |
| 168 | |||
| 169 | /**  | 
            ||
| 170 | * Marks the current test case as risky.  | 
            ||
| 171 | *  | 
            ||
| 172 | * @param Throwable $throwable  | 
            ||
| 173 | *  | 
            ||
| 174 | * @return void  | 
            ||
| 175 | */  | 
            ||
| 176 | View Code Duplication | public function skipped(Throwable $throwable): void  | 
            |
| 184 | |||
| 185 | /**  | 
            ||
| 186 | * Marks the current test case as risky.  | 
            ||
| 187 | *  | 
            ||
| 188 | * @return void  | 
            ||
| 189 | */  | 
            ||
| 190 | View Code Duplication | public function warn(Warning $warning): void  | 
            |
| 198 | |||
| 199 | public function end(): void  | 
            ||
| 205 | |||
| 206 | /**  | 
            ||
| 207 | * Updates the console with the current state.  | 
            ||
| 208 | *  | 
            ||
| 209 | * @param string $icon  | 
            ||
| 210 | * @param string $color  | 
            ||
| 211 | *  | 
            ||
| 212 | * @param bool $create  | 
            ||
| 213 | *  | 
            ||
| 214 | * @return void  | 
            ||
| 215 | */  | 
            ||
| 216 | private function updateTest(string $icon, string $color, bool $create = false, string $note = null): void  | 
            ||
| 237 | |||
| 238 | /**  | 
            ||
| 239 | * Updates the console with the current state.  | 
            ||
| 240 | *  | 
            ||
| 241 | * @return void  | 
            ||
| 242 | */  | 
            ||
| 243 | private function update(): void  | 
            ||
| 248 | |||
| 249 | /**  | 
            ||
| 250 | * Get the current test case description.  | 
            ||
| 251 | *  | 
            ||
| 252 | * @return string  | 
            ||
| 253 | */  | 
            ||
| 254 | private function getTestCaseDescription(): string  | 
            ||
| 273 | |||
| 274 | private function title(string $title, string $color): void  | 
            ||
| 297 | }  | 
            ||
| 298 | |||
| 299 | 
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.