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:
Complex classes like MetaBox often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MetaBox, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class MetaBox extends Component |
||
11 | { |
||
12 | const CONDITIONS = [ |
||
13 | 'class_exists', 'defined', 'function_exists', 'hook', 'is_front_page', 'is_home', |
||
14 | 'is_page_template', 'is_plugin_active', 'is_plugin_inactive', |
||
15 | ]; |
||
16 | |||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | public $metaboxes = []; |
||
21 | |||
22 | /** |
||
23 | * {@inheritdoc} |
||
24 | */ |
||
25 | public function init() |
||
32 | |||
33 | /** |
||
34 | * @return bool |
||
35 | * @filter rwmb_show |
||
36 | */ |
||
37 | public function isVisible( $bool, array $metabox ) |
||
46 | |||
47 | /** |
||
48 | * @return array |
||
49 | * @filter rwmb_meta_boxes |
||
50 | */ |
||
51 | public function register() |
||
61 | |||
62 | /** |
||
63 | * @return bool |
||
64 | */ |
||
65 | public function verifyMetaBoxCondition( array $conditions ) |
||
75 | |||
76 | /** |
||
77 | * @return void |
||
78 | */ |
||
79 | protected function addInstructions() |
||
97 | |||
98 | /** |
||
99 | * @return string |
||
100 | */ |
||
101 | protected function generateInstructions() |
||
114 | |||
115 | /** |
||
116 | * @return array |
||
117 | */ |
||
118 | protected function getInstructions() |
||
125 | |||
126 | /** |
||
127 | * @return int |
||
128 | */ |
||
129 | protected function getPostId() |
||
136 | |||
137 | /** |
||
138 | * @return array |
||
139 | */ |
||
140 | protected function getPostTypes() |
||
149 | |||
150 | /** |
||
151 | * @return bool |
||
152 | */ |
||
153 | protected function hasPostType( array $metabox ) |
||
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | View Code Duplication | protected function normalize() |
|
179 | |||
180 | /** |
||
181 | * @param mixed $conditions |
||
182 | * @return array |
||
183 | */ |
||
184 | protected function normalizeCondition( $conditions ) |
||
201 | |||
202 | /** |
||
203 | * @param string $id |
||
|
|||
204 | * @param string $parentId |
||
205 | * @return string |
||
206 | */ |
||
207 | protected function normalizeDepends( $depends, array $data, $parentId ) |
||
213 | |||
214 | /** |
||
215 | * @return array |
||
216 | */ |
||
217 | protected function normalizeFields( array $fields, array $data, $parentId ) |
||
231 | |||
232 | /** |
||
233 | * @param string $id |
||
234 | * @param string $parentId |
||
235 | * @return string |
||
236 | */ |
||
237 | protected function normalizeId( $id, array $data, $parentId ) |
||
241 | |||
242 | /** |
||
243 | * @param mixed $types |
||
244 | * @return array |
||
245 | */ |
||
246 | protected function normalizePostTypes( $types ) |
||
250 | |||
251 | /** |
||
252 | * @return array |
||
253 | */ |
||
254 | protected function setDependencies( array $metabox ) |
||
264 | |||
265 | /** |
||
266 | * @param string $value |
||
267 | * @return bool |
||
268 | */ |
||
269 | protected function validateClassExists( $value ) |
||
273 | |||
274 | /** |
||
275 | * @param string $value |
||
276 | * @return bool |
||
277 | */ |
||
278 | protected function validateDefined( $value ) |
||
282 | |||
283 | /** |
||
284 | * @param string $value |
||
285 | * @return bool |
||
286 | */ |
||
287 | protected function validateFunctionExists( $value ) |
||
291 | |||
292 | /** |
||
293 | * @param string $value |
||
294 | * @return bool |
||
295 | */ |
||
296 | protected function validateHook( $value ) |
||
300 | |||
301 | /** |
||
302 | * @param bool $value |
||
303 | * @return bool |
||
304 | */ |
||
305 | protected function validateIsFrontPage( $value ) |
||
309 | |||
310 | /** |
||
311 | * @param bool $value |
||
312 | * @return bool |
||
313 | */ |
||
314 | protected function validateIsHome( $value ) |
||
318 | |||
319 | /** |
||
320 | * @param string $value |
||
321 | * @return bool |
||
322 | */ |
||
323 | protected function validateIsPageTemplate( $value ) |
||
327 | |||
328 | /** |
||
329 | * @param string $value |
||
330 | * @return bool |
||
331 | */ |
||
332 | protected function validateIsPluginActive( $value ) |
||
336 | |||
337 | /** |
||
338 | * @param string $value |
||
339 | * @return bool |
||
340 | */ |
||
341 | protected function validateIsPluginInactive( $value ) |
||
345 | |||
346 | /** |
||
347 | * @param string $key |
||
348 | * @param mixed $value |
||
349 | * @return bool |
||
350 | */ |
||
351 | protected function validateUnknown( $key, $value ) |
||
355 | } |
||
356 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.