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 |
||
11 | class StarWarsData |
||
12 | { |
||
13 | View Code Duplication | private static function luke() |
|
23 | |||
24 | View Code Duplication | private static function vader() |
|
34 | |||
35 | View Code Duplication | private static function han() |
|
44 | |||
45 | View Code Duplication | private static function leia() |
|
55 | |||
56 | private static function tarkin() |
||
65 | |||
66 | static function humans() |
||
76 | |||
77 | View Code Duplication | private static function threepio() |
|
87 | |||
88 | /** |
||
89 | * We export artoo directly because the schema returns him |
||
90 | * from a root field, and hence needs to reference him. |
||
91 | */ |
||
92 | static function artoo() |
||
103 | |||
104 | static function droids() |
||
111 | |||
112 | /** |
||
113 | * Helper function to get a character by ID. |
||
114 | */ |
||
115 | static function getCharacter($id) |
||
128 | |||
129 | /** |
||
130 | * @param $episode |
||
131 | * @return array |
||
132 | */ |
||
133 | static function getHero($episode) |
||
143 | |||
144 | /** |
||
145 | * Allows us to query for a character's friends. |
||
146 | */ |
||
147 | static function getFriends($character) |
||
151 | } |
||
152 |
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.