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 |
||
8 | View Code Duplication | class RegionNameEntity implements JsonSerializable |
|
|
|||
9 | { |
||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | private $name; |
||
14 | |||
15 | /** |
||
16 | * @var string|LanguageEntity |
||
17 | */ |
||
18 | private $language; |
||
19 | |||
20 | /** |
||
21 | * @return array |
||
22 | */ |
||
23 | public function jsonSerialize() |
||
30 | |||
31 | /** |
||
32 | * @return LanguageEntity |
||
33 | */ |
||
34 | public function getLanguage() |
||
38 | |||
39 | /** |
||
40 | * @param LanguageEntity|string $language |
||
41 | * |
||
42 | * @return $this |
||
43 | */ |
||
44 | 4 | public function setLanguage($language) |
|
50 | |||
51 | /** |
||
52 | * @return string |
||
53 | */ |
||
54 | public function getName() |
||
58 | |||
59 | /** |
||
60 | * @param string $name |
||
61 | * |
||
62 | * @return $this |
||
63 | */ |
||
64 | 4 | public function setName($name) |
|
70 | |||
71 | /** |
||
72 | * @return string |
||
73 | */ |
||
74 | public function __toString() |
||
78 | } |
||
79 |
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.