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_File 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_File, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class PodsField_File extends PodsField { |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Field Type Group |
||
| 9 | * |
||
| 10 | * @var string |
||
| 11 | * @since 2.0 |
||
| 12 | */ |
||
| 13 | public static $group = 'Relationships / Media'; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Field Type Identifier |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | * @since 2.0 |
||
| 20 | */ |
||
| 21 | public static $type = 'file'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Field Type Label |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | * @since 2.0 |
||
| 28 | */ |
||
| 29 | public static $label = 'File / Image / Video'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * API caching for fields that need it during validate/save |
||
| 33 | * |
||
| 34 | * @var \PodsAPI |
||
| 35 | * @since 2.3 |
||
| 36 | */ |
||
| 37 | protected static $api = false; |
||
| 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 admin_init actions |
||
| 50 | * |
||
| 51 | * @since 2.3 |
||
| 52 | */ |
||
| 53 | public function admin_init() { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Add options and set defaults to |
||
| 61 | * |
||
| 62 | * @param array $options |
||
|
|
|||
| 63 | * |
||
| 64 | * @since 2.0 |
||
| 65 | */ |
||
| 66 | public function options () { |
||
| 67 | $sizes = get_intermediate_image_sizes(); |
||
| 68 | |||
| 69 | $image_sizes = array(); |
||
| 70 | |||
| 71 | foreach ( $sizes as $size ) { |
||
| 72 | $image_sizes[ $size ] = ucwords( str_replace( '-', ' ', $size ) ); |
||
| 73 | } |
||
| 74 | |||
| 75 | $options = array( |
||
| 76 | self::$type . '_format_type' => array( |
||
| 77 | 'label' => __( 'Upload Limit', 'pods' ), |
||
| 78 | 'default' => 'single', |
||
| 79 | 'type' => 'pick', |
||
| 80 | 'data' => array( |
||
| 81 | 'single' => __( 'Single File', 'pods' ), |
||
| 82 | 'multi' => __( 'Multiple Files', 'pods' ) |
||
| 83 | ), |
||
| 84 | 'dependency' => true |
||
| 85 | ), |
||
| 86 | self::$type . '_uploader' => array( |
||
| 87 | 'label' => __( 'File Uploader', 'pods' ), |
||
| 88 | 'default' => 'attachment', |
||
| 89 | 'type' => 'pick', |
||
| 90 | 'data' => apply_filters( |
||
| 91 | 'pods_form_ui_field_' . self::$type . '_uploader_options', |
||
| 92 | array( |
||
| 93 | 'attachment' => __( 'Attachments (WP Media Library)', 'pods' ), |
||
| 94 | 'plupload' => __( 'Plupload', 'pods' ) |
||
| 95 | ) |
||
| 96 | ), |
||
| 97 | 'dependency' => true |
||
| 98 | ), |
||
| 99 | self::$type . '_attachment_tab' => array( |
||
| 100 | 'label' => __( 'Attachments Default Tab', 'pods' ), |
||
| 101 | 'depends-on' => array( self::$type . '_uploader' => 'attachment' ), |
||
| 102 | 'default' => 'upload', |
||
| 103 | 'type' => 'pick', |
||
| 104 | 'data' => array( |
||
| 105 | // keys MUST match WP's router names |
||
| 106 | 'upload' => __( 'Upload File', 'pods' ), |
||
| 107 | 'browse' => __( 'Media Library', 'pods' ) |
||
| 108 | ) |
||
| 109 | ), |
||
| 110 | self::$type . '_edit_title' => array( |
||
| 111 | 'label' => __( 'Editable Title', 'pods' ), |
||
| 112 | 'default' => 1, |
||
| 113 | 'type' => 'boolean' |
||
| 114 | ), |
||
| 115 | self::$type . '_show_edit_link' => array( |
||
| 116 | 'label' => __( 'Show Edit Link', 'pods' ), |
||
| 117 | 'default' => 0, |
||
| 118 | 'type' => 'boolean' |
||
| 119 | ), |
||
| 120 | self::$type . '_linked' => array( |
||
| 121 | 'label' => __( 'Show Download Link', 'pods' ), |
||
| 122 | 'default' => 0, |
||
| 123 | 'type' => 'boolean' |
||
| 124 | ), |
||
| 125 | self::$type . '_limit' => array( |
||
| 126 | 'label' => __( 'Max Number of Files', 'pods' ), |
||
| 127 | 'depends-on' => array( self::$type . '_format_type' => 'multi' ), |
||
| 128 | 'default' => 0, |
||
| 129 | 'type' => 'number' |
||
| 130 | ), |
||
| 131 | self::$type . '_restrict_filesize' => array( |
||
| 132 | 'label' => __( 'Restrict File Size', 'pods' ), |
||
| 133 | 'depends-on' => array( self::$type . '_uploader' => 'plupload' ), |
||
| 134 | 'default' => '10MB', |
||
| 135 | 'type' => 'text' |
||
| 136 | ), |
||
| 137 | self::$type . '_type' => array( |
||
| 138 | 'label' => __( 'Restrict File Types', 'pods' ), |
||
| 139 | 'default' => apply_filters( 'pods_form_ui_field_' . self::$type . '_type_default', 'images' ), |
||
| 140 | 'type' => 'pick', |
||
| 141 | 'data' => apply_filters( |
||
| 142 | 'pods_form_ui_field_' . self::$type . '_type_options', |
||
| 143 | array( |
||
| 144 | 'images' => __( 'Images (jpg, jpeg, png, gif)', 'pods' ), |
||
| 145 | 'video' => __( 'Video (mpg, mov, flv, mp4, etc..)', 'pods' ), |
||
| 146 | 'audio' => __( 'Audio (mp3, m4a, wav, wma, etc..)', 'pods' ), |
||
| 147 | 'text' => __( 'Text (txt, csv, tsv, rtx, etc..)', 'pods' ), |
||
| 148 | 'any' => __( 'Any Type (no restriction)', 'pods' ), |
||
| 149 | 'other' => __( 'Other (customize allowed extensions)', 'pods' ) |
||
| 150 | ) |
||
| 151 | ), |
||
| 152 | 'dependency' => true |
||
| 153 | ), |
||
| 154 | self::$type . '_allowed_extensions' => array( |
||
| 155 | 'label' => __( 'Allowed File Extensions', 'pods' ), |
||
| 156 | 'description' => __( 'Separate file extensions with a comma (ex. jpg,png,mp4,mov)', 'pods' ), |
||
| 157 | 'depends-on' => array( self::$type . '_type' => 'other' ), |
||
| 158 | 'default' => apply_filters( 'pods_form_ui_field_' . self::$type . '_extensions_default', '' ), |
||
| 159 | 'type' => 'text' |
||
| 160 | ), |
||
| 161 | self::$type . '_field_template' => array( |
||
| 162 | 'label' => __( 'List Style', 'pods' ), |
||
| 163 | 'help' => __( 'You can choose which style you would like the files to appear within the form.', 'pods' ), |
||
| 164 | 'depends-on' => array( self::$type . '_type' => 'images' ), |
||
| 165 | 'default' => apply_filters( 'pods_form_ui_field_' . self::$type . '_template_default', 'rows' ), |
||
| 166 | 'type' => 'pick', |
||
| 167 | 'data' => apply_filters( |
||
| 168 | 'pods_form_ui_field_' . self::$type . '_type_templates', |
||
| 169 | array( |
||
| 170 | 'rows' => __( 'Rows', 'pods' ), |
||
| 171 | 'tiles' => __( 'Tiles', 'pods' ), |
||
| 172 | ) |
||
| 173 | ), |
||
| 174 | ),/* |
||
| 175 | self::$type . '_image_size' => array( |
||
| 176 | 'label' => __( 'Excluded Image Sizes', 'pods' ), |
||
| 177 | 'description' => __( 'Image sizes not to generate when processing the image', 'pods' ), |
||
| 178 | 'depends-on' => array( self::$type . '_type' => 'images' ), |
||
| 179 | 'default' => 'images', |
||
| 180 | 'type' => 'pick', |
||
| 181 | 'pick_format_type' => 'multi', |
||
| 182 | 'pick_format_multi' => 'checkbox', |
||
| 183 | 'data' => apply_filters( |
||
| 184 | 'pods_form_ui_field_' . self::$type . '_image_size_options', |
||
| 185 | $image_sizes |
||
| 186 | ) |
||
| 187 | ),*/ |
||
| 188 | self::$type . '_add_button' => array( |
||
| 189 | 'label' => __( 'Add Button Text', 'pods' ), |
||
| 190 | 'default' => __( 'Add File', 'pods' ), |
||
| 191 | 'type' => 'text' |
||
| 192 | ), |
||
| 193 | self::$type . '_modal_title' => array( |
||
| 194 | 'label' => __( 'Modal Title', 'pods' ), |
||
| 195 | 'depends-on' => array( self::$type . '_uploader' => 'attachment' ), |
||
| 196 | 'default' => __( 'Attach a file', 'pods' ), |
||
| 197 | 'type' => 'text' |
||
| 198 | ), |
||
| 199 | self::$type . '_modal_add_button' => array( |
||
| 200 | 'label' => __( 'Modal Add Button Text', 'pods' ), |
||
| 201 | 'depends-on' => array( self::$type . '_uploader' => 'attachment' ), |
||
| 202 | 'default' => __( 'Add File', 'pods' ), |
||
| 203 | 'type' => 'text' |
||
| 204 | ) |
||
| 205 | ); |
||
| 206 | |||
| 207 | View Code Duplication | if ( !pods_version_check( 'wp', '3.5' ) ) { |
|
| 208 | unset( $options[ self::$type . '_linked' ] ); |
||
| 209 | unset( $options[ self::$type . '_modal_title' ] ); |
||
| 210 | unset( $options[ self::$type . '_modal_add_button' ] ); |
||
| 211 | |||
| 212 | $options[ self::$type . '_attachment_tab' ][ 'default' ] = 'type'; |
||
| 213 | $options[ self::$type . '_attachment_tab' ][ 'data' ] = array( |
||
| 214 | 'type' => __( 'Upload File', 'pods' ), |
||
| 215 | 'library' => __( 'Media Library', 'pods' ) |
||
| 216 | ); |
||
| 217 | } |
||
| 218 | |||
| 219 | return $options; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Define the current field's schema for DB table storage |
||
| 224 | * |
||
| 225 | * @param array $options |
||
| 226 | * |
||
| 227 | * @return array |
||
| 228 | * @since 2.0 |
||
| 229 | */ |
||
| 230 | public function schema ( $options = null ) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Change the way the value of the field is displayed with Pods::get |
||
| 238 | * |
||
| 239 | * @param mixed $value |
||
| 240 | * @param string $name |
||
| 241 | * @param array $options |
||
| 242 | * @param array $pod |
||
| 243 | * @param int $id |
||
| 244 | * |
||
| 245 | * @return mixed|null |
||
| 246 | * @since 2.0 |
||
| 247 | */ |
||
| 248 | public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Customize output of the form field |
||
| 272 | * |
||
| 273 | * @param string $name |
||
| 274 | * @param mixed $value |
||
| 275 | * @param array $options |
||
| 276 | * @param array $pod |
||
| 277 | * @param int $id |
||
| 278 | * |
||
| 279 | * @since 2.0 |
||
| 280 | */ |
||
| 281 | public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Build regex necessary for JS validation |
||
| 335 | * |
||
| 336 | * @param mixed $value |
||
| 337 | * @param string $name |
||
| 338 | * @param array $options |
||
| 339 | * @param string $pod |
||
| 340 | * @param int $id |
||
| 341 | * |
||
| 342 | * @return bool |
||
| 343 | * @since 2.0 |
||
| 344 | */ |
||
| 345 | public function regex ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Validate a value before it's saved |
||
| 351 | * |
||
| 352 | * @param mixed $value |
||
| 353 | * @param string $name |
||
| 354 | * @param array $options |
||
| 355 | * @param array $fields |
||
| 356 | * @param array $pod |
||
| 357 | * @param int $id |
||
| 358 | * @param null $params |
||
| 359 | * |
||
| 360 | * @return bool |
||
| 361 | * @since 2.0 |
||
| 362 | */ |
||
| 363 | public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Change the value or perform actions after validation but before saving to the DB |
||
| 371 | * |
||
| 372 | * @param mixed $value |
||
| 373 | * @param int $id |
||
| 374 | * @param string $name |
||
| 375 | * @param array $options |
||
| 376 | * @param array $fields |
||
| 377 | * @param array $pod |
||
| 378 | * @param object $params |
||
| 379 | * |
||
| 380 | * @return mixed |
||
| 381 | * @since 2.0 |
||
| 382 | */ |
||
| 383 | public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Save the value to the DB |
||
| 389 | * |
||
| 390 | * @param mixed $value |
||
| 391 | * @param int $id |
||
| 392 | * @param string $name |
||
| 393 | * @param array $options |
||
| 394 | * @param array $fields |
||
| 395 | * @param array $pod |
||
| 396 | * @param object $params |
||
| 397 | * |
||
| 398 | * @since 2.3 |
||
| 399 | */ |
||
| 400 | public function save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Customize the Pods UI manage table column output |
||
| 435 | * |
||
| 436 | * @param int $id |
||
| 437 | * @param mixed $value |
||
| 438 | * @param string $name |
||
| 439 | * @param array $options |
||
| 440 | * @param array $fields |
||
| 441 | * @param array $pod |
||
| 442 | * |
||
| 443 | * @return mixed|void |
||
| 444 | * @since 2.0 |
||
| 445 | */ |
||
| 446 | public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Return image(s) markup |
||
| 460 | * |
||
| 461 | * @param int $id |
||
| 462 | * @param mixed $value |
||
| 463 | * @param string $name |
||
| 464 | * @param array $options |
||
| 465 | * @param array $pod |
||
| 466 | * @param string $image_size |
||
| 467 | * |
||
| 468 | * @return string |
||
| 469 | * @since 2.3 |
||
| 470 | */ |
||
| 471 | public function images ( $id, $value, $name = null, $options = null, $pod = null, $image_size = null ) { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Handle file row output for uploaders |
||
| 486 | * |
||
| 487 | * @param array $attributes |
||
| 488 | * @param int $limit |
||
| 489 | * @param bool $editable |
||
| 490 | * @param int $id |
||
| 491 | * @param string $icon |
||
| 492 | * @param string $name |
||
| 493 | * |
||
| 494 | * @return string |
||
| 495 | * @since 2.0 |
||
| 496 | */ |
||
| 497 | public function markup ( $attributes, $limit = 1, $editable = true, $id = null, $icon = null, $name = null, $linked = false, $link = null ) { |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Handle plupload AJAX |
||
| 565 | * |
||
| 566 | * @since 2.3 |
||
| 567 | */ |
||
| 568 | public function admin_ajax_upload () { |
||
| 825 | } |
||
| 826 |
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.