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 SettingsPage |
||
9 | { |
||
|
|||
10 | /** |
||
11 | * Configuration array |
||
12 | * |
||
13 | * @var array |
||
14 | */ |
||
15 | private $config; |
||
16 | |||
17 | /** |
||
18 | * The UI form instance |
||
19 | * |
||
20 | * @var Amarkal\UI\Form |
||
21 | */ |
||
22 | private $form; |
||
23 | |||
24 | /** |
||
25 | * The list of arguments for each section |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | private $sections; |
||
30 | |||
31 | /** |
||
32 | * The list of fields for the entire settings page |
||
33 | * |
||
34 | * @var Amarkal\UI\ComponentList |
||
35 | */ |
||
36 | private $fields; |
||
37 | |||
38 | /** |
||
39 | * An instance of the current field values (either the from the database, or the defaults) |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | private $values; |
||
44 | |||
45 | /** |
||
46 | * Set the config, create a form instance and add actions. |
||
47 | * |
||
48 | * @param array $config |
||
49 | */ |
||
50 | public function __construct( array $config = array() ) |
||
60 | |||
61 | /** |
||
62 | * Add a section to this settings page |
||
63 | * |
||
64 | * @param array $args |
||
65 | * @return void |
||
66 | */ |
||
67 | public function add_section( array $args ) |
||
77 | |||
78 | /** |
||
79 | * Add a settings field to this settings page |
||
80 | * |
||
81 | * @param array $args |
||
82 | * @return void |
||
83 | */ |
||
84 | public function add_field( array $args ) |
||
88 | |||
89 | /** |
||
90 | * Get the current values for all fields from the database, or the default values if none exists |
||
91 | * |
||
92 | * @return array |
||
93 | */ |
||
94 | public function get_field_values() |
||
102 | |||
103 | /** |
||
104 | * Get the value of the given field from the database, or the default value if none exists |
||
105 | * |
||
106 | * @param string $name |
||
107 | * @return any |
||
108 | */ |
||
109 | public function get_field_value( $name ) |
||
114 | |||
115 | /** |
||
116 | * Internally used to add a submenu page for this settings page |
||
117 | */ |
||
118 | public function add_submenu_page() |
||
129 | |||
130 | /** |
||
131 | * Conditionally enqueue settings scripts and styles if the calling page is |
||
132 | * a settings page. |
||
133 | */ |
||
134 | public function enqueue_scripts() |
||
143 | |||
144 | /** |
||
145 | * Render the settings page |
||
146 | */ |
||
147 | public function render() |
||
153 | |||
154 | /** |
||
155 | * Ajax callback internally used to update options values for the given |
||
156 | * settings page. |
||
157 | * |
||
158 | * @param array $new_instance |
||
159 | * @return array |
||
160 | */ |
||
161 | View Code Duplication | public function update( $new_instance ) |
|
179 | |||
180 | /** |
||
181 | * Ajax callback internally used to reset all component values to their |
||
182 | * defaults for the given settings page. |
||
183 | * |
||
184 | * @return type |
||
185 | */ |
||
186 | public function reset() |
||
202 | |||
203 | /** |
||
204 | * Ajax callback internally used to reset all component values to their |
||
205 | * defaults for the given settings section. |
||
206 | * |
||
207 | * @param string $slug |
||
208 | * @return void |
||
209 | */ |
||
210 | View Code Duplication | public function reset_section( $slug ) |
|
231 | |||
232 | /** |
||
233 | * Renders Amarkal's credits on the page's footer. |
||
234 | */ |
||
235 | public function footer_credits() |
||
239 | |||
240 | /** |
||
241 | * Get the component corresponding to the given name |
||
242 | * |
||
243 | * @param [string] $name |
||
244 | * @throws RuntimeException when the component cannot be found |
||
245 | * @return void |
||
246 | */ |
||
247 | public function get_component($name) |
||
251 | |||
252 | /** |
||
253 | * Get all the fields for the given section |
||
254 | * |
||
255 | * @param string $slug |
||
256 | * @return array |
||
257 | */ |
||
258 | private function get_section_fields($slug) |
||
270 | |||
271 | /** |
||
272 | * Get all errors from the form instance. |
||
273 | * |
||
274 | * @return array |
||
275 | */ |
||
276 | private function get_errors() |
||
285 | |||
286 | /** |
||
287 | * Generates a results array to be returned when an Ajax request is made. |
||
288 | * |
||
289 | * @param array $errors The list of errors |
||
290 | * @param array $values The list of values |
||
291 | * @return array |
||
292 | */ |
||
293 | private function results_array( $errors = array(), $values = '' ) |
||
300 | |||
301 | /** |
||
302 | * Check if the current user has the required privileges to update the |
||
303 | * settings values. |
||
304 | * |
||
305 | * @return boolean |
||
306 | */ |
||
307 | private function can_update() |
||
311 | |||
312 | /** |
||
313 | * Get the old instance from the database. |
||
314 | * |
||
315 | * @return array |
||
316 | */ |
||
317 | private function get_old_instance() |
||
321 | |||
322 | /** |
||
323 | * The default config arguments array for a page. |
||
324 | * |
||
325 | * @return array |
||
326 | */ |
||
327 | private function default_args() |
||
340 | |||
341 | /** |
||
342 | * The default config arguments array for a section. |
||
343 | * |
||
344 | * @return void |
||
345 | */ |
||
346 | private function default_section_args() |
||
354 | } |