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 | * Get the value of the given field from the database, or the default value if none exists | ||
| 84 | * | ||
| 85 | * @param string $name | ||
| 86 | * @return void | ||
| 87 | */ | ||
| 88 | public function get_field_value( $name ) | ||
| 100 | |||
| 101 | /** | ||
| 102 | * Internally used to add a submenu page for this settings page | ||
| 103 | */ | ||
| 104 | public function add_submenu_page() | ||
| 115 | |||
| 116 | /** | ||
| 117 | * Conditionally enqueue settings scripts and styles if the calling page is | ||
| 118 | * a settings page. | ||
| 119 | */ | ||
| 120 | public function enqueue_scripts() | ||
| 129 | |||
| 130 | /** | ||
| 131 | * Render the settings page | ||
| 132 | */ | ||
| 133 | public function render() | ||
| 139 | |||
| 140 | /** | ||
| 141 | * Ajax callback internally used to update options values for the given | ||
| 142 | * settings page. | ||
| 143 | * | ||
| 144 | * @param array $new_instance | ||
| 145 | * @return array | ||
| 146 | */ | ||
| 147 | View Code Duplication | public function update( $new_instance ) | |
| 165 | |||
| 166 | /** | ||
| 167 | * Ajax callback internally used to reset all component values to their | ||
| 168 | * defaults for the given settings page. | ||
| 169 | * | ||
| 170 | * @return type | ||
| 171 | */ | ||
| 172 | public function reset() | ||
| 187 | |||
| 188 | /** | ||
| 189 | * Ajax callback internally used to reset all component values to their | ||
| 190 | * defaults for the given settings section. | ||
| 191 | * | ||
| 192 | * @param string $slug | ||
| 193 | * @return void | ||
| 194 | */ | ||
| 195 | View Code Duplication | public function reset_section( $slug ) | |
| 217 | |||
| 218 | /** | ||
| 219 | * Renders Amarkal's credits on the page's footer. | ||
| 220 | */ | ||
| 221 | public function footer_credits() | ||
| 225 | |||
| 226 | /** | ||
| 227 | * Get the component corresponding to the given name | ||
| 228 | * | ||
| 229 | * @param [string] $name | ||
| 230 | * @throws RuntimeException when the component cannot be found | ||
| 231 | * @return void | ||
| 232 | */ | ||
| 233 | public function get_component($name) | ||
| 237 | |||
| 238 | /** | ||
| 239 | * Get all the fields for the given section | ||
| 240 | * | ||
| 241 | * @param string $slug | ||
| 242 | * @return array | ||
| 243 | */ | ||
| 244 | private function get_section_fields($slug) | ||
| 256 | |||
| 257 | /** | ||
| 258 | * Get all errors from the form instance. | ||
| 259 | * | ||
| 260 | * @return array | ||
| 261 | */ | ||
| 262 | private function get_errors() | ||
| 271 | |||
| 272 | /** | ||
| 273 | * Generates a results array to be returned when an Ajax request is made. | ||
| 274 | * | ||
| 275 | * @param array $errors The list of errors | ||
| 276 | * @param array $values The list of values | ||
| 277 | * @return array | ||
| 278 | */ | ||
| 279 | private function results_array( $errors = array(), $values = '' ) | ||
| 286 | |||
| 287 | /** | ||
| 288 | * Check if the current user has the required privileges to update the | ||
| 289 | * settings values. | ||
| 290 | * | ||
| 291 | * @return boolean | ||
| 292 | */ | ||
| 293 | private function can_update() | ||
| 297 | |||
| 298 | /** | ||
| 299 | * Get the old instance from the database. | ||
| 300 | * | ||
| 301 | * @return array | ||
| 302 | */ | ||
| 303 | private function get_old_instance() | ||
| 307 | |||
| 308 | /** | ||
| 309 | * The default config arguments array for a page. | ||
| 310 | * | ||
| 311 | * @return array | ||
| 312 | */ | ||
| 313 | private function default_args() | ||
| 326 | |||
| 327 | /** | ||
| 328 | * The default config arguments array for a section. | ||
| 329 | * | ||
| 330 | * @return void | ||
| 331 | */ | ||
| 332 | private function default_section_args() | ||
| 340 | } |