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 |
||
| 9 | class Settings extends MetaBox |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var string |
||
| 13 | */ |
||
| 14 | CONST ID = 'pollux_settings'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | public $hook; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | public $id; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected static $conditions = [ |
||
| 30 | 'hook', 'is_plugin_active', 'is_plugin_inactive', |
||
| 31 | ]; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * {@inheritdoc} |
||
| 35 | */ |
||
| 36 | public function init() |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @return void |
||
| 57 | * @action admin_menu |
||
| 58 | */ |
||
| 59 | public function addPage() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @return void |
||
| 74 | * @action pollux/settings/init |
||
| 75 | */ |
||
| 76 | public function addSubmitMetaBox() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param string $instruction |
||
| 90 | * @param string $fieldId |
||
| 91 | * @param string $metaboxId |
||
| 92 | * @return string |
||
| 93 | * @filter pollux/settings/instruction |
||
| 94 | */ |
||
| 95 | public function filterInstruction( $instruction, $fieldId, $metaboxId ) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param string $location |
||
| 102 | * @return string |
||
| 103 | * @filter wp_redirect |
||
| 104 | */ |
||
| 105 | public function filterRedirectOnSave( $location ) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param null|array $settings |
||
| 119 | * @return array |
||
| 120 | * @callback register_setting |
||
| 121 | */ |
||
| 122 | public function filterSavedSettings( $settings ) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return void |
||
| 132 | * @action current_screen |
||
| 133 | */ |
||
| 134 | public function register() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return void |
||
| 149 | * @action admin_menu |
||
| 150 | */ |
||
| 151 | public function registerSetting() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @return void |
||
| 158 | * @action admin_footer-toplevel_page_{$this->id} |
||
| 159 | */ |
||
| 160 | public function renderFooterScript() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @return void |
||
| 171 | * @callback add_menu_page |
||
| 172 | */ |
||
| 173 | public function renderPage() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @return void |
||
| 184 | * @callback add_meta_box |
||
| 185 | */ |
||
| 186 | public function renderSubmitMetaBox() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @return void |
||
| 202 | * @action pollux/settings/init |
||
| 203 | */ |
||
| 204 | public function reset() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param string $key |
||
| 218 | * @return array |
||
| 219 | */ |
||
| 220 | protected function filterArrayByKey( array $array, $key ) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | protected function getDefaults() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * {@inheritdoc} |
||
| 248 | */ |
||
| 249 | View Code Duplication | protected function normalize() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @param string $name |
||
| 266 | * @param string $parentId |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | protected function normalizeFieldName( $name, array $data, $parentId ) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param string $id |
||
| 280 | * @param string $parentId |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | protected function normalizeId( $id, array $data, $parentId ) |
||
| 289 | } |
||
| 290 |