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() { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Options for the Admin area, defaults to $this->options() |
||
| 144 | * |
||
| 145 | * @return array $options |
||
| 146 | * |
||
| 147 | * @since 2.0 |
||
| 148 | * @see PodsField::options |
||
| 149 | */ |
||
| 150 | public function ui_options() { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Define the current field's schema for DB table storage |
||
| 158 | * |
||
| 159 | * @param array|null $options |
||
| 160 | * |
||
| 161 | * @return string |
||
| 162 | * |
||
| 163 | * @since 2.0 |
||
| 164 | */ |
||
| 165 | public function schema( $options = null ) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Define the current field's preparation for sprintf |
||
| 175 | * |
||
| 176 | * @param array|null $options |
||
| 177 | * |
||
| 178 | * @return string |
||
| 179 | * |
||
| 180 | * @since 2.0 |
||
| 181 | */ |
||
| 182 | public function prepare( $options = null ) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Check if the field is empty. |
||
| 192 | * |
||
| 193 | * @param mixed $value Field value. |
||
| 194 | * |
||
| 195 | * @return bool |
||
| 196 | * |
||
| 197 | * @since 2.7 |
||
| 198 | */ |
||
| 199 | public function is_empty( $value ) { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Check if the field values are empty. |
||
| 217 | * |
||
| 218 | * @param array|mixed $values Field values. |
||
| 219 | * @param boolean $strict Whether to check if any of the values are non-empty in an array. |
||
| 220 | * |
||
| 221 | * @return bool |
||
| 222 | * |
||
| 223 | * @since 2.7 |
||
| 224 | */ |
||
| 225 | public function values_are_empty( $values, $strict = true ) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Change the value of the field |
||
| 253 | * |
||
| 254 | * @param mixed|null $value |
||
| 255 | * @param string|null $name |
||
| 256 | * @param array|null $options |
||
| 257 | * @param array|null $pod |
||
| 258 | * @param int|null $id |
||
| 259 | * |
||
| 260 | * @return mixed|null|string |
||
| 261 | * |
||
| 262 | * @since 2.3 |
||
| 263 | */ |
||
| 264 | public function value( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Change the way the value of the field is displayed with Pods::get |
||
| 272 | * |
||
| 273 | * @param mixed|null $value |
||
| 274 | * @param string|null $name |
||
| 275 | * @param array|null $options |
||
| 276 | * @param array|null $pod |
||
| 277 | * @param int|null $id |
||
| 278 | * |
||
| 279 | * @return mixed|null|string |
||
| 280 | * |
||
| 281 | * @since 2.0 |
||
| 282 | */ |
||
| 283 | public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Reformat a number to the way the value of the field is displayed. |
||
| 291 | * |
||
| 292 | * @param mixed $value |
||
| 293 | * @param string $name |
||
| 294 | * @param array $options |
||
| 295 | * @param array $pod |
||
| 296 | * @param int $id |
||
| 297 | * |
||
| 298 | * @return string|null |
||
| 299 | * @since 2.0 |
||
| 300 | */ |
||
| 301 | public function format( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Customize output of the form field |
||
| 309 | * |
||
| 310 | * @param string $name |
||
| 311 | * @param mixed|null $value |
||
| 312 | * @param array|null $options |
||
| 313 | * @param array|null $pod |
||
| 314 | * @param int|null $id |
||
| 315 | * |
||
| 316 | * @since 2.0 |
||
| 317 | */ |
||
| 318 | public function input( $name, $value = null, $options = null, $pod = null, $id = null ) { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Render input script for Pods DFV |
||
| 346 | * |
||
| 347 | * @param array|object $args { |
||
| 348 | * Field information arguments. |
||
| 349 | * |
||
| 350 | * @type string $name Field name |
||
| 351 | * @type string $type Field type |
||
| 352 | * @type array $options Field options |
||
| 353 | * @type mixed $value Current value |
||
| 354 | * @type array $pod Pod information |
||
| 355 | * @type int|string $id Current item ID |
||
| 356 | * } |
||
| 357 | */ |
||
| 358 | public function render_input_script( $args ) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Build field data for Pods DFV |
||
| 375 | * |
||
| 376 | * @param object $args { |
||
| 377 | * Field information arguments. |
||
| 378 | * |
||
| 379 | * @type string $name Field name |
||
| 380 | * @type string $type Pod field type |
||
| 381 | * @type string $form_field_type HTML field type |
||
| 382 | * @type array $options Field options |
||
| 383 | * @type mixed $value Current value |
||
| 384 | * @type array $pod Pod information |
||
| 385 | * @type int|string $id Current item ID |
||
| 386 | * } |
||
| 387 | * |
||
| 388 | * @return array |
||
| 389 | */ |
||
| 390 | public function build_dfv_field_data( $args ) { |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Build field options and handle any validation/customization for Pods DFV |
||
| 441 | * |
||
| 442 | * @param array $options |
||
| 443 | * @param object $args { |
||
| 444 | * Field information arguments. |
||
| 445 | * |
||
| 446 | * @type string $name Field name |
||
| 447 | * @type string $type Field type |
||
| 448 | * @type array $options Field options |
||
| 449 | * @type mixed $value Current value |
||
| 450 | * @type array $pod Pod information |
||
| 451 | * @type int|string $id Current item ID |
||
| 452 | * } |
||
| 453 | * |
||
| 454 | * @return array |
||
| 455 | */ |
||
| 456 | public function build_dfv_field_options( $options, $args ) { |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Build field HTML attributes for Pods DFV. |
||
| 464 | * |
||
| 465 | * @param array $attributes Default HTML attributes from field and PodsForm::merge_attributes. |
||
| 466 | * @param object $args { |
||
| 467 | * Field information arguments. |
||
| 468 | * |
||
| 469 | * @type string $name Field name |
||
| 470 | * @type string $type Field type |
||
| 471 | * @type array $options Field options |
||
| 472 | * @type mixed $value Current value |
||
| 473 | * @type array $pod Pod information |
||
| 474 | * @type int|string $id Current item ID |
||
| 475 | * } |
||
| 476 | * |
||
| 477 | * @return array |
||
| 478 | */ |
||
| 479 | public function build_dfv_field_attributes( $attributes, $args ) { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Build field config for Pods DFV using field options. |
||
| 487 | * |
||
| 488 | * This is for customizing the options and adding output-specific config values. |
||
| 489 | * |
||
| 490 | * @param object $args { |
||
| 491 | * Field information arguments. |
||
| 492 | * |
||
| 493 | * @type string $name Field name. |
||
| 494 | * @type string $type Field type. |
||
| 495 | * @type array $options Field options. |
||
| 496 | * @type mixed $value Current value. |
||
| 497 | * @type array $pod Pod information. |
||
| 498 | * @type int|string $id Current item ID. |
||
| 499 | * } |
||
| 500 | * |
||
| 501 | * @return array |
||
| 502 | */ |
||
| 503 | public function build_dfv_field_config( $args ) { |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Build array of item data for Pods DFV. |
||
| 517 | * |
||
| 518 | * @param object $args { |
||
| 519 | * Field information arguments. |
||
| 520 | * |
||
| 521 | * @type string $name Field name. |
||
| 522 | * @type string $type Field type. |
||
| 523 | * @type array $options Field options. |
||
| 524 | * @type mixed $value Current value. |
||
| 525 | * @type array $pod Pod information. |
||
| 526 | * @type int|string $id Current item ID. |
||
| 527 | * } |
||
| 528 | * |
||
| 529 | * @return array |
||
| 530 | */ |
||
| 531 | public function build_dfv_field_item_data( $args ) { |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Get the data from the field |
||
| 545 | * |
||
| 546 | * @param string $name The name of the field |
||
| 547 | * @param string|array|null $value The value of the field |
||
| 548 | * @param array|null $options |
||
| 549 | * @param array|null $pod |
||
| 550 | * @param int|null $id |
||
| 551 | * @param boolean $in_form |
||
| 552 | * |
||
| 553 | * @return array Array of possible field data |
||
| 554 | * |
||
| 555 | * @since 2.0 |
||
| 556 | */ |
||
| 557 | public function data( $name, $value = null, $options = null, $pod = null, $id = null, $in_form = true ) { |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Build regex necessary for JS validation |
||
| 565 | * |
||
| 566 | * @param mixed|null $value |
||
| 567 | * @param string|null $name |
||
| 568 | * @param array|null $options |
||
| 569 | * @param string|null $pod |
||
| 570 | * @param int|null $id |
||
| 571 | * |
||
| 572 | * @return bool |
||
| 573 | * |
||
| 574 | * @since 2.0 |
||
| 575 | */ |
||
| 576 | public function regex( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Validate a value before it's saved |
||
| 584 | * |
||
| 585 | * @param mixed $value |
||
| 586 | * @param string|null $name |
||
| 587 | * @param array|null $options |
||
| 588 | * @param array|null $fields |
||
| 589 | * @param array|null $pod |
||
| 590 | * @param int|null $id |
||
| 591 | * @param array|null $params |
||
| 592 | * |
||
| 593 | * @return bool |
||
| 594 | * |
||
| 595 | * @since 2.0 |
||
| 596 | */ |
||
| 597 | public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Change the value or perform actions after validation but before saving to the DB |
||
| 605 | * |
||
| 606 | * @param mixed $value |
||
| 607 | * @param int|null $id |
||
| 608 | * @param string|null $name |
||
| 609 | * @param array|null $options |
||
| 610 | * @param array|null $fields |
||
| 611 | * @param array|null $pod |
||
| 612 | * @param object|null $params |
||
| 613 | * |
||
| 614 | * @return mixed |
||
| 615 | * |
||
| 616 | * @since 2.0 |
||
| 617 | */ |
||
| 618 | public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Save the value to the DB |
||
| 626 | * |
||
| 627 | * @param mixed $value |
||
| 628 | * @param int|null $id |
||
| 629 | * @param string|null $name |
||
| 630 | * @param array|null $options |
||
| 631 | * @param array|null $fields |
||
| 632 | * @param array|null $pod |
||
| 633 | * @param object|null $params |
||
| 634 | * |
||
| 635 | * @return bool|null Whether the value was saved, returning null means no save needed to occur |
||
| 636 | * |
||
| 637 | * @since 2.3 |
||
| 638 | */ |
||
| 639 | public function save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Perform actions after saving to the DB |
||
| 647 | * |
||
| 648 | * @param mixed $value |
||
| 649 | * @param int|null $id |
||
| 650 | * @param string|null $name |
||
| 651 | * @param array|null $options |
||
| 652 | * @param array|null $fields |
||
| 653 | * @param array|null $pod |
||
| 654 | * @param object|null $params |
||
| 655 | * |
||
| 656 | * @since 2.0 |
||
| 657 | */ |
||
| 658 | public function post_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Perform actions before deleting from the DB |
||
| 665 | * |
||
| 666 | * @param int|null $id |
||
| 667 | * @param string|null $name |
||
| 668 | * @param array|null $options |
||
| 669 | * @param string|null $pod |
||
| 670 | * |
||
| 671 | * @since 2.0 |
||
| 672 | */ |
||
| 673 | public function pre_delete( $id = null, $name = null, $options = null, $pod = null ) { |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Delete the value from the DB |
||
| 680 | * |
||
| 681 | * @param int|null $id |
||
| 682 | * @param string|null $name |
||
| 683 | * @param array|null $options |
||
| 684 | * @param array|null $pod |
||
| 685 | * |
||
| 686 | * @since 2.3 |
||
| 687 | */ |
||
| 688 | public function delete( $id = null, $name = null, $options = null, $pod = null ) { |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Perform actions after deleting from the DB |
||
| 695 | * |
||
| 696 | * @param int|null $id |
||
| 697 | * @param string|null $name |
||
| 698 | * @param array|null $options |
||
| 699 | * @param array|null $pod |
||
| 700 | * |
||
| 701 | * @since 2.0 |
||
| 702 | */ |
||
| 703 | public function post_delete( $id = null, $name = null, $options = null, $pod = null ) { |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Customize the Pods UI manage table column output |
||
| 710 | * |
||
| 711 | * @param int $id |
||
| 712 | * @param mixed $value |
||
| 713 | * @param string|null $name |
||
| 714 | * @param array|null $options |
||
| 715 | * @param array|null $fields |
||
| 716 | * @param array|null $pod |
||
| 717 | * |
||
| 718 | * @return string Value to be shown in the UI |
||
| 719 | * |
||
| 720 | * @since 2.0 |
||
| 721 | */ |
||
| 722 | public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Strip HTML based on options. |
||
| 730 | * |
||
| 731 | * @param string|array $value Field value. |
||
| 732 | * @param array|null $options Field options. |
||
| 733 | * |
||
| 734 | * @return string |
||
| 735 | */ |
||
| 736 | public function strip_html( $value, $options = null ) { |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Placeholder function to allow var_export() use with classes |
||
| 781 | * |
||
| 782 | * @param array $properties |
||
| 783 | * |
||
| 784 | * @return object|void |
||
| 785 | */ |
||
| 786 | public static function __set_state( $properties ) { |
||
| 791 | |||
| 792 | } |
||
| 793 |
This check marks private properties in classes that are never used. Those properties can be removed.