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 |
||
| 10 | class ChildPage |
||
| 11 | { |
||
|
|
|||
| 12 | /** |
||
| 13 | * @var array Configuration array |
||
| 14 | */ |
||
| 15 | private $config; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var Amarkal\UI\Form The UI form instance |
||
| 19 | */ |
||
| 20 | private $form; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Set the config, create a form instance and add actions. |
||
| 24 | * |
||
| 25 | * @param array $config |
||
| 26 | */ |
||
| 27 | public function __construct( array $config = array() ) |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Internally used to add a submenu page for this child page |
||
| 38 | */ |
||
| 39 | View Code Duplication | public function add_submenu_page() |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Conditionally enqueue settings scripts and styles if the calling page is |
||
| 53 | * a settings page. |
||
| 54 | */ |
||
| 55 | public function enqueue_scripts() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Render the settings page |
||
| 67 | */ |
||
| 68 | public function render() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Ajax callback internally used to update options values for the given |
||
| 77 | * settings page. |
||
| 78 | * |
||
| 79 | * @param array $new_instance |
||
| 80 | * @return array |
||
| 81 | */ |
||
| 82 | public function update( $new_instance ) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Ajax callback internally used to reset all component values to their |
||
| 104 | * defaults for the given settings page. |
||
| 105 | * |
||
| 106 | * @return type |
||
| 107 | */ |
||
| 108 | public function reset() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Renders Amarkal's credits on the page's footer. |
||
| 128 | */ |
||
| 129 | public function footer_credits() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get the component corresponding to the given name |
||
| 136 | * |
||
| 137 | * @param [string] $name |
||
| 138 | * @throws RuntimeException when the component cannot be found |
||
| 139 | * @return void |
||
| 140 | */ |
||
| 141 | public function get_component($name) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get all errors from the form instance. |
||
| 148 | * |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | private function get_errors() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Generates a results array to be returned when an Ajax request is made. |
||
| 163 | * |
||
| 164 | * @param array $errors The list of errors |
||
| 165 | * @param array $values The list of values |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | private function results_array( $errors = array(), $values = '' ) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Check if the current user has the required privileges to update the |
||
| 178 | * settings values. |
||
| 179 | * |
||
| 180 | * @return boolean |
||
| 181 | */ |
||
| 182 | private function can_update() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Get the old instance from the database. |
||
| 189 | * |
||
| 190 | * @return array |
||
| 191 | */ |
||
| 192 | private function get_old_instance() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * The default config arguments array. |
||
| 204 | * |
||
| 205 | * @return array |
||
| 206 | */ |
||
| 207 | private function default_args() |
||
| 222 | } |