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() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Generates a results array to be returned when an Ajax request is made. |
||
| 151 | * |
||
| 152 | * @param array $errors The list of errors |
||
| 153 | * @param array $values The list of values |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | private function results_array( $errors = array(), $values = '' ) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Check if the current user has the required privileges to update the |
||
| 166 | * settings values. |
||
| 167 | * |
||
| 168 | * @return boolean |
||
| 169 | */ |
||
| 170 | private function can_update() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Get the old instance from the database. |
||
| 177 | * |
||
| 178 | * @return array |
||
| 179 | */ |
||
| 180 | private function get_old_instance() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * The default config arguments array. |
||
| 192 | * |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | private function default_args() |
||
| 210 | } |