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 | * Configuration array |
||
| 14 | * |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | private $config; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The UI form instance |
||
| 21 | * |
||
| 22 | * @var Amarkal\UI\Form |
||
| 23 | */ |
||
| 24 | private $form; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Set the config, create a form instance and add actions. |
||
| 28 | * |
||
| 29 | * @param array $config |
||
| 30 | */ |
||
| 31 | public function __construct( array $config = array() ) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Internally used to add a submenu page for this child page |
||
| 44 | */ |
||
| 45 | View Code Duplication | public function add_submenu_page() |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Conditionally enqueue settings scripts and styles if the calling page is |
||
| 59 | * a settings page. |
||
| 60 | */ |
||
| 61 | public function enqueue_scripts() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Render the settings page |
||
| 73 | */ |
||
| 74 | public function render() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Ajax callback internally used to update options values for the given |
||
| 83 | * settings page. |
||
| 84 | * |
||
| 85 | * @param array $new_instance |
||
| 86 | * @return array |
||
| 87 | */ |
||
| 88 | public function update( $new_instance ) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Ajax callback internally used to reset all component values to their |
||
| 110 | * defaults for the given settings page. |
||
| 111 | * |
||
| 112 | * @return type |
||
| 113 | */ |
||
| 114 | public function reset() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Renders Amarkal's credits on the page's footer. |
||
| 134 | */ |
||
| 135 | public function footer_credits() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Get the component corresponding to the given name |
||
| 142 | * |
||
| 143 | * @param [string] $name |
||
| 144 | * @throws RuntimeException when the component cannot be found |
||
| 145 | * @return void |
||
| 146 | */ |
||
| 147 | public function get_component($name) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get all errors from the form instance. |
||
| 154 | * |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | private function get_errors() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Generates a results array to be returned when an Ajax request is made. |
||
| 169 | * |
||
| 170 | * @param array $errors The list of errors |
||
| 171 | * @param array $values The list of values |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | private function results_array( $errors = array(), $values = '' ) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Check if the current user has the required privileges to update the |
||
| 184 | * settings values. |
||
| 185 | * |
||
| 186 | * @return boolean |
||
| 187 | */ |
||
| 188 | private function can_update() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Get the old instance from the database. |
||
| 195 | * |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | private function get_old_instance() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * The default config arguments array. |
||
| 210 | * |
||
| 211 | * @return array |
||
| 212 | */ |
||
| 213 | private function default_args() |
||
| 228 | } |