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 | const CONDITIONS = [ |
||
| 12 | 'hook', 'is_plugin_active', 'is_plugin_inactive', |
||
| 13 | ]; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | CONST ID = 'pollux-settings'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | public $hook; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * {@inheritdoc} |
||
| 27 | */ |
||
| 28 | public function init() |
||
| 29 | { |
||
| 30 | // @todo: run GateKeeper to check dependencies and capability (make sure it it run on the correct hook!) |
||
|
|
|||
| 31 | // if( !is_plugin_active( 'meta-box/meta-box.php' ))return; |
||
| 32 | |||
| 33 | $this->normalize(); |
||
| 34 | |||
| 35 | add_action( 'admin_menu', [$this, 'addPage'] ); |
||
| 36 | add_action( 'pollux/settings/init', [$this, 'addSubmitMetaBox'] ); |
||
| 37 | add_filter( 'pollux/settings/instruction', [$this, 'filterInstruction'], 10, 3 ); |
||
| 38 | add_filter( 'wp_redirect', [$this, 'filterRedirectOnSave'] ); |
||
| 39 | add_action( 'current_screen', [$this, 'register'] ); |
||
| 40 | add_action( 'admin_menu', [$this, 'registerSetting'] ); |
||
| 41 | add_action( 'pollux/settings/init', [$this, 'reset'] ); |
||
| 42 | add_action( 'admin_footer-toplevel_page_' . static::ID, [$this, 'renderFooterScript'] ); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @return void |
||
| 47 | */ |
||
| 48 | public function addPage() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @return void |
||
| 63 | */ |
||
| 64 | public function addSubmitMetaBox() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param string $instruction |
||
| 78 | * @param string $fieldId |
||
| 79 | * @param string $metaboxId |
||
| 80 | * @return string |
||
| 81 | */ |
||
| 82 | public function filterInstruction( $instruction, $fieldId, $metaboxId ) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param string $location |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | public function filterRedirectOnSave( $location ) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | public function filterSavedSettings( array $settings ) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @return void |
||
| 113 | */ |
||
| 114 | public function register( $metaboxes = [] ) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return void |
||
| 129 | */ |
||
| 130 | public function registerSetting() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @return void |
||
| 137 | */ |
||
| 138 | public function renderFooterScript() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return void |
||
| 149 | */ |
||
| 150 | public function renderPage() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @return void |
||
| 161 | */ |
||
| 162 | public function renderSubmitMetaBox() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @return void |
||
| 178 | */ |
||
| 179 | public function reset() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param string $key |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | protected function filterArrayByKey( array $array, $key ) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | protected function getDefaults() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * {@inheritdoc} |
||
| 223 | */ |
||
| 224 | View Code Duplication | protected function normalize() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * @param string $name |
||
| 239 | * @param string $parentId |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | protected function normalizeFieldName( $name, array $data, $parentId ) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param string $id |
||
| 253 | * @param string $parentId |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | protected function normalizeId( $id, array $data, $parentId ) |
||
| 262 | } |
||
| 263 |