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 |
||
27 | class Settings { |
||
28 | |||
29 | /** |
||
30 | * The plugin's instance. |
||
31 | * |
||
32 | * @since 1.0.0 |
||
33 | * @access private |
||
34 | * @var Plugin $plugin This plugin's instance. |
||
35 | */ |
||
36 | private $plugin; |
||
37 | |||
38 | /** |
||
39 | * Initialize the class and set its properties. |
||
40 | * |
||
41 | * @since 1.0.0 |
||
42 | * |
||
43 | * @param Plugin $plugin This plugin's instance. |
||
44 | */ |
||
45 | public function __construct( Plugin $plugin ) { |
||
48 | |||
49 | /** |
||
50 | * Register the menu entry for the plugin's settings page. |
||
51 | * |
||
52 | * @since 1.1.0 |
||
53 | */ |
||
54 | public function menu() { |
||
63 | |||
64 | /** |
||
65 | * Register plugin settings. |
||
66 | * |
||
67 | * @uses add_settings_field() |
||
68 | * @uses add_settings_section() |
||
69 | * @uses register_setting() |
||
70 | * |
||
71 | * @since 1.0.0 |
||
72 | */ |
||
73 | public function add() { |
||
97 | |||
98 | /** |
||
99 | * Validates and updates CAS server plugin settings. |
||
100 | * |
||
101 | * @param array $input Unvalidated input arguments when settings are updated. |
||
102 | * |
||
103 | * @return array Validated plugin settings to be saved in the database. |
||
104 | * |
||
105 | * @since 1.1.0 |
||
106 | */ |
||
107 | public function validate( $input ) { |
||
115 | |||
116 | /** |
||
117 | * Displays the CAS server settings page in the dashboard. |
||
118 | * |
||
119 | * @uses \_e() |
||
120 | * @uses \do_settings_sections() |
||
121 | * @uses \settings_fields() |
||
122 | * @uses \submit_button() |
||
123 | * |
||
124 | * @since 1.1.0 |
||
125 | */ |
||
126 | public function page() { |
||
141 | |||
142 | /** |
||
143 | * Display the configuration field for the Redmine root URL. |
||
144 | * |
||
145 | * @uses \esc_url() |
||
146 | * |
||
147 | * @since 1.0.0 |
||
148 | */ |
||
149 | View Code Duplication | public function field_root_url() { |
|
160 | |||
161 | /** |
||
162 | * Display the configuration field for the Redmine API key. |
||
163 | * |
||
164 | * @uses \esc_url() |
||
165 | * |
||
166 | * @since 1.0.0 |
||
167 | */ |
||
168 | View Code Duplication | public function field_api_key() { |
|
179 | |||
180 | } |
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.