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 | * Constructor. |
||
68 | * |
||
69 | * Parameters are not set to maintain PHP overloading compatibility (strict standards) |
||
70 | * |
||
71 | * @return WP_Fields_API_Field $field |
||
|
|||
72 | */ |
||
73 | public function __construct() { |
||
80 | |||
81 | /** |
||
82 | * Secondary constructor; Any supplied $args override class property defaults. |
||
83 | * |
||
84 | * @param string $object_type Object type. |
||
85 | * @param string $id A specific ID of the field. Can be a |
||
86 | * theme mod or option name. |
||
87 | * @param array $args Field arguments. |
||
88 | * |
||
89 | * @return WP_Fields_API_Field $field |
||
90 | */ |
||
91 | public function init( $object_type, $id, $args = array() ) { |
||
131 | |||
132 | /** |
||
133 | * Check user capabilities and theme supports, and then save |
||
134 | * the value of the field. |
||
135 | * |
||
136 | * @param mixed $value The value to save. |
||
137 | * @param int $item_id The Item ID. |
||
138 | * |
||
139 | * @return false|mixed False if cap check fails or value isn't set. |
||
140 | */ |
||
141 | public function save() { |
||
165 | |||
166 | /** |
||
167 | * Sanitize an input. |
||
168 | * |
||
169 | * @param mixed $value The value to sanitize. |
||
170 | * |
||
171 | * @return mixed Null if an input isn't valid, otherwise the sanitized value. |
||
172 | */ |
||
173 | public function sanitize( $value ) { |
||
186 | |||
187 | /** |
||
188 | * Save the value of the field, using the related API. |
||
189 | * |
||
190 | * @param mixed $value The value to update. |
||
191 | * @param int $item_id Item ID. |
||
192 | * |
||
193 | * @return mixed The result of saving the value. |
||
194 | */ |
||
195 | protected function update( $value ) { |
||
232 | |||
233 | /** |
||
234 | * Update the theme mod from the value of the parameter. |
||
235 | * |
||
236 | * @param mixed $value The value to update. |
||
237 | * |
||
238 | * @return null |
||
239 | */ |
||
240 | View Code Duplication | protected function _update_theme_mod( $value ) { |
|
262 | |||
263 | /** |
||
264 | * Update the option from the value of the field. |
||
265 | * |
||
266 | * @param mixed $value The value to update. |
||
267 | * |
||
268 | * @return bool|null The result of saving the value. |
||
269 | */ |
||
270 | View Code Duplication | protected function _update_option( $value ) { |
|
292 | |||
293 | /** |
||
294 | * Update the meta from the value of the field. |
||
295 | * |
||
296 | * @param string $meta_type The meta type. |
||
297 | * @param mixed $value The value to update. |
||
298 | * @param int $item_id Item ID. |
||
299 | * |
||
300 | * @return bool|null The result of saving the value. |
||
301 | */ |
||
302 | protected function _update_meta( $meta_type, $value, $item_id = 0 ) { |
||
324 | |||
325 | /** |
||
326 | * Fetch the value of the field. |
||
327 | * |
||
328 | * @param int $item_id (optional) The Item ID. |
||
329 | * |
||
330 | * @return mixed The value. |
||
331 | */ |
||
332 | public function value() { |
||
371 | |||
372 | /** |
||
373 | * Get value from meta / object |
||
374 | * |
||
375 | * @param int $item_id |
||
376 | * |
||
377 | * @return mixed|null |
||
378 | */ |
||
379 | public function get_object_value( $item_id ) { |
||
415 | |||
416 | /** |
||
417 | * Get value from option / theme_mod |
||
418 | * |
||
419 | * @return mixed|void |
||
420 | */ |
||
421 | public function get_option_value() { |
||
449 | |||
450 | /** |
||
451 | * Sanitize the field's value for use in JavaScript. |
||
452 | * |
||
453 | * @return mixed The requested escaped value. |
||
454 | */ |
||
455 | public function js_value() { |
||
476 | |||
477 | /** |
||
478 | * Validate user capabilities whether the theme supports the field. |
||
479 | * |
||
480 | * @return bool False if theme doesn't support the section or user can't change section, otherwise true. |
||
481 | */ |
||
482 | View Code Duplication | public function check_capabilities() { |
|
495 | |||
496 | /** |
||
497 | * Multidimensional helper function. |
||
498 | * |
||
499 | * @param $root |
||
500 | * @param $keys |
||
501 | * @param bool $create Default is false. |
||
502 | * |
||
503 | * @return null|array Keys are 'root', 'node', and 'key'. |
||
504 | */ |
||
505 | final protected function multidimensional( &$root, $keys, $create = false ) { |
||
551 | |||
552 | /** |
||
553 | * Will attempt to replace a specific value in a multidimensional array. |
||
554 | * |
||
555 | * @param $root |
||
556 | * @param $keys |
||
557 | * @param mixed $value The value to update. |
||
558 | * |
||
559 | * @return |
||
560 | */ |
||
561 | View Code Duplication | final protected function multidimensional_replace( $root, $keys, $value ) { |
|
579 | |||
580 | /** |
||
581 | * Will attempt to fetch a specific value from a multidimensional array. |
||
582 | * |
||
583 | * @param $root |
||
584 | * @param $keys |
||
585 | * @param mixed $default A default value which is used as a fallback. Default is null. |
||
586 | * |
||
587 | * @return mixed The requested value or the default value. |
||
588 | */ |
||
589 | View Code Duplication | final protected function multidimensional_get( $root, $keys, $default = null ) { |
|
607 | |||
608 | /** |
||
609 | * Will attempt to check if a specific value in a multidimensional array is set. |
||
610 | * |
||
611 | * @param $root |
||
612 | * @param $keys |
||
613 | * |
||
614 | * @return bool True if value is set, false if not. |
||
615 | */ |
||
616 | final protected function multidimensional_isset( $root, $keys ) { |
||
623 | } |
||
624 | |||
647 | } |
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.