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 all errors from the form instance. |
||
| 136 | * |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | private function get_errors() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Generates a results array to be returned when an Ajax request is made. |
||
| 155 | * |
||
| 156 | * @param array $errors The list of errors |
||
| 157 | * @param array $values The list of values |
||
| 158 | * @return array |
||
| 159 | */ |
||
| 160 | private function results_array( $errors = array(), $values = '' ) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Check if the current user has the required privileges to update the |
||
| 170 | * settings values. |
||
| 171 | * |
||
| 172 | * @return boolean |
||
| 173 | */ |
||
| 174 | private function can_update() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get the old instance from the database. |
||
| 181 | * |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | private function get_old_instance() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * The default config arguments array. |
||
| 196 | * |
||
| 197 | * @return array |
||
| 198 | */ |
||
| 199 | private function default_args() |
||
| 213 | } |