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 | * Do things like register/enqueue scripts and stylesheets |
||
| 60 | * |
||
| 61 | * @since 2.0 |
||
| 62 | */ |
||
| 63 | public function __construct() { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Add admin_init actions. |
||
| 70 | * |
||
| 71 | * @since 2.3 |
||
| 72 | */ |
||
| 73 | public function admin_init() { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Add options and set defaults for field type, shows in admin area |
||
| 80 | * |
||
| 81 | * @return array $options |
||
| 82 | * |
||
| 83 | * @since 2.0 |
||
| 84 | * @see PodsField::ui_options |
||
| 85 | */ |
||
| 86 | public function options() { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Options for the Admin area, defaults to $this->options() |
||
| 133 | * |
||
| 134 | * @return array $options |
||
| 135 | * |
||
| 136 | * @since 2.0 |
||
| 137 | * @see PodsField::options |
||
| 138 | */ |
||
| 139 | public function ui_options() { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Define the current field's schema for DB table storage |
||
| 147 | * |
||
| 148 | * @param array|null $options |
||
| 149 | * |
||
| 150 | * @return string |
||
| 151 | * |
||
| 152 | * @since 2.0 |
||
| 153 | */ |
||
| 154 | public function schema( $options = null ) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Define the current field's preparation for sprintf |
||
| 164 | * |
||
| 165 | * @param array|null $options |
||
| 166 | * |
||
| 167 | * @return string |
||
| 168 | * |
||
| 169 | * @since 2.0 |
||
| 170 | */ |
||
| 171 | public function prepare( $options = null ) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Check if the field is empty. |
||
| 181 | * |
||
| 182 | * @param mixed $value |
||
| 183 | * |
||
| 184 | * @return bool |
||
| 185 | * |
||
| 186 | * @since 2.7 |
||
| 187 | */ |
||
| 188 | public function is_empty( $value ) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Change the value of the field |
||
| 206 | * |
||
| 207 | * @param mixed|null $value |
||
| 208 | * @param string|null $name |
||
| 209 | * @param array|null $options |
||
| 210 | * @param array|null $pod |
||
| 211 | * @param int|null $id |
||
| 212 | * |
||
| 213 | * @return mixed|null|string |
||
| 214 | * |
||
| 215 | * @since 2.3 |
||
| 216 | */ |
||
| 217 | public function value( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Change the way the value of the field is displayed with Pods::get |
||
| 225 | * |
||
| 226 | * @param mixed|null $value |
||
| 227 | * @param string|null $name |
||
| 228 | * @param array|null $options |
||
| 229 | * @param array|null $pod |
||
| 230 | * @param int|null $id |
||
| 231 | * |
||
| 232 | * @return mixed|null|string |
||
| 233 | * |
||
| 234 | * @since 2.0 |
||
| 235 | */ |
||
| 236 | public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Reformat a number to the way the value of the field is displayed. |
||
| 244 | * |
||
| 245 | * @param mixed $value |
||
| 246 | * @param string $name |
||
| 247 | * @param array $options |
||
| 248 | * @param array $pod |
||
| 249 | * @param int $id |
||
| 250 | * |
||
| 251 | * @return string|null |
||
| 252 | * @since 2.0 |
||
| 253 | */ |
||
| 254 | public function format( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Customize output of the form field |
||
| 262 | * |
||
| 263 | * @param string $name |
||
| 264 | * @param mixed|null $value |
||
| 265 | * @param array|null $options |
||
| 266 | * @param array|null $pod |
||
| 267 | * @param int|null $id |
||
| 268 | * |
||
| 269 | * @since 2.0 |
||
| 270 | */ |
||
| 271 | public function input( $name, $value = null, $options = null, $pod = null, $id = null ) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Render input script for Pods DFV |
||
| 299 | * |
||
| 300 | * @param array|object $args { |
||
| 301 | * Field information arguments. |
||
| 302 | * |
||
| 303 | * @type string $name Field name |
||
| 304 | * @type string $type Field type |
||
| 305 | * @type array $options Field options |
||
| 306 | * @type mixed $value Current value |
||
| 307 | * @type array $pod Pod information |
||
| 308 | * @type int|string $id Current item ID |
||
| 309 | * } |
||
| 310 | */ |
||
| 311 | public function render_input_script( $args ) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Build field data for Pods DFV |
||
| 328 | * |
||
| 329 | * @param object $args { |
||
| 330 | * Field information arguments. |
||
| 331 | * |
||
| 332 | * @type string $name Field name |
||
| 333 | * @type string $type Pod field type |
||
| 334 | * @type string $form_field_type HTML field type |
||
| 335 | * @type array $options Field options |
||
| 336 | * @type mixed $value Current value |
||
| 337 | * @type array $pod Pod information |
||
| 338 | * @type int|string $id Current item ID |
||
| 339 | * } |
||
| 340 | * |
||
| 341 | * @return array |
||
| 342 | */ |
||
| 343 | public function build_dfv_field_data( $args ) { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Build field options and handle any validation/customization for Pods DFV |
||
| 394 | * |
||
| 395 | * @param array $options |
||
| 396 | * @param object $args { |
||
| 397 | * Field information arguments. |
||
| 398 | * |
||
| 399 | * @type string $name Field name |
||
| 400 | * @type string $type Field type |
||
| 401 | * @type array $options Field options |
||
| 402 | * @type mixed $value Current value |
||
| 403 | * @type array $pod Pod information |
||
| 404 | * @type int|string $id Current item ID |
||
| 405 | * } |
||
| 406 | * |
||
| 407 | * @return array |
||
| 408 | */ |
||
| 409 | public function build_dfv_field_options( $options, $args ) { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Build field HTML attributes for Pods DFV. |
||
| 417 | * |
||
| 418 | * @param array $attributes Default HTML attributes from field and PodsForm::merge_attributes. |
||
| 419 | * @param object $args { |
||
| 420 | * Field information arguments. |
||
| 421 | * |
||
| 422 | * @type string $name Field name |
||
| 423 | * @type string $type Field type |
||
| 424 | * @type array $options Field options |
||
| 425 | * @type mixed $value Current value |
||
| 426 | * @type array $pod Pod information |
||
| 427 | * @type int|string $id Current item ID |
||
| 428 | * } |
||
| 429 | * |
||
| 430 | * @return array |
||
| 431 | */ |
||
| 432 | public function build_dfv_field_attributes( $attributes, $args ) { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Build field config for Pods DFV using field options. |
||
| 440 | * |
||
| 441 | * This is for customizing the options and adding output-specific config values. |
||
| 442 | * |
||
| 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_config( $args ) { |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Build array of item data for Pods DFV. |
||
| 470 | * |
||
| 471 | * @param object $args { |
||
| 472 | * Field information arguments. |
||
| 473 | * |
||
| 474 | * @type string $name Field name. |
||
| 475 | * @type string $type Field type. |
||
| 476 | * @type array $options Field options. |
||
| 477 | * @type mixed $value Current value. |
||
| 478 | * @type array $pod Pod information. |
||
| 479 | * @type int|string $id Current item ID. |
||
| 480 | * } |
||
| 481 | * |
||
| 482 | * @return array |
||
| 483 | */ |
||
| 484 | public function build_dfv_field_item_data( $args ) { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Get the data from the field |
||
| 498 | * |
||
| 499 | * @param string $name The name of the field |
||
| 500 | * @param string|array|null $value The value of the field |
||
| 501 | * @param array|null $options |
||
| 502 | * @param array|null $pod |
||
| 503 | * @param int|null $id |
||
| 504 | * @param boolean $in_form |
||
| 505 | * |
||
| 506 | * @return array Array of possible field data |
||
| 507 | * |
||
| 508 | * @since 2.0 |
||
| 509 | */ |
||
| 510 | public function data( $name, $value = null, $options = null, $pod = null, $id = null, $in_form = true ) { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Build regex necessary for JS validation |
||
| 518 | * |
||
| 519 | * @param mixed|null $value |
||
| 520 | * @param string|null $name |
||
| 521 | * @param array|null $options |
||
| 522 | * @param string|null $pod |
||
| 523 | * @param int|null $id |
||
| 524 | * |
||
| 525 | * @return bool |
||
| 526 | * |
||
| 527 | * @since 2.0 |
||
| 528 | */ |
||
| 529 | public function regex( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Validate a value before it's saved |
||
| 537 | * |
||
| 538 | * @param mixed $value |
||
| 539 | * @param string|null $name |
||
| 540 | * @param array|null $options |
||
| 541 | * @param array|null $fields |
||
| 542 | * @param array|null $pod |
||
| 543 | * @param int|null $id |
||
| 544 | * @param array|null $params |
||
| 545 | * |
||
| 546 | * @return bool |
||
| 547 | * |
||
| 548 | * @since 2.0 |
||
| 549 | */ |
||
| 550 | public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Change the value or perform actions after validation but before saving to the DB |
||
| 558 | * |
||
| 559 | * @param mixed $value |
||
| 560 | * @param int|null $id |
||
| 561 | * @param string|null $name |
||
| 562 | * @param array|null $options |
||
| 563 | * @param array|null $fields |
||
| 564 | * @param array|null $pod |
||
| 565 | * @param object|null $params |
||
| 566 | * |
||
| 567 | * @return mixed |
||
| 568 | * |
||
| 569 | * @since 2.0 |
||
| 570 | */ |
||
| 571 | public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Save the value to the DB |
||
| 579 | * |
||
| 580 | * @param mixed $value |
||
| 581 | * @param int|null $id |
||
| 582 | * @param string|null $name |
||
| 583 | * @param array|null $options |
||
| 584 | * @param array|null $fields |
||
| 585 | * @param array|null $pod |
||
| 586 | * @param object|null $params |
||
| 587 | * |
||
| 588 | * @return bool|null Whether the value was saved, returning null means no save needed to occur |
||
| 589 | * |
||
| 590 | * @since 2.3 |
||
| 591 | */ |
||
| 592 | public function save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Perform actions after saving to the DB |
||
| 600 | * |
||
| 601 | * @param mixed $value |
||
| 602 | * @param int|null $id |
||
| 603 | * @param string|null $name |
||
| 604 | * @param array|null $options |
||
| 605 | * @param array|null $fields |
||
| 606 | * @param array|null $pod |
||
| 607 | * @param object|null $params |
||
| 608 | * |
||
| 609 | * @since 2.0 |
||
| 610 | */ |
||
| 611 | public function post_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Perform actions before deleting from the DB |
||
| 618 | * |
||
| 619 | * @param int|null $id |
||
| 620 | * @param string|null $name |
||
| 621 | * @param array|null $options |
||
| 622 | * @param string|null $pod |
||
| 623 | * |
||
| 624 | * @since 2.0 |
||
| 625 | */ |
||
| 626 | public function pre_delete( $id = null, $name = null, $options = null, $pod = null ) { |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Delete the value from the DB |
||
| 633 | * |
||
| 634 | * @param int|null $id |
||
| 635 | * @param string|null $name |
||
| 636 | * @param array|null $options |
||
| 637 | * @param array|null $pod |
||
| 638 | * |
||
| 639 | * @since 2.3 |
||
| 640 | */ |
||
| 641 | public function delete( $id = null, $name = null, $options = null, $pod = null ) { |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Perform actions after deleting from the DB |
||
| 648 | * |
||
| 649 | * @param int|null $id |
||
| 650 | * @param string|null $name |
||
| 651 | * @param array|null $options |
||
| 652 | * @param array|null $pod |
||
| 653 | * |
||
| 654 | * @since 2.0 |
||
| 655 | */ |
||
| 656 | public function post_delete( $id = null, $name = null, $options = null, $pod = null ) { |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Customize the Pods UI manage table column output |
||
| 663 | * |
||
| 664 | * @param int $id |
||
| 665 | * @param mixed $value |
||
| 666 | * @param string|null $name |
||
| 667 | * @param array|null $options |
||
| 668 | * @param array|null $fields |
||
| 669 | * @param array|null $pod |
||
| 670 | * |
||
| 671 | * @return string Value to be shown in the UI |
||
| 672 | * |
||
| 673 | * @since 2.0 |
||
| 674 | */ |
||
| 675 | public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Strip HTML based on options. |
||
| 683 | * |
||
| 684 | * @param string|array $value Field value. |
||
| 685 | * @param array|null $options Field options. |
||
| 686 | * |
||
| 687 | * @return string |
||
| 688 | */ |
||
| 689 | public function strip_html( $value, $options = null ) { |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Placeholder function to allow var_export() use with classes |
||
| 734 | * |
||
| 735 | * @param array $properties |
||
| 736 | * |
||
| 737 | * @return object|void |
||
| 738 | */ |
||
| 739 | public static function __set_state( $properties ) { |
||
| 744 | |||
| 745 | } |
||
| 746 |
This check marks private properties in classes that are never used. Those properties can be removed.