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 () { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Define the current field's schema for DB table storage |
||
| 111 | * |
||
| 112 | * @param array $options |
||
| 113 | * |
||
| 114 | * @return array |
||
| 115 | * @since 2.0 |
||
| 116 | */ |
||
| 117 | View Code Duplication | public function schema ( $options = null ) { |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Change the way the value of the field is displayed with Pods::get |
||
| 130 | * |
||
| 131 | * @param mixed $value |
||
| 132 | * @param string $name |
||
| 133 | * @param array $options |
||
| 134 | * @param array $pod |
||
| 135 | * @param int $id |
||
| 136 | * |
||
| 137 | * @return mixed|null |
||
| 138 | * @since 2.0 |
||
| 139 | */ |
||
| 140 | public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Customize output of the form field |
||
| 161 | * |
||
| 162 | * @param string $name |
||
| 163 | * @param mixed $value |
||
| 164 | * @param array $options |
||
| 165 | * @param array $pod |
||
| 166 | * @param int $id |
||
| 167 | * |
||
| 168 | * @since 2.0 |
||
| 169 | */ |
||
| 170 | View Code Duplication | public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Validate a value before it's saved |
||
| 199 | * |
||
| 200 | * @param mixed $value |
||
| 201 | * @param string $name |
||
| 202 | * @param array $options |
||
| 203 | * @param array $fields |
||
| 204 | * @param array $pod |
||
| 205 | * @param int $id |
||
| 206 | * |
||
| 207 | * @since 2.0 |
||
| 208 | */ |
||
| 209 | View Code Duplication | public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Change the value or perform actions after validation but before saving to the DB |
||
| 235 | * |
||
| 236 | * @param mixed $value |
||
| 237 | * @param int $id |
||
| 238 | * @param string $name |
||
| 239 | * @param array $options |
||
| 240 | * @param array $fields |
||
| 241 | * @param array $pod |
||
| 242 | * @param object $params |
||
| 243 | * |
||
| 244 | * @since 2.0 |
||
| 245 | */ |
||
| 246 | public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Customize the Pods UI manage table column output |
||
| 328 | * |
||
| 329 | * @param int $id |
||
| 330 | * @param mixed $value |
||
| 331 | * @param string $name |
||
| 332 | * @param array $options |
||
| 333 | * @param array $fields |
||
| 334 | * @param array $pod |
||
| 335 | * |
||
| 336 | * @since 2.0 |
||
| 337 | */ |
||
| 338 | public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Build an url |
||
| 346 | * |
||
| 347 | * @param array|string $url |
||
| 348 | * |
||
| 349 | * @return string |
||
| 350 | */ |
||
| 351 | public function build_url ( $url ) { |
||
| 375 | } |
||
| 376 |
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.