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 Controller |
||
12 | { |
||
13 | /** |
||
14 | * @return void |
||
15 | * @action after_setup_theme |
||
16 | */ |
||
17 | public function afterSetupTheme() |
||
39 | |||
40 | /** |
||
41 | * @return array |
||
42 | * @filter body_class |
||
43 | */ |
||
44 | public function filterBodyClasses( array $classes ) |
||
51 | |||
52 | /** |
||
53 | * @return string |
||
54 | * @filter login_headertitle |
||
55 | */ |
||
56 | public function filterLoginTitle() |
||
60 | |||
61 | /** |
||
62 | * @return string |
||
63 | * @filter login_headerurl |
||
64 | */ |
||
65 | public function filterLoginUrl() |
||
69 | |||
70 | /** |
||
71 | * @return string |
||
72 | * @filter template_include |
||
73 | */ |
||
74 | public function filterTemplate( $template ) |
||
82 | |||
83 | /** |
||
84 | * @return array |
||
85 | * @filter {$type}_template_hierarchy |
||
86 | */ |
||
87 | public function filterTemplateHierarchy( array $templates ) |
||
93 | |||
94 | /** |
||
95 | * @return void |
||
96 | * @action admin_head |
||
97 | * @action login_head |
||
98 | */ |
||
99 | public function loadAdminFavicon() |
||
105 | |||
106 | /** |
||
107 | * @return void |
||
108 | * @action login_head |
||
109 | */ |
||
110 | public function login() |
||
116 | |||
117 | /** |
||
118 | * @return void |
||
119 | * @action admin_enqueue_scripts |
||
120 | */ |
||
121 | public function registerAdminAssets() |
||
139 | |||
140 | /** |
||
141 | * @return void |
||
142 | * @action wp_enqueue_scripts |
||
143 | */ |
||
144 | public function registerAssets() |
||
163 | |||
164 | /** |
||
165 | * @return void |
||
166 | * @action customize_register |
||
167 | */ |
||
168 | public function registerCustomizer( WP_Customize_Manager $manager ) |
||
178 | |||
179 | /** |
||
180 | * @return void |
||
181 | * @action customize_preview_init |
||
182 | */ |
||
183 | public function registerCustomizerAssets() |
||
187 | |||
188 | /** |
||
189 | * @return void |
||
190 | * @action widgets_init |
||
191 | */ |
||
192 | public function registerSidebars() |
||
213 | } |
||
214 |
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.