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 | const ID = 'settings'; |
||
14 | |||
15 | const CAPABILITY = 'edit_theme_options'; |
||
16 | const DEPENDENCY = 'meta-box/meta-box.php'; |
||
17 | |||
18 | const CONDITIONS = [ |
||
19 | 'class_exists', 'defined', 'function_exists', 'hook', 'is_plugin_active', |
||
20 | 'is_plugin_inactive', |
||
21 | ]; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | public $hook; |
||
27 | |||
28 | /** |
||
29 | * @return string |
||
30 | */ |
||
31 | public static function id() |
||
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | public function init() |
||
54 | |||
55 | public function canProceed() |
||
60 | |||
61 | /** |
||
62 | * @return void |
||
63 | * @action pollux/{static::ID}/init |
||
64 | */ |
||
65 | public function addSubmitMetaBox() |
||
76 | |||
77 | /** |
||
78 | * @return string |
||
79 | * @filter pollux/{static::ID}/before/instructions |
||
80 | */ |
||
81 | public function filterBeforeInstructions() |
||
85 | |||
86 | /** |
||
87 | * @param string $instruction |
||
88 | * @return string |
||
89 | * @action pollux/{static::ID}/instruction |
||
90 | */ |
||
91 | public function filterInstruction( $instruction, array $field, array $metabox ) |
||
95 | |||
96 | /** |
||
97 | * @param null|array $settings |
||
98 | * @return array |
||
99 | * @callback register_setting |
||
100 | */ |
||
101 | public function filterSavedSettings( $settings ) |
||
108 | |||
109 | /** |
||
110 | * @param string $key |
||
111 | * @param mixed $fallback |
||
112 | * @param string $group |
||
113 | * @return string|array |
||
114 | */ |
||
115 | public function getMetaValue( $key, $fallback = '', $group = '' ) |
||
119 | |||
120 | /** |
||
121 | * @return void |
||
122 | * @action current_screen |
||
123 | */ |
||
124 | public function register() |
||
138 | |||
139 | /** |
||
140 | * @return void |
||
141 | * @action admin_menu |
||
142 | */ |
||
143 | public function registerPage() |
||
155 | |||
156 | /** |
||
157 | * @return void |
||
158 | * @action admin_menu |
||
159 | */ |
||
160 | public function registerSetting() |
||
164 | |||
165 | /** |
||
166 | * @return void |
||
167 | * @action admin_print_footer_scripts |
||
168 | */ |
||
169 | public function renderFooterScript() |
||
178 | |||
179 | /** |
||
180 | * @return void |
||
181 | * @callback add_menu_page |
||
182 | */ |
||
183 | public function renderPage() |
||
191 | |||
192 | /** |
||
193 | * @return void |
||
194 | * @callback add_meta_box |
||
195 | */ |
||
196 | public function renderSubmitMetaBox() |
||
210 | |||
211 | /** |
||
212 | * @return void |
||
213 | * @action pollux/{static::ID}/init |
||
214 | */ |
||
215 | View Code Duplication | public function resetPage() |
|
231 | |||
232 | /** |
||
233 | * @param string $key |
||
234 | * @return array |
||
235 | */ |
||
236 | protected function filterArrayByKey( array $array, $key ) |
||
242 | |||
243 | /** |
||
244 | * @return array |
||
245 | */ |
||
246 | protected function getDefaults() |
||
261 | |||
262 | protected function getSettings() |
||
266 | |||
267 | /** |
||
268 | * @param string $name |
||
269 | * @param string $parentId |
||
270 | * @return string |
||
271 | */ |
||
272 | protected function normalizeFieldName( $name, array $data, $parentId ) |
||
276 | |||
277 | /** |
||
278 | * @param string $id |
||
279 | * @param string $parentId |
||
280 | * @return string |
||
281 | */ |
||
282 | protected function normalizeId( $id, array $data, $parentId ) |
||
288 | } |
||
289 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.