Complex classes like PodsField 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, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class PodsField { |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Whether this field is running under 1.x deprecated forms |
||
| 12 | * |
||
| 13 | * @var bool |
||
| 14 | * @since 2.0 |
||
| 15 | */ |
||
| 16 | public static $deprecated = false; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Field Type Identifier |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | * @since 2.0 |
||
| 23 | */ |
||
| 24 | public static $type = 'text'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Field Type Label |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | * @since 2.0 |
||
| 31 | */ |
||
| 32 | public static $label = 'Unknown'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Field Type Preparation |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | * @since 2.0 |
||
| 39 | */ |
||
| 40 | public static $prepare = '%s'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Pod Types supported on (true for all, false for none, or give array of specific types supported) |
||
| 44 | * |
||
| 45 | * @var array|bool |
||
| 46 | * @since 2.1 |
||
| 47 | */ |
||
| 48 | public static $pod_types = true; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * API caching for fields that need it during validate/save |
||
| 52 | * |
||
| 53 | * @var \PodsAPI |
||
| 54 | * @since 2.3 |
||
| 55 | */ |
||
| 56 | private static $api; |
||
|
|
|||
| 57 | |||
| 58 | /** |
||
| 59 | * Initial setup of class object. |
||
| 60 | * |
||
| 61 | * @since 2.0 |
||
| 62 | */ |
||
| 63 | public function __construct() { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Do things like register/enqueue scripts+stylesheets, set labels, etc. |
||
| 71 | * |
||
| 72 | * @since 2.7.2 |
||
| 73 | */ |
||
| 74 | public function setup() { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Add admin_init actions. |
||
| 81 | * |
||
| 82 | * @since 2.3 |
||
| 83 | */ |
||
| 84 | public function admin_init() { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Add options and set defaults for field type, shows in admin area |
||
| 91 | * |
||
| 92 | * @return array $options |
||
| 93 | * |
||
| 94 | * @since 2.0 |
||
| 95 | * @see PodsField::ui_options |
||
| 96 | */ |
||
| 97 | public function options() { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Options for the Admin area, defaults to $this->options() |
||
| 145 | * |
||
| 146 | * @return array $options |
||
| 147 | * |
||
| 148 | * @since 2.0 |
||
| 149 | * @see PodsField::options |
||
| 150 | */ |
||
| 151 | public function ui_options() { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Define the current field's schema for DB table storage |
||
| 159 | * |
||
| 160 | * @param array|null $options Field options. |
||
| 161 | * |
||
| 162 | * @return string|false |
||
| 163 | * |
||
| 164 | * @since 2.0 |
||
| 165 | */ |
||
| 166 | public function schema( $options = null ) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Define the current field's preparation for sprintf |
||
| 176 | * |
||
| 177 | * @param array|null $options Field options. |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | * |
||
| 181 | * @since 2.0 |
||
| 182 | */ |
||
| 183 | public function prepare( $options = null ) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Check if the field is empty. |
||
| 193 | * |
||
| 194 | * @param mixed $value Field value. |
||
| 195 | * |
||
| 196 | * @return bool |
||
| 197 | * |
||
| 198 | * @since 2.7 |
||
| 199 | */ |
||
| 200 | public function is_empty( $value ) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Check if the field values are empty. |
||
| 218 | * |
||
| 219 | * @param array|mixed $values Field values. |
||
| 220 | * @param boolean $strict Whether to check if any of the values are non-empty in an array. |
||
| 221 | * |
||
| 222 | * @return bool |
||
| 223 | * |
||
| 224 | * @since 2.7 |
||
| 225 | */ |
||
| 226 | public function values_are_empty( $values, $strict = true ) { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Change the value of the field |
||
| 254 | * |
||
| 255 | * @param mixed|null $value Current value. |
||
| 256 | * @param string|null $name Field name. |
||
| 257 | * @param array|null $options Field options. |
||
| 258 | * @param array|null $pod Pod information. |
||
| 259 | * @param int|string|null $id Current item ID. |
||
| 260 | * |
||
| 261 | * @return mixed|null|string |
||
| 262 | * |
||
| 263 | * @since 2.3 |
||
| 264 | */ |
||
| 265 | public function value( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Change the way the value of the field is displayed with Pods::get |
||
| 273 | * |
||
| 274 | * @param mixed|null $value Current value. |
||
| 275 | * @param string|null $name Field name. |
||
| 276 | * @param array|null $options Field options. |
||
| 277 | * @param array|null $pod Pod information. |
||
| 278 | * @param int|string|null $id Current item ID. |
||
| 279 | * |
||
| 280 | * @return mixed|null|string |
||
| 281 | * |
||
| 282 | * @since 2.0 |
||
| 283 | */ |
||
| 284 | public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Reformat a number to the way the value of the field is displayed. |
||
| 292 | * |
||
| 293 | * @param mixed|null $value Current value. |
||
| 294 | * @param string|null $name Field name. |
||
| 295 | * @param array|null $options Field options. |
||
| 296 | * @param array|null $pod Pod information. |
||
| 297 | * @param int|string|null $id Current item ID. |
||
| 298 | * |
||
| 299 | * @return string|null |
||
| 300 | * @since 2.0 |
||
| 301 | */ |
||
| 302 | public function format( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Customize output of the form field |
||
| 310 | * |
||
| 311 | * @param string|null $name Field name. |
||
| 312 | * @param mixed|null $value Current value. |
||
| 313 | * @param array|null $options Field options. |
||
| 314 | * @param array|null $pod Pod information. |
||
| 315 | * @param int|string|null $id Current item ID. |
||
| 316 | * |
||
| 317 | * @since 2.0 |
||
| 318 | */ |
||
| 319 | public function input( $name, $value = null, $options = null, $pod = null, $id = null ) { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Render input script for Pods DFV |
||
| 347 | * |
||
| 348 | * @param array|object $args { |
||
| 349 | * Field information arguments. |
||
| 350 | * |
||
| 351 | * @type string $name Field name. |
||
| 352 | * @type string $type Field type. |
||
| 353 | * @type array $options Field options. |
||
| 354 | * @type mixed $value Current value. |
||
| 355 | * @type array $pod Pod information. |
||
| 356 | * @type int|string $id Current item ID. |
||
| 357 | * @type string $form_field_type HTML field type. |
||
| 358 | * } |
||
| 359 | */ |
||
| 360 | public function render_input_script( $args ) { |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Build field data for Pods DFV |
||
| 378 | * |
||
| 379 | * @param object $args { |
||
| 380 | * Field information arguments. |
||
| 381 | * |
||
| 382 | * @type string $name Field name. |
||
| 383 | * @type string $type Field type. |
||
| 384 | * @type array $options Field options. |
||
| 385 | * @type mixed $value Current value. |
||
| 386 | * @type array $pod Pod information. |
||
| 387 | * @type int|string $id Current item ID. |
||
| 388 | * @type string $form_field_type HTML field type. |
||
| 389 | * } |
||
| 390 | * |
||
| 391 | * @return array |
||
| 392 | */ |
||
| 393 | public function build_dfv_field_data( $args ) { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Build field options and handle any validation/customization for Pods DFV |
||
| 444 | * |
||
| 445 | * @param array $options Field options. |
||
| 446 | * @param object $args { |
||
| 447 | * Field information arguments. |
||
| 448 | * |
||
| 449 | * @type string $name Field name. |
||
| 450 | * @type string $type Field type. |
||
| 451 | * @type array $options Field options. |
||
| 452 | * @type mixed $value Current value. |
||
| 453 | * @type array $pod Pod information. |
||
| 454 | * @type int|string $id Current item ID. |
||
| 455 | * @type string $form_field_type HTML field type. |
||
| 456 | * } |
||
| 457 | * |
||
| 458 | * @return array |
||
| 459 | */ |
||
| 460 | public function build_dfv_field_options( $options, $args ) { |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Build field HTML attributes for Pods DFV. |
||
| 468 | * |
||
| 469 | * @param array $attributes Default HTML attributes from field and PodsForm::merge_attributes. |
||
| 470 | * @param object $args { |
||
| 471 | * Field information arguments. |
||
| 472 | * |
||
| 473 | * @type string $name Field name. |
||
| 474 | * @type string $type Field type. |
||
| 475 | * @type array $options Field options. |
||
| 476 | * @type mixed $value Current value. |
||
| 477 | * @type array $pod Pod information. |
||
| 478 | * @type int|string $id Current item ID. |
||
| 479 | * @type string $form_field_type HTML field type. |
||
| 480 | * } |
||
| 481 | * |
||
| 482 | * @return array |
||
| 483 | */ |
||
| 484 | public function build_dfv_field_attributes( $attributes, $args ) { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Build field config for Pods DFV using field options. |
||
| 492 | * |
||
| 493 | * This is for customizing the options and adding output-specific config values. |
||
| 494 | * |
||
| 495 | * @param object $args { |
||
| 496 | * Field information arguments. |
||
| 497 | * |
||
| 498 | * @type string $name Field name. |
||
| 499 | * @type string $type Field type. |
||
| 500 | * @type array $options Field options. |
||
| 501 | * @type mixed $value Current value. |
||
| 502 | * @type array $pod Pod information. |
||
| 503 | * @type int|string $id Current item ID. |
||
| 504 | * @type string $form_field_type HTML field type. |
||
| 505 | * } |
||
| 506 | * |
||
| 507 | * @return array |
||
| 508 | */ |
||
| 509 | public function build_dfv_field_config( $args ) { |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Build array of item data for Pods DFV. |
||
| 523 | * |
||
| 524 | * @param object $args { |
||
| 525 | * Field information arguments. |
||
| 526 | * |
||
| 527 | * @type string $name Field name. |
||
| 528 | * @type string $type Field type. |
||
| 529 | * @type array $options Field options. |
||
| 530 | * @type mixed $value Current value. |
||
| 531 | * @type array $pod Pod information. |
||
| 532 | * @type int|string $id Current item ID. |
||
| 533 | * @type string $form_field_type HTML field type. |
||
| 534 | * } |
||
| 535 | * |
||
| 536 | * @return array |
||
| 537 | */ |
||
| 538 | public function build_dfv_field_item_data( $args ) { |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Get the data from the field. |
||
| 552 | * |
||
| 553 | * @param string|null $name Field name. |
||
| 554 | * @param mixed|null $value Current value. |
||
| 555 | * @param array|null $options Field options. |
||
| 556 | * @param array|null $pod Pod information. |
||
| 557 | * @param int|string|null $id Current item ID. |
||
| 558 | * @param boolean $in_form Whether we are in the form context. |
||
| 559 | * |
||
| 560 | * @return array Array of possible field data. |
||
| 561 | * |
||
| 562 | * @since 2.0 |
||
| 563 | */ |
||
| 564 | public function data( $name, $value = null, $options = null, $pod = null, $id = null, $in_form = true ) { |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Build regex necessary for JS validation. |
||
| 572 | * |
||
| 573 | * @param mixed|null $value Current value. |
||
| 574 | * @param string|null $name Field name. |
||
| 575 | * @param array|null $options Field options. |
||
| 576 | * @param array|null $pod Pod information. |
||
| 577 | * @param int|string|null $id Current item ID. |
||
| 578 | * |
||
| 579 | * @return string|false |
||
| 580 | * |
||
| 581 | * @since 2.0 |
||
| 582 | */ |
||
| 583 | public function regex( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Validate a value before it's saved. |
||
| 591 | * |
||
| 592 | * @param mixed|null $value Current value. |
||
| 593 | * @param string|null $name Field name. |
||
| 594 | * @param array|null $options Field options. |
||
| 595 | * @param array|null $fields Pod fields. |
||
| 596 | * @param array|null $pod Pod information. |
||
| 597 | * @param int|string|null $id Current item ID. |
||
| 598 | * @param array|null $params Additional parameters. |
||
| 599 | * |
||
| 600 | * @return bool |
||
| 601 | * |
||
| 602 | * @since 2.0 |
||
| 603 | */ |
||
| 604 | public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Change the value or perform actions after validation but before saving to the DB |
||
| 612 | * |
||
| 613 | * @param mixed|null $value Current value. |
||
| 614 | * @param int|string|null $id Current Item ID. |
||
| 615 | * @param string|null $name Field name. |
||
| 616 | * @param array|null $options Field options. |
||
| 617 | * @param array|null $fields Pod fields. |
||
| 618 | * @param array|null $pod Pod information. |
||
| 619 | * @param array|null $params Additional parameters. |
||
| 620 | * |
||
| 621 | * @return mixed |
||
| 622 | * |
||
| 623 | * @since 2.0 |
||
| 624 | */ |
||
| 625 | public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Save the value to the DB |
||
| 633 | * |
||
| 634 | * @param mixed|null $value Current value. |
||
| 635 | * @param int|string|null $id Current Item ID. |
||
| 636 | * @param string|null $name Field name. |
||
| 637 | * @param array|null $options Field options. |
||
| 638 | * @param array|null $fields Pod fields. |
||
| 639 | * @param array|null $pod Pod information. |
||
| 640 | * @param array|null $params Additional parameters. |
||
| 641 | * |
||
| 642 | * @return bool|null Whether the value was saved, returning null means no save needed to occur |
||
| 643 | * |
||
| 644 | * @since 2.3 |
||
| 645 | */ |
||
| 646 | public function save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Perform actions after saving to the DB |
||
| 654 | * |
||
| 655 | * @param mixed|null $value Current value. |
||
| 656 | * @param int|string|null $id Current Item ID. |
||
| 657 | * @param string|null $name Field name. |
||
| 658 | * @param array|null $options Field options. |
||
| 659 | * @param array|null $fields Pod fields. |
||
| 660 | * @param array|null $pod Pod information. |
||
| 661 | * @param array|null $params Additional parameters. |
||
| 662 | * |
||
| 663 | * @since 2.0 |
||
| 664 | */ |
||
| 665 | public function post_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Perform actions before deleting from the DB |
||
| 672 | * |
||
| 673 | * @param int|string|null $id Current Item ID. |
||
| 674 | * @param string|null $name Field name. |
||
| 675 | * @param array|null $options Field options. |
||
| 676 | * @param array|null $pod Pod information. |
||
| 677 | * |
||
| 678 | * @since 2.0 |
||
| 679 | */ |
||
| 680 | public function pre_delete( $id = null, $name = null, $options = null, $pod = null ) { |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Delete the value from the DB |
||
| 687 | * |
||
| 688 | * @param int|string|null $id Current Item ID. |
||
| 689 | * @param string|null $name Field name. |
||
| 690 | * @param array|null $options Field options. |
||
| 691 | * @param array|null $pod Pod information. |
||
| 692 | * |
||
| 693 | * @since 2.3 |
||
| 694 | */ |
||
| 695 | public function delete( $id = null, $name = null, $options = null, $pod = null ) { |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Perform actions after deleting from the DB |
||
| 702 | * |
||
| 703 | * @param int|string|null $id Current Item ID. |
||
| 704 | * @param string|null $name Field name. |
||
| 705 | * @param array|null $options Field options. |
||
| 706 | * @param array|null $pod Pod information. |
||
| 707 | * |
||
| 708 | * @since 2.0 |
||
| 709 | */ |
||
| 710 | public function post_delete( $id = null, $name = null, $options = null, $pod = null ) { |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Customize the Pods UI manage table column output |
||
| 717 | * |
||
| 718 | * @param int|string|null $id Current Item ID. |
||
| 719 | * @param mixed|null $value Current value. |
||
| 720 | * @param string|null $name Field name. |
||
| 721 | * @param array|null $options Field options. |
||
| 722 | * @param array|null $fields Pod fields. |
||
| 723 | * @param array|null $pod Pod information. |
||
| 724 | * |
||
| 725 | * @return string Value to be shown in the UI |
||
| 726 | * |
||
| 727 | * @since 2.0 |
||
| 728 | */ |
||
| 729 | public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Strip HTML based on options. |
||
| 737 | * |
||
| 738 | * @param string|array $value Field value. |
||
| 739 | * @param array|null $options Field options. |
||
| 740 | * |
||
| 741 | * @return string |
||
| 742 | */ |
||
| 743 | public function strip_html( $value, $options = null ) { |
||
| 785 | |||
| 786 | /** |
||
| 787 | * Placeholder function to allow var_export() use with classes. |
||
| 788 | * |
||
| 789 | * @param array $properties Properties to export. |
||
| 790 | * |
||
| 791 | * @return void |
||
| 792 | */ |
||
| 793 | public static function __set_state( $properties ) { |
||
| 796 | |||
| 797 | } |
||
| 798 |
This check marks private properties in classes that are never used. Those properties can be removed.