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 | class Menu |
||
9 | { |
||
10 | public $page_title; |
||
11 | |||
12 | public $menu_title; |
||
13 | |||
14 | public $capability; |
||
15 | |||
16 | public $slug; |
||
17 | |||
18 | public $callback; |
||
19 | |||
20 | public $icon; |
||
21 | |||
22 | public $position; |
||
23 | |||
24 | public $plugin_name; |
||
25 | |||
26 | public function save() |
||
30 | |||
31 | View Code Duplication | public static function make($options = []) |
|
40 | |||
41 | /** |
||
42 | * Register our menu page. |
||
43 | * |
||
44 | * @return void |
||
45 | */ |
||
46 | public function create_menu() |
||
66 | |||
67 | /** |
||
68 | * Initialize our hooks for the admin page. |
||
69 | * |
||
70 | * @return void |
||
71 | */ |
||
72 | public function init_hooks() |
||
76 | |||
77 | /** |
||
78 | * Load scripts and styles for the app. |
||
79 | * |
||
80 | * @return void |
||
81 | */ |
||
82 | public function enqueue_scripts() |
||
88 | |||
89 | /** |
||
90 | * Render our admin page. |
||
91 | * |
||
92 | * @return void |
||
93 | */ |
||
94 | public function plugin_page() |
||
98 | } |
||
99 |
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.