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 () { |
||
| 56 | $options = array( |
||
| 57 | self::$type . '_repeatable' => array( |
||
| 58 | 'label' => __( 'Repeatable Field', 'pods' ), |
||
| 59 | 'default' => 0, |
||
| 60 | 'type' => 'boolean', |
||
| 61 | 'help' => __( 'Making a field repeatable will add controls next to the field which allows users to Add/Remove/Reorder additional values. These values are saved in the database as an array, so searching and filtering by them may require further adjustments".', 'pods' ), |
||
| 62 | 'boolean_yes_label' => '', |
||
| 63 | 'dependency' => true, |
||
| 64 | 'developer_mode' => true |
||
| 65 | ), |
||
| 66 | self::$type . '_format' => array( |
||
| 67 | 'label' => __( 'Format', 'pods' ), |
||
| 68 | 'default' => 'normal', |
||
| 69 | 'type' => 'pick', |
||
| 70 | 'data' => array( |
||
| 71 | 'normal' => __( 'http://example.com/', 'pods' ), |
||
| 72 | 'no-www' => __( 'http://example.com/ (remove www)', 'pods' ), |
||
| 73 | 'force-www' => __( 'http://www.example.com/ (force www if no sub-domain provided)', 'pods' ), |
||
| 74 | 'no-http' => __( 'example.com', 'pods' ), |
||
| 75 | 'no-http-no-www' => __( 'example.com (force removal of www)', 'pods' ), |
||
| 76 | 'no-http-force-www' => __( 'www.example.com (force www if no sub-domain provided)', 'pods' ) |
||
| 77 | ) |
||
| 78 | ), |
||
| 79 | self::$type . '_clickable' => array( |
||
| 80 | 'label' => __( 'Output as a link?', 'pods' ), |
||
| 81 | 'default' => apply_filters( 'pods_form_ui_field_website_clickable', 0, self::$type ), |
||
| 82 | 'type' => 'boolean', |
||
| 83 | 'dependency' => true, |
||
| 84 | ), |
||
| 85 | self::$type . '_new_window' => array( |
||
| 86 | 'label' => __( 'Open link in new window?', 'pods' ), |
||
| 87 | 'default' => apply_filters( 'pods_form_ui_field_website_new_window', 0, self::$type ), |
||
| 88 | 'type' => 'boolean', |
||
| 89 | 'depends-on' => array( self::$type . '_clickable' => true ), |
||
| 90 | ), |
||
| 91 | self::$type . '_max_length' => array( |
||
| 92 | 'label' => __( 'Maximum Length', 'pods' ), |
||
| 93 | 'default' => 255, |
||
| 94 | 'type' => 'number', |
||
| 95 | 'help' => __( 'Set to -1 for no limit', 'pods' ) |
||
| 96 | ), |
||
| 97 | self::$type . '_html5' => array( |
||
| 98 | 'label' => __( 'Enable HTML5 Input Field?', 'pods' ), |
||
| 99 | 'default' => apply_filters( 'pods_form_ui_field_html5', 0, self::$type ), |
||
| 100 | 'type' => 'boolean' |
||
| 101 | )/*, |
||
| 102 | self::$type . '_size' => array( |
||
| 103 | 'label' => __( 'Field Size', 'pods' ), |
||
| 104 | 'default' => 'medium', |
||
| 105 | 'type' => 'pick', |
||
| 106 | 'data' => array( |
||
| 107 | 'small' => __( 'Small', 'pods' ), |
||
| 108 | 'medium' => __( 'Medium', 'pods' ), |
||
| 109 | 'large' => __( 'Large', 'pods' ) |
||
| 110 | ) |
||
| 111 | )*/ |
||
| 112 | ); |
||
| 113 | return $options; |
||
| 114 | } |
||
| 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 ) { |
||
| 148 | // Ensure proper format |
||
| 149 | $value = $this->pre_save( $value, $id, $name, $options, null, $pod ); |
||
| 150 | |||
| 151 | if ( 1 == pods_v( self::$type . '_clickable', $options ) && 0 < strlen( $value ) ) { |
||
| 152 | $link = '<a href="%s"%s>%s</a>'; |
||
| 153 | |||
| 154 | $atts = ''; |
||
| 155 | |||
| 156 | if ( 1 == pods_v( self::$type . '_new_window', $options ) ) { |
||
| 157 | $atts .= ' target="_blank"'; |
||
| 158 | } |
||
| 159 | |||
| 160 | $value = sprintf( $link, esc_url( $value ), $atts, esc_html( $value ) ); |
||
| 161 | } |
||
| 162 | |||
| 163 | return $value; |
||
| 164 | } |
||
| 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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.