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 = 'settings'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | public static $conditions = [ |
||
| 22 | 'class_exists', 'defined', 'function_exists', 'hook', 'is_plugin_active', |
||
| 23 | 'is_plugin_inactive', |
||
| 24 | ]; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | public $hook; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @return string |
||
| 33 | */ |
||
| 34 | public static function id() |
||
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritdoc} |
||
| 41 | */ |
||
| 42 | public function init() |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @return void |
||
| 61 | */ |
||
| 62 | public function action() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return void |
||
| 71 | * @action admin_menu |
||
| 72 | */ |
||
| 73 | View Code Duplication | public function addPage() |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @return void |
||
| 88 | * @action pollux/{static::ID}/init |
||
| 89 | */ |
||
| 90 | public function addSubmitMetaBox() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return mixed |
||
| 104 | */ |
||
| 105 | public function filter() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param string $instruction |
||
| 114 | * @param string $fieldId |
||
| 115 | * @param string $metaboxId |
||
| 116 | * @return string |
||
| 117 | * @action pollux/{static::ID}/instruction |
||
| 118 | */ |
||
| 119 | public function filterInstruction( $instruction, $fieldId, $metaboxId ) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param string $location |
||
| 126 | * @return string |
||
| 127 | * @filter wp_redirect |
||
| 128 | */ |
||
| 129 | public function filterRedirectOnSave( $location ) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param null|array $settings |
||
| 143 | * @return array |
||
| 144 | * @callback register_setting |
||
| 145 | */ |
||
| 146 | public function filterSavedSettings( $settings ) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return void |
||
| 157 | * @action current_screen |
||
| 158 | */ |
||
| 159 | public function register() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @return void |
||
| 174 | * @action admin_menu |
||
| 175 | */ |
||
| 176 | public function registerSetting() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @return void |
||
| 183 | * @action admin_print_footer_scripts |
||
| 184 | */ |
||
| 185 | public function renderFooterScript() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @return void |
||
| 197 | * @callback add_menu_page |
||
| 198 | */ |
||
| 199 | public function renderPage() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return void |
||
| 210 | * @callback add_meta_box |
||
| 211 | */ |
||
| 212 | public function renderSubmitMetaBox() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @return void |
||
| 228 | * @action pollux/{static::ID}/init |
||
| 229 | */ |
||
| 230 | public function reset() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param string $key |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | protected function filterArrayByKey( array $array, $key ) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | protected function getDefaults() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @return string|array |
||
| 274 | */ |
||
| 275 | protected function getValue( $key, $group ) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param string $name |
||
| 282 | * @param string $parentId |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | protected function normalizeFieldName( $name, array $data, $parentId ) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param string $id |
||
| 296 | * @param string $parentId |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | protected function normalizeId( $id, array $data, $parentId ) |
||
| 305 | } |
||
| 306 |