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 () { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Define the current field's schema for DB table storage |
||
| 135 | * |
||
| 136 | * @param array $options |
||
| 137 | * |
||
| 138 | * @return array |
||
| 139 | * @since 2.0 |
||
| 140 | */ |
||
| 141 | View Code Duplication | public function schema ( $options = null ) { |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Change the way the value of the field is displayed with Pods::get |
||
| 154 | * |
||
| 155 | * @param mixed $value |
||
| 156 | * @param string $name |
||
| 157 | * @param array $options |
||
| 158 | * @param array $pod |
||
| 159 | * @param int $id |
||
| 160 | * |
||
| 161 | * @return mixed|null |
||
| 162 | * @since 2.0 |
||
| 163 | */ |
||
| 164 | public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Customize output of the form field |
||
| 185 | * |
||
| 186 | * @param string $name |
||
| 187 | * @param mixed $value |
||
| 188 | * @param array $options |
||
| 189 | * @param array $pod |
||
| 190 | * @param int $id |
||
| 191 | * |
||
| 192 | * @since 2.0 |
||
| 193 | */ |
||
| 194 | public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Validate a value before it's saved |
||
| 223 | * |
||
| 224 | * @param mixed $value |
||
| 225 | * @param string $name |
||
| 226 | * @param array $options |
||
| 227 | * @param array $fields |
||
| 228 | * @param array $pod |
||
| 229 | * @param int $id |
||
| 230 | * |
||
| 231 | * @return bool|array |
||
| 232 | * |
||
| 233 | * @since 2.0 |
||
| 234 | */ |
||
| 235 | View Code Duplication | public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Change the value or perform actions after validation but before saving to the DB |
||
| 261 | * |
||
| 262 | * @param mixed $value |
||
| 263 | * @param int $id |
||
| 264 | * @param string $name |
||
| 265 | * @param array $options |
||
| 266 | * @param array $fields |
||
| 267 | * @param array $pod |
||
| 268 | * @param object $params |
||
| 269 | * |
||
| 270 | * @return string |
||
| 271 | * |
||
| 272 | * @since 2.0 |
||
| 273 | */ |
||
| 274 | public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Customize the Pods UI manage table column output |
||
| 299 | * |
||
| 300 | * @param int $id |
||
| 301 | * @param mixed $value |
||
| 302 | * @param string $name |
||
| 303 | * @param array $options |
||
| 304 | * @param array $fields |
||
| 305 | * @param array $pod |
||
| 306 | * |
||
| 307 | * @return string |
||
| 308 | * |
||
| 309 | * @since 2.0 |
||
| 310 | */ |
||
| 311 | public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Validate an URL with the options |
||
| 319 | * |
||
| 320 | * @param string $value |
||
| 321 | * @param array $options |
||
| 322 | * |
||
| 323 | * @return string |
||
| 324 | * |
||
| 325 | * @since 2.7 |
||
| 326 | */ |
||
| 327 | public function validate_url( $value, $options = null ) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Strip HTML based on options |
||
| 411 | * |
||
| 412 | * @param string $value |
||
| 413 | * @param array $options |
||
| 414 | * |
||
| 415 | * @return string |
||
| 416 | * |
||
| 417 | * @since 2.7 |
||
| 418 | */ |
||
| 419 | View Code Duplication | public function strip_html ( $value, $options = null ) { |
|
| 446 | |||
| 447 | /** |
||
| 448 | * Validate an target attribute with the options |
||
| 449 | * |
||
| 450 | * @param string $value |
||
| 451 | * |
||
| 452 | * @return string |
||
| 453 | * |
||
| 454 | * @since 2.7 |
||
| 455 | */ |
||
| 456 | public function validate_target( $value ) { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Build a url from url parts |
||
| 467 | * |
||
| 468 | * @param array|string $url |
||
| 469 | * @param array $options |
||
| 470 | * |
||
| 471 | * @return string |
||
| 472 | */ |
||
| 473 | public function build_url( $url, $options = array() ) { |
||
| 524 | |||
| 525 | } |
||
| 526 |
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.