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 |
||
10 | View Code Duplication | class CountryEntity |
|
|
|||
11 | { |
||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | public $short; |
||
16 | |||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | public $name; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | public $de; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | public $en; |
||
31 | |||
32 | /** |
||
33 | * @var int |
||
34 | */ |
||
35 | public $translationId; |
||
36 | |||
37 | /** |
||
38 | * @var bool |
||
39 | */ |
||
40 | public $listDefaultDe; |
||
41 | |||
42 | /** |
||
43 | * @var bool |
||
44 | */ |
||
45 | public $listDefaultEn; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | public $sortDe; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | public $sortEn; |
||
56 | |||
57 | /** |
||
58 | * Checks if the entity is new. |
||
59 | * |
||
60 | * @return bool |
||
61 | */ |
||
62 | public function isNew() |
||
66 | |||
67 | /** |
||
68 | * Sets all properties from array. |
||
69 | * |
||
70 | * @param array $data |
||
71 | */ |
||
72 | public function fromArray(array $data) |
||
82 | |||
83 | /** |
||
84 | * Returns all properties as array. |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | public function toArray() |
||
92 | } |
||
93 |
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.