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 WP_Fields_API_Control 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 WP_Fields_API_Control, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class WP_Fields_API_Control { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Incremented with each new class instantiation, then stored in $instance_number. |
||
| 14 | * |
||
| 15 | * Used when sorting two instances whose priorities are equal. |
||
| 16 | * |
||
| 17 | * @access protected |
||
| 18 | * @var int |
||
| 19 | */ |
||
| 20 | protected static $instance_count = 0; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Order in which this instance was created in relation to other instances. |
||
| 24 | * |
||
| 25 | * @access public |
||
| 26 | * @var int |
||
| 27 | */ |
||
| 28 | public $instance_number = 0; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Unique identifier. |
||
| 32 | * |
||
| 33 | * @access public |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | public $id = ''; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Object type. |
||
| 40 | * |
||
| 41 | * @access public |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | public $object_type = ''; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Object name (for post types and taxonomies). |
||
| 48 | * |
||
| 49 | * @access public |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | public $object_name = ''; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Item ID of current item passed to WP_Fields_API_Field for value() |
||
| 56 | * |
||
| 57 | * @access public |
||
| 58 | * @var int|string |
||
| 59 | */ |
||
| 60 | public $item_id; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * All fields tied to the control. |
||
| 64 | * |
||
| 65 | * @access public |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | public $fields = array(); |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The primary field for the control (if there is one). |
||
| 72 | * |
||
| 73 | * @access public |
||
| 74 | * @var string|WP_Fields_API_Field |
||
| 75 | */ |
||
| 76 | public $field = 'default'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * The primary screen for the control (if there is one). |
||
| 80 | * |
||
| 81 | * @access public |
||
| 82 | * @var string|WP_Fields_API_Section |
||
| 83 | */ |
||
| 84 | public $section = ''; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The primary screen for the control (if there is one). |
||
| 88 | * |
||
| 89 | * @access public |
||
| 90 | * @var string|WP_Fields_API_Screen |
||
| 91 | */ |
||
| 92 | public $screen = ''; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @access public |
||
| 96 | * @var int |
||
| 97 | */ |
||
| 98 | public $priority = 10; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @access public |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | public $label = ''; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @access public |
||
| 108 | * @var string |
||
| 109 | */ |
||
| 110 | public $description = ''; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @access public |
||
| 114 | * @var array |
||
| 115 | */ |
||
| 116 | public $input_attrs = array(); |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @access public |
||
| 120 | * @var string |
||
| 121 | */ |
||
| 122 | public $type = 'text'; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Callback. |
||
| 126 | * |
||
| 127 | * @access public |
||
| 128 | * |
||
| 129 | * @see WP_Fields_API_Control::active() |
||
| 130 | * |
||
| 131 | * @var callable Callback is called with one argument, the instance of |
||
| 132 | * WP_Fields_API_Control, and returns bool to indicate whether |
||
| 133 | * the control is active (such as it relates to the URL |
||
| 134 | * currently being previewed). |
||
| 135 | */ |
||
| 136 | public $active_callback = ''; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Capabilities Callback. |
||
| 140 | * |
||
| 141 | * @access public |
||
| 142 | * |
||
| 143 | * @see WP_Fields_API_Control::check_capabilities() |
||
| 144 | * |
||
| 145 | * @var callable Callback is called with one argument, the instance of |
||
| 146 | * WP_Fields_API_Control, and returns bool to indicate whether |
||
| 147 | * the control has capabilities to be used. |
||
| 148 | */ |
||
| 149 | public $capabilities_callback = ''; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Constructor. |
||
| 153 | * |
||
| 154 | * Parameters are not set to maintain PHP overloading compatibility (strict standards) |
||
| 155 | */ |
||
| 156 | public function __construct() { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Secondary constructor; Any supplied $args override class property defaults. |
||
| 166 | * |
||
| 167 | * @param string $object_type Object type. |
||
| 168 | * @param string $id A specific ID of the control. |
||
| 169 | * @param array $args Control arguments. |
||
| 170 | */ |
||
| 171 | public function init( $object_type, $id, $args = array() ) { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Setup the choices values and set the choices property to allow dynamic building |
||
| 233 | */ |
||
| 234 | public function setup_choices() { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get the choices values from the choices property and allow dynamic building |
||
| 246 | */ |
||
| 247 | public function choices() { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Enqueue control related scripts/styles. |
||
| 255 | */ |
||
| 256 | public function enqueue() {} |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Check whether control is active to current Fields API preview. |
||
| 260 | * |
||
| 261 | * @access public |
||
| 262 | * |
||
| 263 | * @return bool Whether the control is active to the current preview. |
||
| 264 | */ |
||
| 265 | View Code Duplication | final public function active() { |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Default callback used when invoking WP_Fields_API_Control::active(). |
||
| 288 | * |
||
| 289 | * Subclasses can override this with their specific logic, or they may |
||
| 290 | * provide an 'active_callback' argument to the constructor. |
||
| 291 | * |
||
| 292 | * @access public |
||
| 293 | * |
||
| 294 | * @return bool Always true. |
||
| 295 | */ |
||
| 296 | public function active_callback() { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Fetch a field's value. |
||
| 304 | * Grabs the main field by default. |
||
| 305 | * |
||
| 306 | * @param string $field_key |
||
| 307 | * @return mixed The requested field's value, if the field exists. |
||
| 308 | */ |
||
| 309 | final public function value( $field_key = 'default' ) { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Get the data to export to the client via JSON. |
||
| 326 | * |
||
| 327 | * @return array Array of parameters passed to the JavaScript. |
||
| 328 | */ |
||
| 329 | public function json() { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Check if the theme supports the control and check user capabilities. |
||
| 349 | * |
||
| 350 | * @return bool False if theme doesn't support the control or user doesn't have the required permissions, otherwise true. |
||
| 351 | */ |
||
| 352 | final public function check_capabilities() { |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Get the control's content for insertion. |
||
| 386 | * |
||
| 387 | * @return string Contents of the control. |
||
| 388 | */ |
||
| 389 | final public function get_content() { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Check capabilities and render the control. |
||
| 403 | * |
||
| 404 | * @uses WP_Fields_API_Control::render() |
||
| 405 | */ |
||
| 406 | final public function maybe_render() { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Renders the control wrapper and calls $this->render_content() for the internals. |
||
| 435 | * |
||
| 436 | */ |
||
| 437 | protected function render() { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Get the data link attribute for a field. |
||
| 450 | * |
||
| 451 | * |
||
| 452 | * @param string $field_key |
||
| 453 | * @return string Data link parameter, if $field_key is a valid field, empty string otherwise. |
||
| 454 | */ |
||
| 455 | public function get_link( $field_key = 'default' ) { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Render the data link attribute for the control's input element. |
||
| 467 | * |
||
| 468 | * @uses WP_Fields_API_Control::get_link() |
||
| 469 | * |
||
| 470 | * @param string $field_key |
||
| 471 | */ |
||
| 472 | public function link( $field_key = 'default' ) { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Render the custom attributes for the control's input element. |
||
| 480 | * |
||
| 481 | * @access public |
||
| 482 | */ |
||
| 483 | public function input_attrs() { |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Render the control's content. |
||
| 493 | * |
||
| 494 | * Allows the content to be overriden without having to rewrite the wrapper in $this->render(). |
||
| 495 | * |
||
| 496 | * Supports basic input types `text`, `checkbox`, `textarea`, `radio`, `select` and `dropdown-pages`. |
||
| 497 | * Additional input types such as `email`, `url`, `number`, `hidden` and `date` are supported implicitly. |
||
| 498 | * |
||
| 499 | * Control content can alternately be rendered in JS. See {@see WP_Fields_API_Control::print_template()}. |
||
| 500 | */ |
||
| 501 | View Code Duplication | public function render_content() { |
|
| 516 | |||
| 517 | /** |
||
| 518 | * Render the control's JS template. |
||
| 519 | * |
||
| 520 | * This function is only run for control types that have been registered with |
||
| 521 | * {@see WP_Fields_API::register_control_type()}. |
||
| 522 | * |
||
| 523 | * In the future, this will also print the template for the control's container |
||
| 524 | * element and be override-able. |
||
| 525 | */ |
||
| 526 | public function print_template() { |
||
| 535 | |||
| 536 | /** |
||
| 537 | * An Underscore (JS) template for this control's content (but not its container). |
||
| 538 | * |
||
| 539 | * Class variables for this control class are available in the `data` JS object; |
||
| 540 | * export custom variables by overriding {@see WP_Fields_API_Control::to_json()}. |
||
| 541 | * |
||
| 542 | * @see WP_Fields_API_Control::print_template() |
||
| 543 | */ |
||
| 544 | public function content_template() { |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Magic method for handling backwards compatible properties / methods |
||
| 552 | * |
||
| 553 | * @param string $name Parameter name |
||
| 554 | * |
||
| 555 | * @return mixed|null |
||
| 556 | */ |
||
| 557 | public function &__get( $name ) { |
||
| 569 | |||
| 570 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..