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 Recipe extends ApiResource |
|
|
|||
8 | { |
||
9 | /** |
||
10 | * Resource type. |
||
11 | * |
||
12 | * @return string |
||
13 | */ |
||
14 | public static function resourceType() |
||
18 | |||
19 | /** |
||
20 | * Resource path (relative to Server URL). |
||
21 | * |
||
22 | * @return string |
||
23 | */ |
||
24 | public function resourcePath() |
||
28 | |||
29 | /** |
||
30 | * Recipe user. |
||
31 | * |
||
32 | * @return string|null |
||
33 | */ |
||
34 | public function user() |
||
38 | |||
39 | /** |
||
40 | * Recipe script. |
||
41 | * |
||
42 | * @return string|null |
||
43 | */ |
||
44 | public function script() |
||
48 | |||
49 | /** |
||
50 | * Run the recipe. |
||
51 | * |
||
52 | * @return bool |
||
53 | */ |
||
54 | public function run() |
||
60 | } |
||
61 |
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.