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 () { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Add options and set defaults to |
||
| 52 | * |
||
| 53 | * @param array $options |
||
|
|
|||
| 54 | * |
||
| 55 | * @since 2.0 |
||
| 56 | */ |
||
| 57 | public function options () { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Define the current field's schema for DB table storage |
||
| 120 | * |
||
| 121 | * @param array $options |
||
| 122 | * |
||
| 123 | * @return array |
||
| 124 | * @since 2.0 |
||
| 125 | */ |
||
| 126 | View Code Duplication | public function schema ( $options = null ) { |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Change the way the value of the field is displayed with Pods::get |
||
| 139 | * |
||
| 140 | * @param mixed $value |
||
| 141 | * @param string $name |
||
| 142 | * @param array $options |
||
| 143 | * @param array $pod |
||
| 144 | * @param int $id |
||
| 145 | * |
||
| 146 | * @return mixed|null |
||
| 147 | * @since 2.0 |
||
| 148 | */ |
||
| 149 | public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Customize output of the form field |
||
| 170 | * |
||
| 171 | * @param string $name |
||
| 172 | * @param mixed $value |
||
| 173 | * @param array $options |
||
| 174 | * @param array $pod |
||
| 175 | * @param int $id |
||
| 176 | * |
||
| 177 | * @since 2.0 |
||
| 178 | */ |
||
| 179 | View Code Duplication | public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Validate a value before it's saved |
||
| 208 | * |
||
| 209 | * @param mixed $value |
||
| 210 | * @param string $name |
||
| 211 | * @param array $options |
||
| 212 | * @param array $fields |
||
| 213 | * @param array $pod |
||
| 214 | * @param int $id |
||
| 215 | * |
||
| 216 | * @since 2.0 |
||
| 217 | */ |
||
| 218 | View Code Duplication | public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Change the value or perform actions after validation but before saving to the DB |
||
| 244 | * |
||
| 245 | * @param mixed $value |
||
| 246 | * @param int $id |
||
| 247 | * @param string $name |
||
| 248 | * @param array $options |
||
| 249 | * @param array $fields |
||
| 250 | * @param array $pod |
||
| 251 | * @param object $params |
||
| 252 | * |
||
| 253 | * @since 2.0 |
||
| 254 | */ |
||
| 255 | View Code Duplication | public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Customize the Pods UI manage table column output |
||
| 271 | * |
||
| 272 | * @param int $id |
||
| 273 | * @param mixed $value |
||
| 274 | * @param string $name |
||
| 275 | * @param array $options |
||
| 276 | * @param array $fields |
||
| 277 | * @param array $pod |
||
| 278 | * |
||
| 279 | * @since 2.0 |
||
| 280 | */ |
||
| 281 | public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Validate an URL with the options |
||
| 289 | * |
||
| 290 | * @param string $value |
||
| 291 | * @param array $options |
||
| 292 | * |
||
| 293 | * @since 2.7 |
||
| 294 | */ |
||
| 295 | public function validate_url( $value, $options = null ) { |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Strip HTML based on options |
||
| 378 | * |
||
| 379 | * @param string $value |
||
| 380 | * @param array $options |
||
| 381 | * |
||
| 382 | * @return string |
||
| 383 | * |
||
| 384 | * @since 2.7 |
||
| 385 | */ |
||
| 386 | View Code Duplication | public function strip_html ( $value, $options = null ) { |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Validate an target attribute with the options |
||
| 416 | * |
||
| 417 | * @param string $value |
||
| 418 | * @param array $options |
||
| 419 | * |
||
| 420 | * @since 2.7 |
||
| 421 | */ |
||
| 422 | public function validate_target( $value, $options = null ) { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Build an url |
||
| 433 | * |
||
| 434 | * @param array|string $url |
||
| 435 | * |
||
| 436 | * @return string |
||
| 437 | */ |
||
| 438 | public function build_url ( $url ) { |
||
| 462 | |||
| 463 | } |
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.