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 | * Set the config, create a form instance and add actions. |
||
40 | * |
||
41 | * @param array $config |
||
42 | */ |
||
43 | public function __construct( array $config = array() ) |
||
53 | |||
54 | /** |
||
55 | * Add a section to this settings page |
||
56 | * |
||
57 | * @param array $args |
||
58 | * @return void |
||
59 | */ |
||
60 | public function add_section( array $args ) |
||
70 | |||
71 | /** |
||
72 | * Add a settings field to this settings page |
||
73 | * |
||
74 | * @param array $args |
||
75 | * @return void |
||
76 | */ |
||
77 | public function add_field( array $args ) |
||
81 | |||
82 | /** |
||
83 | * Internally used to add a submenu page for this settings page |
||
84 | */ |
||
85 | public function add_submenu_page() |
||
96 | |||
97 | /** |
||
98 | * Conditionally enqueue settings scripts and styles if the calling page is |
||
99 | * a settings page. |
||
100 | */ |
||
101 | public function enqueue_scripts() |
||
110 | |||
111 | /** |
||
112 | * Render the settings page |
||
113 | */ |
||
114 | public function render() |
||
120 | |||
121 | /** |
||
122 | * Ajax callback internally used to update options values for the given |
||
123 | * settings page. |
||
124 | * |
||
125 | * @param array $new_instance |
||
126 | * @return array |
||
127 | */ |
||
128 | View Code Duplication | public function update( $new_instance ) |
|
146 | |||
147 | /** |
||
148 | * Ajax callback internally used to reset all component values to their |
||
149 | * defaults for the given settings page. |
||
150 | * |
||
151 | * @return type |
||
152 | */ |
||
153 | public function reset() |
||
168 | |||
169 | /** |
||
170 | * Ajax callback internally used to reset all component values to their |
||
171 | * defaults for the given settings section. |
||
172 | * |
||
173 | * @param string $slug |
||
174 | * @return void |
||
175 | */ |
||
176 | View Code Duplication | public function reset_section( $slug ) |
|
198 | |||
199 | /** |
||
200 | * Renders Amarkal's credits on the page's footer. |
||
201 | */ |
||
202 | public function footer_credits() |
||
206 | |||
207 | /** |
||
208 | * Get the component corresponding to the given name |
||
209 | * |
||
210 | * @param [string] $name |
||
211 | * @throws RuntimeException when the component cannot be found |
||
212 | * @return void |
||
213 | */ |
||
214 | public function get_component($name) |
||
218 | |||
219 | /** |
||
220 | * Get all the fields for the given section |
||
221 | * |
||
222 | * @param string $slug |
||
223 | * @return array |
||
224 | */ |
||
225 | private function get_section_fields($slug) |
||
237 | |||
238 | /** |
||
239 | * Get all errors from the form instance. |
||
240 | * |
||
241 | * @return array |
||
242 | */ |
||
243 | private function get_errors() |
||
252 | |||
253 | /** |
||
254 | * Generates a results array to be returned when an Ajax request is made. |
||
255 | * |
||
256 | * @param array $errors The list of errors |
||
257 | * @param array $values The list of values |
||
258 | * @return array |
||
259 | */ |
||
260 | private function results_array( $errors = array(), $values = '' ) |
||
267 | |||
268 | /** |
||
269 | * Check if the current user has the required privileges to update the |
||
270 | * settings values. |
||
271 | * |
||
272 | * @return boolean |
||
273 | */ |
||
274 | private function can_update() |
||
278 | |||
279 | /** |
||
280 | * Get the old instance from the database. |
||
281 | * |
||
282 | * @return array |
||
283 | */ |
||
284 | private function get_old_instance() |
||
288 | |||
289 | /** |
||
290 | * The default config arguments array for a page. |
||
291 | * |
||
292 | * @return array |
||
293 | */ |
||
294 | private function default_args() |
||
307 | |||
308 | /** |
||
309 | * The default config arguments array for a section. |
||
310 | * |
||
311 | * @return void |
||
312 | */ |
||
313 | private function default_section_args() |
||
321 | } |