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 |
||
| 11 | class Settings extends MetaBox |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | CONST ID = 'pollux_settings'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | public $hook; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | public $id; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected static $conditions = [ |
||
| 32 | 'hook', 'is_plugin_active', 'is_plugin_inactive', |
||
| 33 | ]; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * {@inheritdoc} |
||
| 37 | */ |
||
| 38 | public function init() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @return void |
||
| 59 | * @action admin_menu |
||
| 60 | */ |
||
| 61 | public function addPage() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @return void |
||
| 76 | * @action pollux/settings/init |
||
| 77 | */ |
||
| 78 | public function addSubmitMetaBox() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param string $instruction |
||
| 92 | * @param string $fieldId |
||
| 93 | * @param string $metaboxId |
||
| 94 | * @return string |
||
| 95 | * @filter pollux/settings/instruction |
||
| 96 | */ |
||
| 97 | public function filterInstruction( $instruction, $fieldId, $metaboxId ) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param string $location |
||
| 104 | * @return string |
||
| 105 | * @filter wp_redirect |
||
| 106 | */ |
||
| 107 | public function filterRedirectOnSave( $location ) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param null|array $settings |
||
| 121 | * @return array |
||
| 122 | * @callback register_setting |
||
| 123 | */ |
||
| 124 | public function filterSavedSettings( $settings ) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return void |
||
| 134 | * @action current_screen |
||
| 135 | */ |
||
| 136 | public function register() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @return void |
||
| 151 | * @action admin_menu |
||
| 152 | */ |
||
| 153 | public function registerSetting() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return void |
||
| 160 | * @action admin_footer-toplevel_page_{$this->id} |
||
| 161 | */ |
||
| 162 | public function renderFooterScript() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @return void |
||
| 173 | * @callback add_menu_page |
||
| 174 | */ |
||
| 175 | public function renderPage() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return void |
||
| 186 | * @callback add_meta_box |
||
| 187 | */ |
||
| 188 | public function renderSubmitMetaBox() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @return void |
||
| 204 | * @action pollux/settings/init |
||
| 205 | */ |
||
| 206 | public function reset() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param string $key |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | protected function filterArrayByKey( array $array, $key ) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @return array |
||
| 231 | */ |
||
| 232 | protected function getDefaults() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @return string|array |
||
| 250 | */ |
||
| 251 | protected function getValue( $key, $group ) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * {@inheritdoc} |
||
| 258 | */ |
||
| 259 | View Code Duplication | protected function normalize() |
|
| 273 | |||
| 274 | /** |
||
| 275 | * @param string $name |
||
| 276 | * @param string $parentId |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | protected function normalizeFieldName( $name, array $data, $parentId ) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param string $id |
||
| 290 | * @param string $parentId |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | protected function normalizeId( $id, array $data, $parentId ) |
||
| 299 | } |
||
| 300 |