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 |
||
13 | class Field { |
||
14 | /** |
||
15 | * Stores all the field backbone templates |
||
16 | * |
||
17 | * @see factory() |
||
18 | * @see add_template() |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $templates = array(); |
||
22 | |||
23 | /** |
||
24 | * Globally unique field identificator. Generated randomly |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $id; |
||
29 | |||
30 | /** |
||
31 | * Stores the initial <kbd>$type</kbd> variable passed to the <code>factory()</code> method |
||
32 | * |
||
33 | * @see factory |
||
34 | * @var string |
||
35 | */ |
||
36 | public $type; |
||
37 | |||
38 | /** |
||
39 | * Field value |
||
40 | * |
||
41 | * @var mixed |
||
42 | */ |
||
43 | protected $value; |
||
44 | |||
45 | /** |
||
46 | * Default field value |
||
47 | * |
||
48 | * @var mixed |
||
49 | */ |
||
50 | protected $default_value; |
||
51 | |||
52 | /** |
||
53 | * Sanitized field name used as input name attribute during field render |
||
54 | * |
||
55 | * @see factory() |
||
56 | * @see set_name() |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $name; |
||
60 | |||
61 | /** |
||
62 | * The base field name which is used in the container. |
||
63 | * |
||
64 | * @see set_base_name() |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $base_name; |
||
68 | |||
69 | /** |
||
70 | * Field name used as label during field render |
||
71 | * |
||
72 | * @see factory() |
||
73 | * @see set_label() |
||
74 | * @var string |
||
75 | */ |
||
76 | protected $label; |
||
77 | |||
78 | /** |
||
79 | * Additional text containing information and guidance for the user |
||
80 | * |
||
81 | * @see help_text() |
||
82 | * @var string |
||
83 | */ |
||
84 | protected $help_text; |
||
85 | |||
86 | /** |
||
87 | * Field DataStore instance to which save, load and delete calls are delegated |
||
88 | * |
||
89 | * @see set_datastore() |
||
90 | * @see get_datastore() |
||
91 | * @var Carbon_DataStore |
||
92 | */ |
||
93 | protected $store; |
||
94 | |||
95 | /** |
||
96 | * The type of the container this field is in |
||
97 | * |
||
98 | * @see get_context() |
||
99 | * @var string |
||
100 | */ |
||
101 | protected $context; |
||
102 | |||
103 | /** |
||
104 | * Whether or not this value should be auto loaded. Applicable to theme options only. |
||
105 | * |
||
106 | * @see set_autoload() |
||
107 | * @var bool |
||
108 | **/ |
||
109 | protected $autoload = false; |
||
110 | |||
111 | /** |
||
112 | * Whether or not this field will be initialized when the field is in the viewport (visible). |
||
113 | * |
||
114 | * @see set_lazyload() |
||
115 | * @var bool |
||
116 | **/ |
||
117 | protected $lazyload = false; |
||
118 | |||
119 | /** |
||
120 | * The width of the field. |
||
121 | * |
||
122 | * @see set_width() |
||
123 | * @var int |
||
124 | **/ |
||
125 | protected $width = 0; |
||
126 | |||
127 | /** |
||
128 | * Custom CSS classes. |
||
129 | * |
||
130 | * @see add_class() |
||
131 | * @var array |
||
132 | **/ |
||
133 | protected $classes = array(); |
||
134 | |||
135 | /** |
||
136 | * Whether or not this field is required. |
||
137 | * |
||
138 | * @see set_required() |
||
139 | * @var bool |
||
140 | **/ |
||
141 | protected $required = false; |
||
142 | |||
143 | /** |
||
144 | * Prefix to be pretended to the field name during load, save, delete and <strong>render</strong> |
||
145 | * |
||
146 | * @var string |
||
147 | **/ |
||
148 | protected $name_prefix = '_'; |
||
149 | |||
150 | /** |
||
151 | * Stores the field conditional logic rules. |
||
152 | * |
||
153 | * @var array |
||
154 | **/ |
||
155 | protected $conditional_logic = array(); |
||
156 | |||
157 | /** |
||
158 | * Create a new field of type $type and name $name and label $label. |
||
159 | * |
||
160 | * @param string $type |
||
161 | * @param string $name lower case and underscore-delimited |
||
162 | * @param string $label (optional) Automatically generated from $name if not present |
||
163 | * @return object $field |
||
164 | **/ |
||
165 | 22 | public static function factory( $type, $name, $label = null ) { |
|
166 | 22 | $type = str_replace( ' ', '_', ucwords( str_replace( '_', ' ', $type ) ) ); |
|
167 | |||
168 | 22 | $class = __NAMESPACE__ . '\\' . $type . '_Field'; |
|
169 | |||
170 | 22 | if ( ! class_exists( $class ) ) { |
|
171 | 3 | Incorrect_Syntax_Exception::raise( 'Unknown field "' . $type . '".' ); |
|
172 | 1 | $class = __NAMESPACE__ . '\\Broken_Field'; |
|
173 | 1 | } |
|
174 | |||
175 | 20 | if ( strpos( $name, '-' ) !== false ) { |
|
176 | 1 | Incorrect_Syntax_Exception::raise( 'Forbidden character "-" in name "' . $name . '".' ); |
|
177 | $class = __NAMESPACE__ . '\\Broken_Field'; |
||
178 | } |
||
179 | |||
180 | 19 | $field = new $class( $name, $label ); |
|
181 | 18 | $field->type = $type; |
|
182 | 18 | $field->add_template( $field->get_type(), array( $field, 'template' ) ); |
|
183 | |||
184 | 18 | return $field; |
|
185 | } |
||
186 | |||
187 | /** |
||
188 | * An alias of factory(). |
||
189 | * |
||
190 | * @see Field::factory() |
||
191 | **/ |
||
192 | 22 | public static function make( $type, $name, $label = null ) { |
|
193 | 22 | return self::factory( $type, $name, $label ); |
|
194 | } |
||
195 | |||
196 | /** |
||
197 | * Create a field from a certain type with the specified label. |
||
198 | * @param string $name Field name |
||
199 | * @param string $label Field label |
||
200 | */ |
||
201 | 19 | protected function __construct( $name, $label ) { |
|
202 | 19 | $this->set_name( $name ); |
|
203 | 18 | $this->set_label( $label ); |
|
204 | 18 | $this->set_base_name( $name ); |
|
205 | |||
206 | // Pick random ID |
||
207 | 18 | $random_string = md5( mt_rand() . $this->get_name() . $this->get_label() ); |
|
208 | 18 | $random_string = substr( $random_string, 0, 5 ); // 5 chars should be enough |
|
209 | 18 | $this->id = 'carbon-' . $random_string; |
|
210 | |||
211 | 18 | $this->init(); |
|
212 | 18 | if ( is_admin() ) { |
|
213 | $this->admin_init(); |
||
214 | } |
||
215 | |||
216 | 18 | add_action( 'admin_print_scripts', array( $this, 'admin_hook_scripts' ) ); |
|
217 | 18 | add_action( 'admin_print_styles', array( $this, 'admin_hook_styles' ) ); |
|
218 | 18 | } |
|
219 | |||
220 | /** |
||
221 | * Perform instance initialization after calling setup() |
||
222 | **/ |
||
223 | public function init() {} |
||
224 | |||
225 | /** |
||
226 | * Instance initialization when in the admin area. Called during object construction |
||
227 | **/ |
||
228 | public function admin_init() {} |
||
229 | |||
230 | /** |
||
231 | * Enqueue admin scripts. |
||
232 | * Called once per field type. |
||
233 | **/ |
||
234 | public function admin_enqueue_scripts() {} |
||
235 | |||
236 | /** |
||
237 | * Prints the main Underscore template |
||
238 | **/ |
||
239 | public function template() { } |
||
240 | |||
241 | /** |
||
242 | * Returns all the backbone templates |
||
243 | * |
||
244 | * @return array |
||
245 | **/ |
||
246 | public function get_templates() { |
||
247 | return $this->templates; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Adds a new backbone template |
||
252 | **/ |
||
253 | 18 | public function add_template( $name, $callback ) { |
|
254 | 18 | $this->templates[ $name ] = $callback; |
|
255 | 18 | } |
|
256 | |||
257 | /** |
||
258 | * Delegate load to the field DataStore instance |
||
259 | **/ |
||
260 | public function load() { |
||
261 | $this->store->load( $this ); |
||
262 | |||
263 | if ( $this->get_value() === false ) { |
||
264 | $this->set_value( $this->default_value ); |
||
265 | } |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Delegate save to the field DataStore instance |
||
270 | **/ |
||
271 | public function save() { |
||
272 | return $this->store->save( $this ); |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Delegate delete to the field DataStore instance |
||
277 | **/ |
||
278 | public function delete() { |
||
279 | return $this->store->delete( $this ); |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Load the field value from an input array based on it's name |
||
284 | * |
||
285 | * @param array $input (optional) Array of field names and values. Defaults to $_POST |
||
286 | **/ |
||
287 | public function set_value_from_input( $input = null ) { |
||
288 | if ( is_null( $input ) ) { |
||
289 | $input = $_POST; |
||
290 | } |
||
291 | |||
292 | if ( ! isset( $input[ $this->name ] ) ) { |
||
293 | $this->set_value( null ); |
||
294 | } else { |
||
295 | $this->set_value( stripslashes_deep( $input[ $this->name ] ) ); |
||
296 | } |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * Assign DataStore instance for use during load, save and delete |
||
301 | * |
||
302 | * @param object $store |
||
303 | * @return object $this |
||
304 | **/ |
||
305 | public function set_datastore( Datastore_Interface $store ) { |
||
306 | $this->store = $store; |
||
307 | return $this; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Return the DataStore instance used by the field |
||
312 | * |
||
313 | * @return object $store |
||
314 | **/ |
||
315 | public function get_datastore() { |
||
316 | return $this->store; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Assign the type of the container this field is in |
||
321 | * |
||
322 | * @param string |
||
323 | * @return object $this |
||
324 | **/ |
||
325 | public function set_context( $context ) { |
||
326 | $this->context = $context; |
||
327 | return $this; |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * Return the type of the container this field is in |
||
332 | * |
||
333 | * @return string |
||
334 | **/ |
||
335 | public function get_context() { |
||
336 | return $this->context; |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * Directly modify the field value |
||
341 | * |
||
342 | * @param mixed $value |
||
343 | **/ |
||
344 | public function set_value( $value ) { |
||
345 | $this->value = $value; |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Set default field value |
||
350 | * |
||
351 | * @param mixed $default_value |
||
352 | **/ |
||
353 | public function set_default_value( $default_value ) { |
||
354 | $this->default_value = $default_value; |
||
355 | return $this; |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * Get default field value |
||
360 | * |
||
361 | * @return mixed |
||
362 | **/ |
||
363 | public function get_default_value() { |
||
364 | return $this->default_value; |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Return the field value |
||
369 | * |
||
370 | * @return mixed |
||
371 | **/ |
||
372 | public function get_value() { |
||
373 | return $this->value; |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * Set field name. |
||
378 | * Use only if you are completely aware of what you are doing. |
||
379 | * |
||
380 | * @param string $name Field name, either sanitized or not |
||
381 | **/ |
||
382 | 19 | public function set_name( $name ) { |
|
383 | 19 | $name = preg_replace( '~\s+~', '_', mb_strtolower( $name ) ); |
|
384 | |||
385 | 19 | if ( empty( $name ) ) { |
|
386 | 1 | Incorrect_Syntax_Exception::raise('Field name can\'t be empty'); |
|
387 | } |
||
388 | |||
389 | 18 | if ( $this->name_prefix && strpos( $name, $this->name_prefix ) !== 0 ) { |
|
390 | 14 | $name = $this->name_prefix . $name; |
|
391 | 14 | } |
|
392 | |||
393 | |||
394 | 18 | $this->name = $name; |
|
395 | 18 | } |
|
396 | |||
397 | /** |
||
398 | * Return the field name |
||
399 | * |
||
400 | * @return string |
||
401 | **/ |
||
402 | 18 | public function get_name() { |
|
403 | 18 | return $this->name; |
|
404 | } |
||
405 | |||
406 | /** |
||
407 | * Set field base name as defined in the container. |
||
408 | **/ |
||
409 | 18 | public function set_base_name( $name ) { |
|
410 | 18 | $this->base_name = $name; |
|
411 | 18 | } |
|
412 | |||
413 | /** |
||
414 | * Return the field base name. |
||
415 | * |
||
416 | * @return string |
||
417 | **/ |
||
418 | public function get_base_name() { |
||
421 | |||
422 | /** |
||
423 | * Set field name prefix. Calling this method will update the current field |
||
424 | * name and the conditional logic fields. |
||
425 | * |
||
426 | * @param string $prefix |
||
427 | * @return object $this |
||
428 | **/ |
||
429 | View Code Duplication | public function set_prefix( $prefix ) { |
|
430 | $escaped_prefix = preg_quote( $this->name_prefix, '~' ); |
||
431 | $this->name = preg_replace( '~^' . $escaped_prefix . '~', '', $this->name ); |
||
432 | $this->name_prefix = $prefix; |
||
433 | $this->name = $this->name_prefix . $this->name; |
||
434 | |||
435 | return $this; |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * Set field label. |
||
440 | * |
||
441 | * @param string $label If null, the label will be generated from the field name |
||
442 | **/ |
||
443 | 18 | public function set_label( $label ) { |
|
444 | // Try to guess field label from it's name |
||
445 | 18 | View Code Duplication | if ( is_null( $label ) ) { |
446 | // remove the leading underscore(if it's there) |
||
447 | 18 | $label = preg_replace( '~^_~', '', $this->name ); |
|
448 | |||
449 | // remove the leading "crb_"(if it's there) |
||
450 | 18 | $label = preg_replace( '~^crb_~', '', $label ); |
|
451 | |||
452 | // split the name into words and make them capitalized |
||
453 | 18 | $label = mb_convert_case( str_replace( '_', ' ', $label ), MB_CASE_TITLE ); |
|
454 | 18 | } |
|
455 | |||
456 | 18 | $this->label = $label; |
|
457 | 18 | } |
|
458 | |||
459 | /** |
||
460 | * Return field label. |
||
461 | * |
||
462 | * @return string |
||
463 | **/ |
||
464 | 18 | public function get_label() { |
|
465 | 18 | return $this->label; |
|
466 | } |
||
467 | |||
468 | /** |
||
469 | * Set additional text to be displayed during field render, |
||
470 | * containing information and guidance for the user |
||
471 | * |
||
472 | * @return object $this |
||
473 | **/ |
||
474 | public function set_help_text( $help_text ) { |
||
478 | |||
479 | /** |
||
480 | * Alias for set_help_text() |
||
481 | * |
||
482 | * @see set_help_text() |
||
483 | * @return object $this |
||
484 | **/ |
||
485 | public function help_text( $help_text ) { |
||
488 | |||
489 | /** |
||
490 | * Return the field help text |
||
491 | * |
||
492 | * @return object $this |
||
493 | **/ |
||
494 | public function get_help_text() { |
||
497 | |||
498 | /** |
||
499 | * Whether or not this value should be auto loaded. Applicable to theme options only. |
||
500 | * |
||
501 | * @param bool $autoload |
||
502 | * @return object $this |
||
503 | **/ |
||
504 | public function set_autoload( $autoload ) { |
||
508 | |||
509 | /** |
||
510 | * Return whether or not this value should be auto loaded. |
||
511 | * |
||
512 | * @return bool |
||
513 | **/ |
||
514 | public function get_autoload() { |
||
517 | |||
518 | /** |
||
519 | * Whether or not this field will be initialized when the field is in the viewport (visible). |
||
520 | * |
||
521 | * @param bool $lazyload |
||
522 | * @return object $this |
||
523 | **/ |
||
524 | public function set_lazyload( $lazyload ) { |
||
528 | |||
529 | /** |
||
530 | * Return whether or not this field should be lazyloaded. |
||
531 | * |
||
532 | * @return bool |
||
533 | **/ |
||
534 | public function get_lazyload() { |
||
537 | |||
538 | /** |
||
539 | * Set the field width. |
||
540 | * |
||
541 | * @param int $width |
||
542 | * @return object $this |
||
543 | **/ |
||
544 | public function set_width( $width ) { |
||
548 | |||
549 | /** |
||
550 | * Get the field width. |
||
551 | * |
||
552 | * @return int $width |
||
553 | **/ |
||
554 | public function get_width() { |
||
557 | |||
558 | /** |
||
559 | * Add custom CSS class to the field html container. |
||
560 | * |
||
561 | * @param string|array $classes |
||
562 | * @return object $this |
||
563 | **/ |
||
564 | public function add_class( $classes ) { |
||
572 | |||
573 | /** |
||
574 | * Get the field custom CSS classes. |
||
575 | * |
||
576 | * @return array |
||
577 | **/ |
||
578 | public function get_classes() { |
||
581 | |||
582 | /** |
||
583 | * Whether this field is mandatory for the user |
||
584 | * |
||
585 | * @param bool $required |
||
586 | * @return object $this |
||
587 | **/ |
||
588 | public function set_required( $required ) { |
||
592 | |||
593 | /** |
||
594 | * HTML id attribute getter. |
||
595 | * @return string |
||
596 | */ |
||
597 | 1 | public function get_id() { |
|
600 | |||
601 | /** |
||
602 | * HTML id attribute setter |
||
603 | * @param string $id |
||
604 | */ |
||
605 | 1 | public function set_id( $id ) { |
|
608 | |||
609 | /** |
||
610 | * Return whether this field is mandatory for the user |
||
611 | * |
||
612 | * @return bool |
||
613 | **/ |
||
614 | public function is_required() { |
||
617 | |||
618 | /** |
||
619 | * Returns the type of the field based on the class. |
||
620 | * The class is stripped by the "CarbonFields" prefix. |
||
621 | * Also the "Field" suffix is removed. |
||
622 | * Then underscores and backslashes are removed. |
||
623 | * |
||
624 | * @return string |
||
625 | */ |
||
626 | 18 | public function get_type() { |
|
627 | 18 | $class = get_class( $this ); |
|
628 | |||
629 | 18 | return $this->clean_type( $class ); |
|
630 | } |
||
631 | |||
632 | /** |
||
633 | * Cleans up an object class for usage as HTML class |
||
634 | * |
||
635 | * @return string |
||
636 | */ |
||
637 | 18 | protected function clean_type( $type ) { |
|
638 | $remove = array( |
||
639 | 18 | '_', |
|
640 | 18 | '\\', |
|
641 | 18 | 'CarbonFields', |
|
642 | 18 | 'Field', |
|
643 | 18 | ); |
|
644 | 18 | $clean_class = str_replace( $remove, '', $type ); |
|
645 | |||
646 | 18 | return $clean_class; |
|
647 | } |
||
648 | |||
649 | /** |
||
650 | * Return an array of html classes to be used for the field container |
||
651 | * |
||
652 | * @return array |
||
653 | */ |
||
654 | public function get_html_class() { |
||
671 | |||
672 | /** |
||
673 | * Allows the value of a field to be processed after loading. |
||
674 | * Can be implemented by the extending class if necessary. |
||
675 | * |
||
676 | * @return array |
||
677 | */ |
||
678 | public function process_value() { |
||
681 | |||
682 | /** |
||
683 | * Returns an array that holds the field data, suitable for JSON representation. |
||
684 | * This data will be available in the Underscore template and the Backbone Model. |
||
685 | * |
||
686 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
687 | * @return array |
||
688 | */ |
||
689 | public function to_json( $load ) { |
||
715 | |||
716 | /** |
||
717 | * Set the field visibility conditional logic. |
||
718 | * |
||
719 | * @param array |
||
720 | */ |
||
721 | 8 | public function set_conditional_logic( $rules ) { |
|
722 | 8 | $this->conditional_logic = $this->parse_conditional_rules( $rules ); |
|
723 | |||
724 | 3 | return $this; |
|
725 | } |
||
726 | |||
727 | /** |
||
728 | * Get the conditional logic rules |
||
729 | * |
||
730 | * @return array |
||
731 | */ |
||
732 | 3 | public function get_conditional_logic() { |
|
733 | 3 | return $this->conditional_logic; |
|
734 | } |
||
735 | |||
736 | /** |
||
737 | * Validate and parse the conditional logic rules. |
||
738 | * |
||
739 | * @param array $rules |
||
740 | * @return array |
||
741 | */ |
||
742 | 8 | protected function parse_conditional_rules( $rules ) { |
|
743 | 8 | if ( ! is_array( $rules ) ) { |
|
744 | 1 | Incorrect_Syntax_Exception::raise( 'Conditional logic rules argument should be an array.' ); |
|
745 | } |
||
746 | |||
801 | |||
802 | |||
803 | /** |
||
804 | * Hook administration scripts. |
||
805 | */ |
||
806 | public function admin_hook_scripts() { |
||
829 | |||
830 | /** |
||
831 | * Hook administration styles. |
||
832 | */ |
||
833 | public function admin_hook_styles() { |
||
836 | } // END Field |
||
837 |