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 Field 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 Field, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Field implements Datastore_Holder_Interface { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Array of field class names that have had their activation method called |
||
| 22 | * |
||
| 23 | * @var array<string> |
||
| 24 | */ |
||
| 25 | protected static $activated_field_types = array(); |
||
|
1 ignored issue
–
show
|
|||
| 26 | |||
| 27 | /** |
||
| 28 | * Globally unique field identificator. Generated randomly |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $id; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Stores the initial <kbd>$type</kbd> variable passed to the <code>factory()</code> method |
||
| 36 | * |
||
| 37 | * @see factory |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | public $type; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Array of ancestor field names |
||
| 44 | * |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected $hierarchy = array(); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Array of complex entry ids |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $hierarchy_index = array(); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Field value |
||
| 58 | * |
||
| 59 | * @var Value_Set |
||
| 60 | */ |
||
| 61 | protected $value_set; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Default field value |
||
| 65 | * |
||
| 66 | * @var mixed |
||
| 67 | */ |
||
| 68 | protected $default_value = ''; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Sanitized field name used as input name attribute during field render |
||
| 72 | * |
||
| 73 | * @see factory() |
||
| 74 | * @see set_name() |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | protected $name; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Field name prefix |
||
| 81 | * |
||
| 82 | * @see set_name() |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | protected $name_prefix = '_'; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The base field name which is used in the container. |
||
| 89 | * |
||
| 90 | * @see set_base_name() |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | protected $base_name; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Field name used as label during field render |
||
| 97 | * |
||
| 98 | * @see factory() |
||
| 99 | * @see set_label() |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | protected $label; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Additional text containing information and guidance for the user |
||
| 106 | * |
||
| 107 | * @see help_text() |
||
| 108 | * @var string |
||
| 109 | */ |
||
| 110 | protected $help_text; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Field DataStore instance to which save, load and delete calls are delegated |
||
| 114 | * |
||
| 115 | * @see set_datastore() |
||
| 116 | * @see get_datastore() |
||
| 117 | * @var Datastore_Interface |
||
| 118 | */ |
||
| 119 | protected $datastore; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Flag whether the datastore is the default one or replaced with a custom one |
||
| 123 | * |
||
| 124 | * @see set_datastore() |
||
| 125 | * @see get_datastore() |
||
| 126 | * @var boolean |
||
| 127 | */ |
||
| 128 | protected $has_default_datastore = true; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * The type of the container this field is in |
||
| 132 | * |
||
| 133 | * @see get_context() |
||
| 134 | * @var string |
||
| 135 | */ |
||
| 136 | protected $context; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Whether or not this value should be auto loaded. Applicable to theme options only. |
||
| 140 | * |
||
| 141 | * @see set_autoload() |
||
| 142 | * @var bool |
||
| 143 | */ |
||
| 144 | protected $autoload = false; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Whether or not this field will be initialized when the field is in the viewport (visible). |
||
| 148 | * |
||
| 149 | * @see set_lazyload() |
||
| 150 | * @var bool |
||
| 151 | */ |
||
| 152 | protected $lazyload = false; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * The width of the field. |
||
| 156 | * |
||
| 157 | * @see set_width() |
||
| 158 | * @var int |
||
| 159 | */ |
||
| 160 | protected $width = 0; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Custom CSS classes. |
||
| 164 | * |
||
| 165 | * @see set_classes() |
||
| 166 | * @var array |
||
| 167 | */ |
||
| 168 | protected $classes = array(); |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Whether or not this field is required. |
||
| 172 | * |
||
| 173 | * @see set_required() |
||
| 174 | * @var bool |
||
| 175 | */ |
||
| 176 | protected $required = false; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Stores the field conditional logic rules. |
||
| 180 | * |
||
| 181 | * @var array |
||
| 182 | */ |
||
| 183 | protected $conditional_logic = array(); |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Whether the field should be included in the response of the requests to the REST API |
||
| 187 | * |
||
| 188 | * @see set_visible_in_rest_api |
||
| 189 | * @see get_visible_in_rest_api |
||
| 190 | * @var boolean |
||
| 191 | */ |
||
| 192 | protected $visible_in_rest_api = false; |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Clone the Value_Set object as well |
||
| 196 | * |
||
| 197 | * @var array |
||
| 198 | */ |
||
| 199 | public function __clone() { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Create a new field of type $type and name $name and label $label. |
||
| 205 | * |
||
| 206 | * @param string $type |
||
| 207 | * @param string $name lower case and underscore-delimited |
||
| 208 | * @param string $label (optional) Automatically generated from $name if not present |
||
| 209 | * @return Field |
||
| 210 | */ |
||
| 211 | public static function factory( $raw_type, $name, $label = null ) { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * An alias of factory(). |
||
| 235 | * |
||
| 236 | * @see Field::factory() |
||
| 237 | * @return Field |
||
| 238 | */ |
||
| 239 | public static function make( $type, $name, $label = null ) { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Create a field from a certain type with the specified label. |
||
| 245 | * |
||
| 246 | * @param string $type Field type |
||
| 247 | * @param string $name Field name |
||
| 248 | * @param string $label Field label |
||
| 249 | */ |
||
| 250 | public function __construct( $type, $name, $label ) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Returns the type of the field based on the class. |
||
| 268 | * The class is stripped by the "CarbonFields" prefix. |
||
| 269 | * Also the "Field" suffix is removed. |
||
| 270 | * Then underscores and backslashes are removed. |
||
| 271 | * |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | public function get_type() { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Activate the field once the container is attached. |
||
| 281 | */ |
||
| 282 | public function activate() { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Activate a field type |
||
| 294 | * @param string $class_name |
||
| 295 | */ |
||
| 296 | public static function activate_field_type( $class_name ) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Prepare the field type for use |
||
| 309 | * Called once per field type when activated |
||
| 310 | */ |
||
| 311 | public static function field_type_activated() {} |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Get array of hierarchy field names |
||
| 315 | * |
||
| 316 | * @return array |
||
| 317 | */ |
||
| 318 | public function get_hierarchy() { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Set array of hierarchy field names |
||
| 324 | */ |
||
| 325 | public function set_hierarchy( $hierarchy ) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Get array of hierarchy indexes |
||
| 331 | * |
||
| 332 | * @return array |
||
| 333 | */ |
||
| 334 | public function get_hierarchy_index() { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Set array of hierarchy indexes |
||
| 340 | */ |
||
| 341 | public function set_hierarchy_index( $hierarchy_index ) { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Return whether the field is a root field and holds a single value |
||
| 348 | * |
||
| 349 | * @return bool |
||
| 350 | */ |
||
| 351 | public function is_simple_root_field() { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Perform instance initialization |
||
| 366 | */ |
||
| 367 | public function init() {} |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Instance initialization when in the admin area |
||
| 371 | * Called during field boot |
||
| 372 | */ |
||
| 373 | public function admin_init() {} |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Enqueue scripts and styles in admin |
||
| 377 | * Called once per field type |
||
| 378 | */ |
||
| 379 | public static function admin_enqueue_scripts() {} |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Get value from datastore |
||
| 383 | * |
||
| 384 | * @param bool $fallback_to_default |
||
| 385 | * @return mixed |
||
| 386 | */ |
||
| 387 | protected function get_value_from_datastore( $fallback_to_default = true ) { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Load value from datastore |
||
| 404 | */ |
||
| 405 | public function load() { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Save value to storage |
||
| 412 | */ |
||
| 413 | public function save() { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Delete value from storage |
||
| 428 | */ |
||
| 429 | public function delete() { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Load the field value from an input array based on it's name |
||
| 435 | * |
||
| 436 | * @param array $input Array of field names and values. |
||
| 437 | */ |
||
| 438 | public function set_value_from_input( $input ) { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Return whether the datastore instance is the default one or has been overriden |
||
| 448 | * |
||
| 449 | * @return boolean |
||
| 450 | */ |
||
| 451 | public function has_default_datastore() { |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Get the DataStore instance |
||
| 457 | * |
||
| 458 | * @return Datastore_Interface $datastore |
||
| 459 | */ |
||
| 460 | public function get_datastore() { |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Set datastore instance |
||
| 466 | * |
||
| 467 | * @param Datastore_Interface $datastore |
||
| 468 | * @return object $this |
||
| 469 | */ |
||
| 470 | public function set_datastore( Datastore_Interface $datastore, $set_as_default = false ) { |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Return the type of the container this field is in |
||
| 481 | * |
||
| 482 | * @return string |
||
| 483 | */ |
||
| 484 | public function get_context() { |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Assign the type of the container this field is in |
||
| 490 | * |
||
| 491 | * @param string |
||
| 492 | * @return object $this |
||
| 493 | */ |
||
| 494 | public function set_context( $context ) { |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Get the Value_Set object |
||
| 501 | * |
||
| 502 | * @return Value_Set |
||
| 503 | */ |
||
| 504 | public function get_value_set() { |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Set the Value_Set object |
||
| 513 | * |
||
| 514 | * @param Value_Set $value_set |
||
| 515 | */ |
||
| 516 | public function set_value_set( $value_set ) { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Alias for $this->get_value_set()->get(); with fallback to default value |
||
| 522 | * |
||
| 523 | * @return mixed |
||
| 524 | */ |
||
| 525 | public function get_value() { |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Alias for $this->get_value_set()->get_set(); with fallback to default value |
||
| 534 | * |
||
| 535 | * @return array<array> |
||
| 536 | */ |
||
| 537 | public function get_full_value() { |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Return a differently formatted value for end-users |
||
| 546 | * |
||
| 547 | * @return mixed |
||
| 548 | */ |
||
| 549 | public function get_formatted_value() { |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Alias for $this->get_value_set()->set( $value ); |
||
| 555 | */ |
||
| 556 | public function set_value( $value ) { |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Clear the field value to a blank one (but not the default one) |
||
| 562 | */ |
||
| 563 | public function clear_value() { |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Get default field value |
||
| 569 | * |
||
| 570 | * @return mixed |
||
| 571 | */ |
||
| 572 | public function get_default_value() { |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Set default field value |
||
| 578 | * |
||
| 579 | * @param mixed $default_value |
||
| 580 | */ |
||
| 581 | public function set_default_value( $default_value ) { |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Return the field base name. |
||
| 588 | * |
||
| 589 | * @return string |
||
| 590 | */ |
||
| 591 | public function get_base_name() { |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Set field base name as defined in the container. |
||
| 597 | */ |
||
| 598 | public function set_base_name( $name ) { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Return the field name |
||
| 604 | * |
||
| 605 | * @return string |
||
| 606 | */ |
||
| 607 | public function get_name() { |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Set field name. |
||
| 613 | * Use only if you are completely aware of what you are doing. |
||
| 614 | * |
||
| 615 | * @param string $name Field name, either sanitized or not |
||
| 616 | */ |
||
| 617 | public function set_name( $name ) { |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Return the field name prefix |
||
| 637 | * |
||
| 638 | * @return string |
||
| 639 | */ |
||
| 640 | public function get_name_prefix() { |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Set field name prefix |
||
| 646 | * Use only if you are completely aware of what you are doing. |
||
| 647 | * |
||
| 648 | * @param string $name_prefix |
||
| 649 | */ |
||
| 650 | public function set_name_prefix( $name_prefix ) { |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Return field label. |
||
| 662 | * |
||
| 663 | * @return string |
||
| 664 | */ |
||
| 665 | public function get_label() { |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Set field label. |
||
| 671 | * |
||
| 672 | * @param string $label If null, the label will be generated from the field name |
||
| 673 | */ |
||
| 674 | View Code Duplication | public function set_label( $label ) { |
|
| 689 | |||
| 690 | /** |
||
| 691 | * Return the field help text |
||
| 692 | * |
||
| 693 | * @return object $this |
||
| 694 | */ |
||
| 695 | public function get_help_text() { |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Set additional text to be displayed during field render, |
||
| 701 | * containing information and guidance for the user |
||
| 702 | * |
||
| 703 | * @return object $this |
||
| 704 | */ |
||
| 705 | public function set_help_text( $help_text ) { |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Alias for set_help_text() |
||
| 712 | * |
||
| 713 | * @see set_help_text() |
||
| 714 | * @return object $this |
||
| 715 | */ |
||
| 716 | public function help_text( $help_text ) { |
||
| 719 | |||
| 720 | /** |
||
| 721 | * Return whether or not this value should be auto loaded. |
||
| 722 | * |
||
| 723 | * @return bool |
||
| 724 | */ |
||
| 725 | public function get_autoload() { |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Whether or not this value should be auto loaded. Applicable to theme options only. |
||
| 731 | * |
||
| 732 | * @param bool $autoload |
||
| 733 | * @return object $this |
||
| 734 | */ |
||
| 735 | public function set_autoload( $autoload ) { |
||
| 739 | |||
| 740 | /** |
||
| 741 | * Return whether or not this field should be lazyloaded. |
||
| 742 | * |
||
| 743 | * @return bool |
||
| 744 | */ |
||
| 745 | public function get_lazyload() { |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Whether or not this field will be initialized when the field is in the viewport (visible). |
||
| 751 | * |
||
| 752 | * @param bool $lazyload |
||
| 753 | * @return object $this |
||
| 754 | */ |
||
| 755 | public function set_lazyload( $lazyload ) { |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Get the field width. |
||
| 762 | * |
||
| 763 | * @return int $width |
||
| 764 | */ |
||
| 765 | public function get_width() { |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Set the field width. |
||
| 771 | * |
||
| 772 | * @param int $width |
||
| 773 | * @return object $this |
||
| 774 | */ |
||
| 775 | public function set_width( $width ) { |
||
| 779 | |||
| 780 | /** |
||
| 781 | * Get custom CSS classes. |
||
| 782 | * |
||
| 783 | * @return array<string> |
||
| 784 | */ |
||
| 785 | public function get_classes() { |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Set CSS classes that the container should use. |
||
| 791 | * |
||
| 792 | * @param string|array<string> $classes |
||
| 793 | * @return object $this |
||
| 794 | */ |
||
| 795 | public function set_classes( $classes ) { |
||
| 799 | |||
| 800 | /** |
||
| 801 | * Whether this field is mandatory for the user |
||
| 802 | * |
||
| 803 | * @param bool $required |
||
| 804 | * @return object $this |
||
| 805 | */ |
||
| 806 | public function set_required( $required = true ) { |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Return whether this field is mandatory for the user |
||
| 813 | * |
||
| 814 | * @return bool |
||
| 815 | */ |
||
| 816 | public function is_required() { |
||
| 819 | |||
| 820 | /** |
||
| 821 | * HTML id attribute getter. |
||
| 822 | * @return string |
||
| 823 | */ |
||
| 824 | public function get_id() { |
||
| 827 | |||
| 828 | /** |
||
| 829 | * HTML id attribute setter |
||
| 830 | * @param string $id |
||
| 831 | */ |
||
| 832 | public function set_id( $id ) { |
||
| 835 | |||
| 836 | /** |
||
| 837 | * Set the field visibility conditional logic. |
||
| 838 | * |
||
| 839 | * @param array |
||
| 840 | */ |
||
| 841 | public function set_conditional_logic( $rules ) { |
||
| 846 | |||
| 847 | /** |
||
| 848 | * Get the conditional logic rules |
||
| 849 | * |
||
| 850 | * @return array |
||
| 851 | */ |
||
| 852 | public function get_conditional_logic() { |
||
| 855 | |||
| 856 | /** |
||
| 857 | * Validate and parse the conditional logic rules. |
||
| 858 | * |
||
| 859 | * @param array $rules |
||
| 860 | * @return array |
||
| 861 | */ |
||
| 862 | protected function parse_conditional_rules( $rules ) { |
||
| 915 | |||
| 916 | /** |
||
| 917 | * Set the REST visibility of the field |
||
| 918 | * |
||
| 919 | * @param bool $visible |
||
| 920 | */ |
||
| 921 | public function set_visible_in_rest_api( $visible ) { |
||
| 924 | |||
| 925 | /** |
||
| 926 | * Get the REST visibility of the field |
||
| 927 | * |
||
| 928 | * @return bool |
||
| 929 | */ |
||
| 930 | public function get_visible_in_rest_api() { |
||
| 933 | |||
| 934 | /** |
||
| 935 | * Configuration function for setting the field visibility in the response of the requests to the REST API |
||
| 936 | * |
||
| 937 | * @param bool $visible |
||
| 938 | * @return Field $this |
||
| 939 | */ |
||
| 940 | public function show_in_rest( $visible = true ) { |
||
| 944 | |||
| 945 | /** |
||
| 946 | * Returns an array that holds the field data, suitable for JSON representation. |
||
| 947 | * |
||
| 948 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
| 949 | * @return array |
||
| 950 | */ |
||
| 951 | public function to_json( $load ) { |
||
| 975 | |||
| 976 | /** |
||
| 977 | * Hook administration scripts. |
||
| 978 | */ |
||
| 979 | public static function admin_hook_scripts() { |
||
| 982 | |||
| 983 | /** |
||
| 984 | * Hook administration styles. |
||
| 985 | */ |
||
| 986 | public static function admin_hook_styles() { |
||
| 989 | } |
||
| 990 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.