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 Composer implements ToolInterface |
|
|
|||
8 | { |
||
9 | /** |
||
10 | * @var Enabled |
||
11 | */ |
||
12 | private $enabled; |
||
13 | /** |
||
14 | * @var Undefined |
||
15 | */ |
||
16 | private $undefined; |
||
17 | |||
18 | /** |
||
19 | * Composer constructor. |
||
20 | * |
||
21 | * @param Undefined $undefined |
||
22 | * @param Enabled $enabled |
||
23 | */ |
||
24 | 6 | public function __construct(Undefined $undefined, Enabled $enabled) |
|
29 | |||
30 | /** |
||
31 | * @return bool |
||
32 | */ |
||
33 | 4 | public function isEnabled() |
|
37 | |||
38 | /** |
||
39 | * @return bool |
||
40 | */ |
||
41 | 3 | public function isUndefined() |
|
45 | |||
46 | /** |
||
47 | * @param Enabled $enabled |
||
48 | * |
||
49 | * @return Composer |
||
50 | */ |
||
51 | 2 | public function setEnabled(Enabled $enabled) |
|
58 | } |
||
59 |
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.