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 |
||
7 | View Code Duplication | class ComposerTool implements CommandInterface |
|
|
|||
8 | { |
||
9 | /** |
||
10 | * @var array |
||
11 | */ |
||
12 | private $files; |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | private $errorMessage; |
||
17 | |||
18 | /** |
||
19 | * ComposerToolCommand constructor. |
||
20 | * |
||
21 | * @param array $files |
||
22 | * @param string $errorMessage |
||
23 | */ |
||
24 | 4 | public function __construct(array $files, $errorMessage) |
|
29 | |||
30 | /** |
||
31 | * @return string |
||
32 | */ |
||
33 | 3 | public function getErrorMessage() |
|
37 | |||
38 | /** |
||
39 | * @return array |
||
40 | */ |
||
41 | 3 | public function getFiles() |
|
45 | } |
||
46 |
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.