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 |
||
17 | class CreatePostTypeTable extends Migration |
||
18 | { |
||
19 | const GAME_REVIEW_TYPE = [ |
||
20 | [ |
||
21 | 'name' => 'Release Date', |
||
22 | 'code' => 'release-date', |
||
23 | 'type' => 'datepicker', |
||
24 | 'datepicker_mode' => 'date' |
||
25 | ], |
||
26 | [ |
||
27 | 'name' => 'Publisher', |
||
28 | 'code' => 'publisher', |
||
29 | 'type' => 'text' |
||
30 | ], |
||
31 | [ |
||
32 | 'name' => 'Editor', |
||
33 | 'code' => 'editor', |
||
34 | 'type' => 'text' |
||
35 | ], |
||
36 | [ |
||
37 | 'name' => 'Rating', |
||
38 | 'code' => 'rating', |
||
39 | 'type' => 'dropdown', |
||
40 | 'dropdown_options' => '1,2,3,4,5,6,7,8,9,10', |
||
41 | ], |
||
42 | [ |
||
43 | 'name' => 'Pros', |
||
44 | 'code' => 'pros', |
||
45 | 'type' => 'textarea' |
||
46 | ], |
||
47 | [ |
||
48 | 'name' => 'Cons', |
||
49 | 'code' => 'cons', |
||
50 | 'type' => 'textarea' |
||
51 | ], |
||
52 | ]; |
||
53 | |||
54 | /** |
||
55 | * Execute migrations |
||
56 | */ |
||
57 | public function up() |
||
63 | |||
64 | /** |
||
65 | * Rollback migrations |
||
66 | */ |
||
67 | public function down() |
||
73 | |||
74 | /** |
||
75 | * Rollback PostType migration |
||
76 | */ |
||
77 | View Code Duplication | private function dropPostTypes() |
|
93 | |||
94 | /** |
||
95 | * Create PostType table |
||
96 | */ |
||
97 | private function createPostTypes() |
||
132 | } |
||
133 |
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.