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_Link 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_Link, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class PodsField_Link 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 = 'link'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Field Type Label |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | * @since 2.0 |
||
| 28 | */ |
||
| 29 | public static $label = 'Link'; |
||
| 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 () { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Define the current field's schema for DB table storage |
||
| 130 | * |
||
| 131 | * @param array $options |
||
| 132 | * |
||
| 133 | * @return array |
||
| 134 | * @since 2.0 |
||
| 135 | */ |
||
| 136 | public function schema ( $options = null ) { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Change the value of the field |
||
| 143 | * |
||
| 144 | * @param mixed $value |
||
| 145 | * @param string $name |
||
| 146 | * @param array $options |
||
| 147 | * @param array $pod |
||
| 148 | * @param int $id |
||
| 149 | * |
||
| 150 | * @return mixed|null|string |
||
| 151 | * @since 2.3 |
||
| 152 | */ |
||
| 153 | public function value ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Change the way the value of the field is displayed with Pods::get |
||
| 159 | * |
||
| 160 | * @param mixed $value |
||
| 161 | * @param string $name |
||
| 162 | * @param array $options |
||
| 163 | * @param array $pod |
||
| 164 | * @param int $id |
||
| 165 | * |
||
| 166 | * @return mixed|null|string |
||
| 167 | * @since 2.0 |
||
| 168 | */ |
||
| 169 | public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Customize output of the form field |
||
| 218 | * |
||
| 219 | * @param string $name |
||
| 220 | * @param mixed $value |
||
| 221 | * @param array $options |
||
| 222 | * @param array $pod |
||
| 223 | * @param int $id |
||
| 224 | * |
||
| 225 | * @since 2.0 |
||
| 226 | */ |
||
| 227 | public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Validate a value before it's saved |
||
| 236 | * |
||
| 237 | * @param mixed $value |
||
| 238 | * @param string $name |
||
| 239 | * @param array $options |
||
| 240 | * @param array $fields |
||
| 241 | * @param array $pod |
||
| 242 | * @param int $id |
||
| 243 | * |
||
| 244 | * @since 2.0 |
||
| 245 | */ |
||
| 246 | public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Change the value or perform actions after validation but before saving to the DB |
||
| 272 | * |
||
| 273 | * @param mixed $value |
||
| 274 | * @param int $id |
||
| 275 | * @param string $name |
||
| 276 | * @param array $options |
||
| 277 | * @param array $fields |
||
| 278 | * @param array $pod |
||
| 279 | * @param object $params |
||
| 280 | * |
||
| 281 | * @since 2.0 |
||
| 282 | */ |
||
| 283 | public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Customize the Pods UI manage table column output |
||
| 306 | * |
||
| 307 | * @param int $id |
||
| 308 | * @param mixed $value |
||
| 309 | * @param string $name |
||
| 310 | * @param array $options |
||
| 311 | * @param array $fields |
||
| 312 | * @param array $pod |
||
| 313 | * |
||
| 314 | * @since 2.0 |
||
| 315 | */ |
||
| 316 | public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Validate an URL with the options |
||
| 325 | * |
||
| 326 | * @param string $value |
||
| 327 | * @param array $options |
||
| 328 | * |
||
| 329 | * @since 2.0 |
||
| 330 | */ |
||
| 331 | public function validate_url( $value, $options = null ) { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Validate an targit attribute with the options |
||
| 415 | * |
||
| 416 | * @param string $value |
||
| 417 | * @param array $options |
||
| 418 | * |
||
| 419 | * @since 2.0 |
||
| 420 | */ |
||
| 421 | public function validate_target( $value, $options = null ) { |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Build an url |
||
| 432 | * |
||
| 433 | * @param array|string $url |
||
| 434 | * |
||
| 435 | * @return string |
||
| 436 | */ |
||
| 437 | View Code Duplication | public function build_url ( $url ) { |
|
| 461 | |||
| 462 | |||
| 463 | /** |
||
| 464 | * Strip HTML based on options |
||
| 465 | * |
||
| 466 | * @param string $value |
||
| 467 | * @param array $options |
||
| 468 | * |
||
| 469 | * @return string |
||
| 470 | */ |
||
| 471 | View Code Duplication | public function strip_html ( $value, $options = null ) { |
|
| 498 | |||
| 499 | } |
||
| 500 |
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.