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 PodsField_Number 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 PodsField_Number, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class PodsField_Number extends PodsField { |
||
6 | |||
7 | /** |
||
8 | * Field Type Group |
||
9 | * |
||
10 | * @var string |
||
11 | * @since 2.0 |
||
12 | */ |
||
13 | public static $group = 'Number'; |
||
14 | |||
15 | /** |
||
16 | * Field Type Identifier |
||
17 | * |
||
18 | * @var string |
||
19 | * @since 2.0 |
||
20 | */ |
||
21 | public static $type = 'number'; |
||
22 | |||
23 | /** |
||
24 | * Field Type Label |
||
25 | * |
||
26 | * @var string |
||
27 | * @since 2.0 |
||
28 | */ |
||
29 | public static $label = 'Plain Number'; |
||
30 | |||
31 | /** |
||
32 | * Field Type Preparation |
||
33 | * |
||
34 | * @var string |
||
35 | * @since 2.0 |
||
36 | */ |
||
37 | public static $prepare = '%d'; |
||
38 | |||
39 | /** |
||
40 | * Do things like register/enqueue scripts and stylesheets |
||
41 | * |
||
42 | * @since 2.0 |
||
43 | */ |
||
44 | public function __construct () { |
||
47 | |||
48 | /** |
||
49 | * Add options and set defaults to |
||
50 | * |
||
51 | * @return array |
||
52 | * |
||
53 | * @since 2.0 |
||
54 | */ |
||
55 | public function options () { |
||
139 | |||
140 | /** |
||
141 | * Define the current field's schema for DB table storage |
||
142 | * |
||
143 | * @param array $options |
||
144 | * |
||
145 | * @return array |
||
146 | * @since 2.0 |
||
147 | */ |
||
148 | View Code Duplication | public function schema ( $options = null ) { |
|
168 | |||
169 | /** |
||
170 | * Define the current field's preparation for sprintf |
||
171 | * |
||
172 | * @param array $options |
||
173 | * |
||
174 | * @return array |
||
175 | * @since 2.0 |
||
176 | */ |
||
177 | View Code Duplication | public function prepare ( $options = null ) { |
|
202 | |||
203 | /** |
||
204 | * Change the way the value of the field is displayed with Pods::get |
||
205 | * |
||
206 | * @param mixed $value |
||
207 | * @param string $name |
||
208 | * @param array $options |
||
209 | * @param array $pod |
||
210 | * @param int $id |
||
211 | * |
||
212 | * @return mixed|null|string |
||
213 | * @since 2.0 |
||
214 | */ |
||
215 | public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
220 | |||
221 | /** |
||
222 | * Customize output of the form field |
||
223 | * |
||
224 | * @param string $name |
||
225 | * @param mixed $value |
||
226 | * @param array $options |
||
227 | * @param array $pod |
||
228 | * @param int $id |
||
229 | * |
||
230 | * @since 2.0 |
||
231 | */ |
||
232 | View Code Duplication | public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { |
|
265 | |||
266 | /** |
||
267 | * Build regex necessary for JS validation |
||
268 | * |
||
269 | * @param mixed $value |
||
270 | * @param string $name |
||
271 | * @param array $options |
||
272 | * @param string $pod |
||
273 | * @param int $id |
||
274 | * |
||
275 | * @return bool|string |
||
276 | * @since 2.0 |
||
277 | */ |
||
278 | public function regex ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
308 | |||
309 | /** |
||
310 | * Validate a value before it's saved |
||
311 | * |
||
312 | * @param mixed $value |
||
313 | * @param string $name |
||
314 | * @param array $options |
||
315 | * @param array $fields |
||
316 | * @param array $pod |
||
317 | * @param int $id |
||
318 | * @param null $params |
||
319 | * |
||
320 | * @return bool|mixed |
||
321 | * @since 2.0 |
||
322 | */ |
||
323 | public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
||
367 | |||
368 | /** |
||
369 | * Change the value or perform actions after validation but before saving to the DB |
||
370 | * |
||
371 | * @param mixed $value |
||
372 | * @param int $id |
||
373 | * @param string $name |
||
374 | * @param array $options |
||
375 | * @param array $fields |
||
376 | * @param array $pod |
||
377 | * @param object $params |
||
378 | * |
||
379 | * @return mixed|string |
||
380 | * @since 2.0 |
||
381 | */ |
||
382 | public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
434 | |||
435 | /** |
||
436 | * Customize the Pods UI manage table column output |
||
437 | * |
||
438 | * @param int $id |
||
439 | * @param mixed $value |
||
440 | * @param string $name |
||
441 | * @param array $options |
||
442 | * @param array $fields |
||
443 | * @param array $pod |
||
444 | * |
||
445 | * @return mixed|null|string |
||
446 | * @since 2.0 |
||
447 | */ |
||
448 | public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
||
451 | |||
452 | /** |
||
453 | * Reformat a number to the way the value of the field is displayed |
||
454 | * |
||
455 | * @param mixed $value |
||
456 | * @param string $name |
||
457 | * @param array $options |
||
458 | * @param array $pod |
||
459 | * @param int $id |
||
460 | * |
||
461 | * @return string |
||
462 | * @since 2.0 |
||
463 | */ |
||
464 | public function format ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
529 | } |
||
530 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.