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 |
||
12 | class Stencil_Config { |
||
13 | /** |
||
14 | * Implementations provided by us. |
||
15 | * |
||
16 | * @var array |
||
17 | */ |
||
18 | private $known_implementations = array( |
||
19 | 'stencil-dwoo' => 'Dwoo', |
||
20 | 'stencil-dwoo2' => 'Dwoo 2', |
||
21 | 'stencil-mustache' => 'Mustache', |
||
22 | 'stencil-savant3' => 'Savant 3', |
||
23 | 'stencil-smarty2' => 'Smarty 2.x', |
||
24 | 'stencil-smarty3' => 'Smarty 3.x', |
||
25 | 'stencil-twig' => 'Twig', |
||
26 | ); |
||
27 | |||
28 | /** |
||
29 | * Sample themes |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | private $sample_themes = array( |
||
34 | 'dwoo2' => 'Dwoo', |
||
35 | 'mustache' => 'Mustache', |
||
36 | 'savant' => 'Savant', |
||
37 | 'smarty' => 'Smarty', |
||
38 | 'twig' => 'Twig', |
||
39 | ); |
||
40 | |||
41 | /** |
||
42 | * Implementations that require a specific minimal PHP version |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | private $implementation_php_requirements = array( |
||
47 | 'stencil-dwoo' => '5.3.0', |
||
48 | 'stencil-dwoo2' => '5.3.0', |
||
49 | ); |
||
50 | |||
51 | /** |
||
52 | * Option page |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | private $option_page = 'stencil-options'; |
||
57 | |||
58 | /** |
||
59 | * Option group |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | private $option_group = 'stencil-implementations'; |
||
64 | |||
65 | /** |
||
66 | * The name of the option |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | private $option_name = 'install'; |
||
71 | |||
72 | /** |
||
73 | * Stencil_Config constructor. |
||
74 | */ |
||
75 | public function __construct() { |
||
80 | |||
81 | /** |
||
82 | * Create the menu item |
||
83 | */ |
||
84 | public function create_admin_menu() { |
||
94 | |||
95 | /** |
||
96 | * Register settings |
||
97 | */ |
||
98 | public function register_options() { |
||
125 | |||
126 | /** |
||
127 | * Show plugins that are not installed yet (but tracked) |
||
128 | * Check to install; installed plugins are grayed out and checked |
||
129 | * but are ignored on save. |
||
130 | */ |
||
131 | public function option_implementations() { |
||
169 | |||
170 | /** |
||
171 | * Show plugins that are not installed yet (but tracked) |
||
172 | * Check to install; installed plugins are grayed out and checked |
||
173 | * but are ignored on save. |
||
174 | */ |
||
175 | public function option_themes() { |
||
187 | |||
188 | /** |
||
189 | * Show the settings |
||
190 | */ |
||
191 | public function settings_page() { |
||
217 | |||
218 | /** |
||
219 | * Install selected plugins |
||
220 | */ |
||
221 | View Code Duplication | public function maybe_install_plugins() { |
|
264 | |||
265 | /** |
||
266 | * Install selected themes |
||
267 | */ |
||
268 | View Code Duplication | public function maybe_install_themes() { |
|
311 | |||
312 | /** |
||
313 | * Install plugin by slug |
||
314 | * |
||
315 | * @param string $slug Plugin slug. |
||
316 | * |
||
317 | * @return bool |
||
318 | */ |
||
319 | public function install_plugin( $slug ) { |
||
331 | |||
332 | /** |
||
333 | * Get the download link for the plugin |
||
334 | * |
||
335 | * @param string $slug Plugin slug. |
||
336 | * |
||
337 | * @return string |
||
338 | */ |
||
339 | private function get_plugin_download_link( $slug ) { |
||
342 | |||
343 | /** |
||
344 | * Install theme by slug |
||
345 | * |
||
346 | * @param string $slug Theme slug. |
||
347 | * |
||
348 | * @return bool |
||
349 | */ |
||
350 | public function install_theme( $slug ) { |
||
362 | |||
363 | /** |
||
364 | * Get the download link for the plugin |
||
365 | * |
||
366 | * @param string $slug Plugin slug. |
||
367 | * |
||
368 | * @return string |
||
369 | */ |
||
370 | private function get_theme_download_link( $slug ) { |
||
373 | } |
||
374 |
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.