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 |
||
14 | class Field implements Datastore_Holder_Interface { |
||
15 | /** |
||
16 | * Stores all the field Backbone templates |
||
17 | * |
||
18 | * @see factory() |
||
19 | * @see add_template() |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $templates = array(); |
||
23 | |||
24 | /** |
||
25 | * Globally unique field identificator. Generated randomly |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $id; |
||
30 | |||
31 | /** |
||
32 | * Stores the initial <kbd>$type</kbd> variable passed to the <code>factory()</code> method |
||
33 | * |
||
34 | * @see factory |
||
35 | * @var string |
||
36 | */ |
||
37 | public $type; |
||
38 | |||
39 | /** |
||
40 | * Field value |
||
41 | * |
||
42 | * @var mixed |
||
43 | */ |
||
44 | protected $value; |
||
45 | |||
46 | /** |
||
47 | * Default field value |
||
48 | * |
||
49 | * @var mixed |
||
50 | */ |
||
51 | protected $default_value; |
||
52 | |||
53 | /** |
||
54 | * Sanitized field name used as input name attribute during field render |
||
55 | * |
||
56 | * @see factory() |
||
57 | * @see set_name() |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $name; |
||
61 | |||
62 | /** |
||
63 | * The base field name which is used in the container. |
||
64 | * |
||
65 | * @see set_base_name() |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $base_name; |
||
69 | |||
70 | /** |
||
71 | * Field name used as label during field render |
||
72 | * |
||
73 | * @see factory() |
||
74 | * @see set_label() |
||
75 | * @var string |
||
76 | */ |
||
77 | protected $label; |
||
78 | |||
79 | /** |
||
80 | * Additional text containing information and guidance for the user |
||
81 | * |
||
82 | * @see help_text() |
||
83 | * @var string |
||
84 | */ |
||
85 | protected $help_text; |
||
86 | |||
87 | /** |
||
88 | * Field DataStore instance to which save, load and delete calls are delegated |
||
89 | * |
||
90 | * @see set_datastore() |
||
91 | * @see get_datastore() |
||
92 | * @var Datastore_Interface |
||
93 | */ |
||
94 | protected $datastore; |
||
95 | |||
96 | /** |
||
97 | * Flag whether the datastore is the default one or replaced with a custom one |
||
98 | * |
||
99 | * @see set_datastore() |
||
100 | * @see get_datastore() |
||
101 | * @var object |
||
102 | */ |
||
103 | protected $has_default_datastore = true; |
||
1 ignored issue
–
show
|
|||
104 | |||
105 | /** |
||
106 | * The type of the container this field is in |
||
107 | * |
||
108 | * @see get_context() |
||
109 | * @var string |
||
110 | */ |
||
111 | protected $context; |
||
112 | |||
113 | /** |
||
114 | * Whether or not this value should be auto loaded. Applicable to theme options only. |
||
115 | * |
||
116 | * @see set_autoload() |
||
117 | * @var bool |
||
118 | **/ |
||
119 | protected $autoload = false; |
||
120 | |||
121 | /** |
||
122 | * Whether or not this field will be initialized when the field is in the viewport (visible). |
||
123 | * |
||
124 | * @see set_lazyload() |
||
125 | * @var bool |
||
126 | **/ |
||
127 | protected $lazyload = false; |
||
128 | |||
129 | /** |
||
130 | * The width of the field. |
||
131 | * |
||
132 | * @see set_width() |
||
133 | * @var int |
||
134 | **/ |
||
135 | protected $width = 0; |
||
136 | |||
137 | /** |
||
138 | * Custom CSS classes. |
||
139 | * |
||
140 | * @see add_class() |
||
141 | * @var array |
||
142 | **/ |
||
143 | protected $classes = array(); |
||
144 | |||
145 | /** |
||
146 | * Whether or not this field is required. |
||
147 | * |
||
148 | * @see set_required() |
||
149 | * @var bool |
||
150 | **/ |
||
151 | protected $required = false; |
||
152 | |||
153 | /** |
||
154 | * Prefix to be prepended to the field name during load, save, delete and <strong>render</strong> |
||
155 | * |
||
156 | * @var string |
||
157 | **/ |
||
158 | protected $name_prefix = '_'; |
||
159 | |||
160 | /** |
||
161 | * Stores the field conditional logic rules. |
||
162 | * |
||
163 | * @var array |
||
164 | **/ |
||
165 | protected $conditional_logic = array(); |
||
166 | |||
167 | /** |
||
168 | * Create a new field of type $type and name $name and label $label. |
||
169 | * |
||
170 | * @param string $type |
||
171 | * @param string $name lower case and underscore-delimited |
||
172 | * @param string $label (optional) Automatically generated from $name if not present |
||
173 | * @return object $field |
||
174 | **/ |
||
175 | 14 | View Code Duplication | public static function factory( $type, $name, $label = null ) { |
195 | |||
196 | /** |
||
197 | * An alias of factory(). |
||
198 | * |
||
199 | * @see Field::factory() |
||
200 | **/ |
||
201 | 14 | public static function make( $type, $name, $label = null ) { |
|
204 | |||
205 | /** |
||
206 | * Create a field from a certain type with the specified label. |
||
207 | * @param string $name Field name |
||
208 | * @param string $label Field label |
||
209 | */ |
||
210 | 11 | View Code Duplication | protected function __construct( $name, $label ) { |
222 | |||
223 | /** |
||
224 | * Boot the field once the container is attached. |
||
225 | **/ |
||
226 | public function boot() { |
||
236 | |||
237 | /** |
||
238 | * Perform instance initialization after calling setup() |
||
239 | **/ |
||
240 | public function init() {} |
||
241 | |||
242 | /** |
||
243 | * Instance initialization when in the admin area. |
||
244 | * Called during field boot. |
||
245 | **/ |
||
246 | public function admin_init() {} |
||
247 | |||
248 | /** |
||
249 | * Enqueue admin scripts. |
||
250 | * Called once per field type. |
||
251 | **/ |
||
252 | public static function admin_enqueue_scripts() {} |
||
253 | |||
254 | /** |
||
255 | * Prints the main Underscore template |
||
256 | **/ |
||
257 | public function template() { } |
||
258 | |||
259 | /** |
||
260 | * Returns all the Backbone templates |
||
261 | * |
||
262 | * @return array |
||
263 | **/ |
||
264 | public function get_templates() { |
||
267 | |||
268 | /** |
||
269 | * Adds a new Backbone template |
||
270 | **/ |
||
271 | public function add_template( $name, $callback ) { |
||
274 | |||
275 | /** |
||
276 | * Delegate load to the field DataStore instance |
||
277 | **/ |
||
278 | public function load() { |
||
285 | |||
286 | /** |
||
287 | * Delegate save to the field DataStore instance |
||
288 | **/ |
||
289 | public function save() { |
||
292 | |||
293 | /** |
||
294 | * Delegate delete to the field DataStore instance |
||
295 | **/ |
||
296 | public function delete() { |
||
299 | |||
300 | /** |
||
301 | * Load the field value from an input array based on it's name |
||
302 | * |
||
303 | * @param array $input (optional) Array of field names and values. Defaults to $_POST |
||
304 | **/ |
||
305 | public function set_value_from_input( $input = null ) { |
||
316 | |||
317 | /** |
||
318 | * Return whether the datastore instance is the default one or has been overriden |
||
319 | * |
||
320 | * @return Datastore_Interface $datastore |
||
321 | **/ |
||
322 | public function has_default_datastore() { |
||
325 | |||
326 | /** |
||
327 | * Assign DataStore instance for use during load, save and delete |
||
328 | * |
||
329 | * @param object $datastore |
||
330 | * @return object $this |
||
331 | **/ |
||
332 | public function set_datastore( Datastore_Interface $datastore, $set_as_default = false ) { |
||
340 | |||
341 | /** |
||
342 | * Return the DataStore instance used by the field |
||
343 | * |
||
344 | * @return object $datastore |
||
345 | **/ |
||
346 | public function get_datastore() { |
||
349 | |||
350 | /** |
||
351 | * Assign the type of the container this field is in |
||
352 | * |
||
353 | * @param string |
||
354 | * @return object $this |
||
355 | **/ |
||
356 | public function set_context( $context ) { |
||
360 | |||
361 | /** |
||
362 | * Return the type of the container this field is in |
||
363 | * |
||
364 | * @return string |
||
365 | **/ |
||
366 | public function get_context() { |
||
369 | |||
370 | /** |
||
371 | * Directly modify the field value |
||
372 | * |
||
373 | * @param mixed $value |
||
374 | **/ |
||
375 | public function set_value( $value ) { |
||
378 | |||
379 | /** |
||
380 | * Set default field value |
||
381 | * |
||
382 | * @param mixed $default_value |
||
383 | **/ |
||
384 | public function set_default_value( $default_value ) { |
||
388 | |||
389 | /** |
||
390 | * Get default field value |
||
391 | * |
||
392 | * @return mixed |
||
393 | **/ |
||
394 | public function get_default_value() { |
||
397 | |||
398 | /** |
||
399 | * Return the field value |
||
400 | * |
||
401 | * @return mixed |
||
402 | **/ |
||
403 | public function get_value() { |
||
406 | |||
407 | /** |
||
408 | * Set field name. |
||
409 | * Use only if you are completely aware of what you are doing. |
||
410 | * |
||
411 | * @param string $name Field name, either sanitized or not |
||
412 | **/ |
||
413 | public function set_name( $name ) { |
||
426 | |||
427 | /** |
||
428 | * Return the field name |
||
429 | * |
||
430 | * @return string |
||
431 | **/ |
||
432 | public function get_name() { |
||
435 | |||
436 | /** |
||
437 | * Set field base name as defined in the container. |
||
438 | **/ |
||
439 | public function set_base_name( $name ) { |
||
442 | |||
443 | /** |
||
444 | * Return the field base name. |
||
445 | * |
||
446 | * @return string |
||
447 | **/ |
||
448 | public function get_base_name() { |
||
451 | |||
452 | /** |
||
453 | * Set field name prefix. Calling this method will update the current field |
||
454 | * name and the conditional logic fields. |
||
455 | * |
||
456 | * @param string $prefix |
||
457 | * @return object $this |
||
458 | **/ |
||
459 | public function set_prefix( $prefix ) { |
||
467 | |||
468 | /** |
||
469 | * Set field label. |
||
470 | * |
||
471 | * @param string $label If null, the label will be generated from the field name |
||
472 | **/ |
||
473 | View Code Duplication | public function set_label( $label ) { |
|
488 | |||
489 | /** |
||
490 | * Return field label. |
||
491 | * |
||
492 | * @return string |
||
493 | **/ |
||
494 | public function get_label() { |
||
497 | |||
498 | /** |
||
499 | * Set additional text to be displayed during field render, |
||
500 | * containing information and guidance for the user |
||
501 | * |
||
502 | * @return object $this |
||
503 | **/ |
||
504 | public function set_help_text( $help_text ) { |
||
508 | |||
509 | /** |
||
510 | * Alias for set_help_text() |
||
511 | * |
||
512 | * @see set_help_text() |
||
513 | * @return object $this |
||
514 | **/ |
||
515 | public function help_text( $help_text ) { |
||
518 | |||
519 | /** |
||
520 | * Return the field help text |
||
521 | * |
||
522 | * @return object $this |
||
523 | **/ |
||
524 | public function get_help_text() { |
||
527 | |||
528 | /** |
||
529 | * Whether or not this value should be auto loaded. Applicable to theme options only. |
||
530 | * |
||
531 | * @param bool $autoload |
||
532 | * @return object $this |
||
533 | **/ |
||
534 | public function set_autoload( $autoload ) { |
||
538 | |||
539 | /** |
||
540 | * Return whether or not this value should be auto loaded. |
||
541 | * |
||
542 | * @return bool |
||
543 | **/ |
||
544 | public function get_autoload() { |
||
547 | |||
548 | /** |
||
549 | * Whether or not this field will be initialized when the field is in the viewport (visible). |
||
550 | * |
||
551 | * @param bool $lazyload |
||
552 | * @return object $this |
||
553 | **/ |
||
554 | public function set_lazyload( $lazyload ) { |
||
558 | |||
559 | /** |
||
560 | * Return whether or not this field should be lazyloaded. |
||
561 | * |
||
562 | * @return bool |
||
563 | **/ |
||
564 | public function get_lazyload() { |
||
567 | |||
568 | /** |
||
569 | * Set the field width. |
||
570 | * |
||
571 | * @param int $width |
||
572 | * @return object $this |
||
573 | **/ |
||
574 | public function set_width( $width ) { |
||
578 | |||
579 | /** |
||
580 | * Get the field width. |
||
581 | * |
||
582 | * @return int $width |
||
583 | **/ |
||
584 | public function get_width() { |
||
587 | |||
588 | /** |
||
589 | * Add custom CSS class to the field html container. |
||
590 | * |
||
591 | * @param string|array $classes |
||
592 | * @return object $this |
||
593 | **/ |
||
594 | public function add_class( $classes ) { |
||
602 | |||
603 | /** |
||
604 | * Get the field custom CSS classes. |
||
605 | * |
||
606 | * @return array |
||
607 | **/ |
||
608 | public function get_classes() { |
||
611 | |||
612 | /** |
||
613 | * Whether this field is mandatory for the user |
||
614 | * |
||
615 | * @param bool $required |
||
616 | * @return object $this |
||
617 | **/ |
||
618 | public function set_required( $required = true ) { |
||
622 | |||
623 | /** |
||
624 | * HTML id attribute getter. |
||
625 | * @return string |
||
626 | */ |
||
627 | 1 | public function get_id() { |
|
630 | |||
631 | /** |
||
632 | * HTML id attribute setter |
||
633 | * @param string $id |
||
634 | */ |
||
635 | 1 | public function set_id( $id ) { |
|
638 | |||
639 | /** |
||
640 | * Return whether this field is mandatory for the user |
||
641 | * |
||
642 | * @return bool |
||
643 | **/ |
||
644 | public function is_required() { |
||
647 | |||
648 | /** |
||
649 | * Returns the type of the field based on the class. |
||
650 | * The class is stripped by the "CarbonFields" prefix. |
||
651 | * Also the "Field" suffix is removed. |
||
652 | * Then underscores and backslashes are removed. |
||
653 | * |
||
654 | * @return string |
||
655 | */ |
||
656 | public function get_type() { |
||
661 | |||
662 | /** |
||
663 | * Cleans up an object class for usage as HTML class |
||
664 | * |
||
665 | * @return string |
||
666 | */ |
||
667 | protected function clean_type( $type ) { |
||
678 | |||
679 | /** |
||
680 | * Return an array of html classes to be used for the field container |
||
681 | * |
||
682 | * @return array |
||
683 | */ |
||
684 | public function get_html_class() { |
||
701 | |||
702 | /** |
||
703 | * Allows the value of a field to be processed after loading. |
||
704 | * Can be implemented by the extending class if necessary. |
||
705 | * |
||
706 | * @return array |
||
707 | */ |
||
708 | public function process_value() { |
||
711 | |||
712 | /** |
||
713 | * Returns an array that holds the field data, suitable for JSON representation. |
||
714 | * This data will be available in the Underscore template and the Backbone Model. |
||
715 | * |
||
716 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
717 | * @return array |
||
718 | */ |
||
719 | public function to_json( $load ) { |
||
745 | |||
746 | /** |
||
747 | * Set the field visibility conditional logic. |
||
748 | * |
||
749 | * @param array |
||
750 | */ |
||
751 | 8 | public function set_conditional_logic( $rules ) { |
|
756 | |||
757 | /** |
||
758 | * Get the conditional logic rules |
||
759 | * |
||
760 | * @return array |
||
761 | */ |
||
762 | 3 | public function get_conditional_logic() { |
|
765 | |||
766 | /** |
||
767 | * Validate and parse the conditional logic rules. |
||
768 | * |
||
769 | * @param array $rules |
||
770 | * @return array |
||
771 | */ |
||
772 | protected function parse_conditional_rules( $rules ) { |
||
831 | |||
832 | |||
833 | /** |
||
834 | * Hook administration scripts. |
||
835 | */ |
||
836 | public static function admin_hook_scripts() { |
||
858 | |||
859 | /** |
||
860 | * Hook administration styles. |
||
861 | */ |
||
862 | public static function admin_hook_styles() { |
||
865 | } // END Field |
||
866 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.