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_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 WP_Fields_API_Field, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class WP_Fields_API_Field { |
||
11 | |||
12 | /** |
||
13 | * @access public |
||
14 | * @var string |
||
15 | */ |
||
16 | public $id = ''; |
||
17 | |||
18 | /** |
||
19 | * Object type. |
||
20 | * |
||
21 | * @access public |
||
22 | * @var string |
||
23 | */ |
||
24 | public $object_type = ''; |
||
25 | |||
26 | /** |
||
27 | * Object name (for post types and taxonomies). |
||
28 | * |
||
29 | * @access public |
||
30 | * @var string |
||
31 | */ |
||
32 | public $object_name = ''; |
||
33 | |||
34 | /** |
||
35 | * Capability required to edit this field. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | public $capability = ''; |
||
40 | |||
41 | /** |
||
42 | * Theme feature support for the field. |
||
43 | * |
||
44 | * @access public |
||
45 | * @var string|array |
||
46 | */ |
||
47 | public $theme_supports = ''; |
||
48 | |||
49 | /** |
||
50 | * Default value for field |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | public $default = ''; |
||
55 | |||
56 | /** |
||
57 | * Server-side sanitization callback for the field's value. |
||
58 | * |
||
59 | * @var callback |
||
60 | */ |
||
61 | public $sanitize_callback = ''; |
||
62 | public $sanitize_js_callback = ''; |
||
63 | |||
64 | protected $id_data = array(); |
||
65 | |||
66 | /** |
||
67 | * Capabilities Callback. |
||
68 | * |
||
69 | * @access public |
||
70 | * |
||
71 | * @see WP_Fields_API_Field::check_capabilities() |
||
72 | * |
||
73 | * @var callable Callback is called with one argument, the instance of |
||
74 | * WP_Fields_API_Field, and returns bool to indicate whether |
||
75 | * the field has capabilities to be used. |
||
76 | */ |
||
77 | public $capabilities_callback = ''; |
||
78 | |||
79 | /** |
||
80 | * Value Callback. |
||
81 | * |
||
82 | * @access public |
||
83 | * |
||
84 | * @see WP_Fields_API_Field::value() |
||
85 | * |
||
86 | * @var callable Callback is called with two arguments, the item ID and the instance of |
||
87 | * WP_Fields_API_Field. It returns a string for the value to use. |
||
88 | */ |
||
89 | public $value_callback = ''; |
||
90 | |||
91 | /** |
||
92 | * Pre-Update Value Callback. |
||
93 | * |
||
94 | * @access public |
||
95 | * |
||
96 | * @see WP_Fields_API_Field::save() |
||
97 | * |
||
98 | * @var callable Callback is called with three arguments, the value being saved, the item ID, and the instance of |
||
99 | * WP_Fields_API_Field. It returns a string of the value to save. |
||
100 | */ |
||
101 | public $pre_update_value_callback = ''; |
||
102 | |||
103 | /** |
||
104 | * Update Value Callback. |
||
105 | * |
||
106 | * @access public |
||
107 | * |
||
108 | * @see WP_Fields_API_Field::update() |
||
109 | * |
||
110 | * @var callable Callback is called with three arguments, the value being saved, the item ID, and the instance of |
||
111 | * WP_Fields_API_Field. |
||
112 | */ |
||
113 | public $update_value_callback = ''; |
||
114 | |||
115 | /** |
||
116 | * Constructor. |
||
117 | * |
||
118 | * Parameters are not set to maintain PHP overloading compatibility (strict standards) |
||
119 | * |
||
120 | * @return WP_Fields_API_Field $field |
||
|
|||
121 | */ |
||
122 | public function __construct() { |
||
129 | |||
130 | /** |
||
131 | * Secondary constructor; Any supplied $args override class property defaults. |
||
132 | * |
||
133 | * @param string $object_type Object type. |
||
134 | * @param string $id A specific ID of the field. Can be a |
||
135 | * theme mod or option name. |
||
136 | * @param array $args Field arguments. |
||
137 | * |
||
138 | * @return WP_Fields_API_Field $field |
||
139 | */ |
||
140 | public function init( $object_type, $id, $args = array() ) { |
||
180 | |||
181 | /** |
||
182 | * Check user capabilities and theme supports, and then save |
||
183 | * the value of the field. |
||
184 | * |
||
185 | * @param mixed $value The value to save. |
||
186 | * @param int $item_id The Item ID. |
||
187 | * |
||
188 | * @return false|mixed False if cap check fails or value isn't set. |
||
189 | */ |
||
190 | public function save() { |
||
220 | |||
221 | /** |
||
222 | * Sanitize an input. |
||
223 | * |
||
224 | * @param mixed $value The value to sanitize. |
||
225 | * |
||
226 | * @return mixed Null if an input isn't valid, otherwise the sanitized value. |
||
227 | */ |
||
228 | public function sanitize( $value ) { |
||
241 | |||
242 | /** |
||
243 | * Save the value of the field, using the related API. |
||
244 | * |
||
245 | * @param mixed $value The value to update. |
||
246 | * @param int $item_id Item ID. |
||
247 | * |
||
248 | * @return mixed The result of saving the value. |
||
249 | */ |
||
250 | protected function update( $value ) { |
||
290 | |||
291 | /** |
||
292 | * Update the theme mod from the value of the parameter. |
||
293 | * |
||
294 | * @param mixed $value The value to update. |
||
295 | * |
||
296 | * @return null |
||
297 | */ |
||
298 | View Code Duplication | protected function _update_theme_mod( $value ) { |
|
320 | |||
321 | /** |
||
322 | * Update the option from the value of the field. |
||
323 | * |
||
324 | * @param mixed $value The value to update. |
||
325 | * |
||
326 | * @return bool|null The result of saving the value. |
||
327 | */ |
||
328 | View Code Duplication | protected function _update_option( $value ) { |
|
350 | |||
351 | /** |
||
352 | * Update the meta from the value of the field. |
||
353 | * |
||
354 | * @param string $meta_type The meta type. |
||
355 | * @param mixed $value The value to update. |
||
356 | * @param int $item_id Item ID. |
||
357 | * |
||
358 | * @return bool|null The result of saving the value. |
||
359 | */ |
||
360 | protected function _update_meta( $meta_type, $value, $item_id = 0 ) { |
||
382 | |||
383 | /** |
||
384 | * Fetch the value of the field. |
||
385 | * |
||
386 | * @param int $item_id (optional) The Item ID. |
||
387 | * |
||
388 | * @return mixed The value. |
||
389 | */ |
||
390 | public function value() { |
||
434 | |||
435 | /** |
||
436 | * Get value from meta / object |
||
437 | * |
||
438 | * @param int $item_id |
||
439 | * |
||
440 | * @return mixed|null |
||
441 | */ |
||
442 | public function get_object_value( $item_id ) { |
||
482 | |||
483 | /** |
||
484 | * Get value from option / theme_mod |
||
485 | * |
||
486 | * @return mixed|void |
||
487 | */ |
||
488 | public function get_option_value() { |
||
516 | |||
517 | /** |
||
518 | * Sanitize the field's value for use in JavaScript. |
||
519 | * |
||
520 | * @return mixed The requested escaped value. |
||
521 | */ |
||
522 | public function js_value() { |
||
543 | |||
544 | /** |
||
545 | * Validate user capabilities whether the theme supports the field. |
||
546 | * |
||
547 | * @return bool False if theme doesn't support the section or user can't change section, otherwise true. |
||
548 | */ |
||
549 | View Code Duplication | public function check_capabilities() { |
|
568 | |||
569 | /** |
||
570 | * Multidimensional helper function. |
||
571 | * |
||
572 | * @param $root |
||
573 | * @param $keys |
||
574 | * @param bool $create Default is false. |
||
575 | * |
||
576 | * @return null|array Keys are 'root', 'node', and 'key'. |
||
577 | */ |
||
578 | final protected function multidimensional( &$root, $keys, $create = false ) { |
||
624 | |||
625 | /** |
||
626 | * Will attempt to replace a specific value in a multidimensional array. |
||
627 | * |
||
628 | * @param $root |
||
629 | * @param $keys |
||
630 | * @param mixed $value The value to update. |
||
631 | * |
||
632 | * @return |
||
633 | */ |
||
634 | View Code Duplication | final protected function multidimensional_replace( $root, $keys, $value ) { |
|
652 | |||
653 | /** |
||
654 | * Will attempt to fetch a specific value from a multidimensional array. |
||
655 | * |
||
656 | * @param $root |
||
657 | * @param $keys |
||
658 | * @param mixed $default A default value which is used as a fallback. Default is null. |
||
659 | * |
||
660 | * @return mixed The requested value or the default value. |
||
661 | */ |
||
662 | View Code Duplication | final protected function multidimensional_get( $root, $keys, $default = null ) { |
|
680 | |||
681 | /** |
||
682 | * Will attempt to check if a specific value in a multidimensional array is set. |
||
683 | * |
||
684 | * @param $root |
||
685 | * @param $keys |
||
686 | * |
||
687 | * @return bool True if value is set, false if not. |
||
688 | */ |
||
689 | final protected function multidimensional_isset( $root, $keys ) { |
||
696 | } |
||
697 | |||
720 | } |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.