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_Website 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_Website, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class PodsField_Website extends PodsField { |
||
6 | |||
7 | /** |
||
8 | * Field Type Group |
||
9 | * |
||
10 | * @var string |
||
11 | * @since 2.0 |
||
12 | */ |
||
13 | public static $group = 'Text'; |
||
14 | |||
15 | /** |
||
16 | * Field Type Identifier |
||
17 | * |
||
18 | * @var string |
||
19 | * @since 2.0 |
||
20 | */ |
||
21 | public static $type = 'website'; |
||
22 | |||
23 | /** |
||
24 | * Field Type Label |
||
25 | * |
||
26 | * @var string |
||
27 | * @since 2.0 |
||
28 | */ |
||
29 | public static $label = 'Website'; |
||
30 | |||
31 | /** |
||
32 | * Field Type Preparation |
||
33 | * |
||
34 | * @var string |
||
35 | * @since 2.0 |
||
36 | */ |
||
37 | public static $prepare = '%s'; |
||
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 | * @param array $options |
||
|
|||
52 | * |
||
53 | * @since 2.0 |
||
54 | */ |
||
55 | public function options () { |
||
115 | |||
116 | /** |
||
117 | * Define the current field's schema for DB table storage |
||
118 | * |
||
119 | * @param array $options |
||
120 | * |
||
121 | * @return array |
||
122 | * @since 2.0 |
||
123 | */ |
||
124 | View Code Duplication | public function schema ( $options = null ) { |
|
134 | |||
135 | /** |
||
136 | * Change the way the value of the field is displayed with Pods::get |
||
137 | * |
||
138 | * @param mixed $value |
||
139 | * @param string $name |
||
140 | * @param array $options |
||
141 | * @param array $pod |
||
142 | * @param int $id |
||
143 | * |
||
144 | * @return mixed|null |
||
145 | * @since 2.0 |
||
146 | */ |
||
147 | public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
165 | |||
166 | /** |
||
167 | * Customize output of the form field |
||
168 | * |
||
169 | * @param string $name |
||
170 | * @param mixed $value |
||
171 | * @param array $options |
||
172 | * @param array $pod |
||
173 | * @param int $id |
||
174 | * |
||
175 | * @since 2.0 |
||
176 | */ |
||
177 | View Code Duplication | public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { |
|
203 | |||
204 | /** |
||
205 | * Validate a value before it's saved |
||
206 | * |
||
207 | * @param mixed $value |
||
208 | * @param string $name |
||
209 | * @param array $options |
||
210 | * @param array $fields |
||
211 | * @param array $pod |
||
212 | * @param int $id |
||
213 | * |
||
214 | * @since 2.0 |
||
215 | */ |
||
216 | View Code Duplication | public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
|
239 | |||
240 | /** |
||
241 | * Change the value or perform actions after validation but before saving to the DB |
||
242 | * |
||
243 | * @param mixed $value |
||
244 | * @param int $id |
||
245 | * @param string $name |
||
246 | * @param array $options |
||
247 | * @param array $fields |
||
248 | * @param array $pod |
||
249 | * @param object $params |
||
250 | * |
||
251 | * @since 2.0 |
||
252 | */ |
||
253 | View Code Duplication | public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
|
266 | |||
267 | /** |
||
268 | * Customize the Pods UI manage table column output |
||
269 | * |
||
270 | * @param int $id |
||
271 | * @param mixed $value |
||
272 | * @param string $name |
||
273 | * @param array $options |
||
274 | * @param array $fields |
||
275 | * @param array $pod |
||
276 | * |
||
277 | * @since 2.0 |
||
278 | */ |
||
279 | public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
||
284 | |||
285 | /** |
||
286 | * Validate an URL with the options |
||
287 | * |
||
288 | * @param string $value |
||
289 | * @param array $options |
||
290 | * |
||
291 | * @since 2.7 |
||
292 | */ |
||
293 | public function validate_url( $value, $options = null ) { |
||
373 | |||
374 | /** |
||
375 | * Strip HTML based on options |
||
376 | * |
||
377 | * @param string $value |
||
378 | * @param array $options |
||
379 | * |
||
380 | * @return string |
||
381 | * |
||
382 | * @since 2.7 |
||
383 | */ |
||
384 | View Code Duplication | public function strip_html ( $value, $options = null ) { |
|
411 | |||
412 | /** |
||
413 | * Validate an target attribute with the options |
||
414 | * |
||
415 | * @param string $value |
||
416 | * @param array $options |
||
417 | * |
||
418 | * @since 2.7 |
||
419 | */ |
||
420 | public function validate_target( $value, $options = null ) { |
||
428 | |||
429 | /** |
||
430 | * Build an url |
||
431 | * |
||
432 | * @param array|string $url |
||
433 | * |
||
434 | * @return string |
||
435 | */ |
||
436 | public function build_url ( $url ) { |
||
460 | |||
461 | } |
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.