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 GravityView_View 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 GravityView_View, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class GravityView_View extends Gamajo_Template_Loader { |
||
24 | |||
25 | /** |
||
26 | * Prefix for filter names. |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $filter_prefix = 'gravityview'; |
||
30 | |||
31 | /** |
||
32 | * Directory name where custom templates for this plugin should be found in the theme. |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $theme_template_directory = 'gravityview'; |
||
36 | |||
37 | /** |
||
38 | * Reference to the root directory path of this plugin. |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $plugin_directory = GRAVITYVIEW_DIR; |
||
42 | |||
43 | /** |
||
44 | * Store templates locations that have already been located |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $located_templates = array(); |
||
48 | |||
49 | /** |
||
50 | * The name of the template, like "list", "table", or "datatables" |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $template_part_slug = ''; |
||
54 | |||
55 | /** |
||
56 | * The name of the file part, like "body" or "single" |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $template_part_name = ''; |
||
60 | |||
61 | /** |
||
62 | * @var int Gravity Forms form ID |
||
63 | */ |
||
64 | protected $form_id = NULL; |
||
65 | |||
66 | /** |
||
67 | * @var int View ID |
||
68 | * @todo: this needs to be public until extensions support 1.7+ |
||
69 | */ |
||
70 | public $view_id = NULL; |
||
71 | |||
72 | /** |
||
73 | * @var array Fields for the form |
||
74 | */ |
||
75 | protected $fields = array(); |
||
76 | |||
77 | /** |
||
78 | * @var string Current screen. Defaults to "directory" or "single" |
||
79 | */ |
||
80 | protected $context = 'directory'; |
||
81 | |||
82 | /** |
||
83 | * @var int|null If in embedded post or page, the ID of it |
||
84 | */ |
||
85 | protected $post_id = NULL; |
||
86 | |||
87 | /** |
||
88 | * @var array Gravity Forms form array at ID $form_id |
||
89 | */ |
||
90 | protected $form = NULL; |
||
91 | |||
92 | /** |
||
93 | * @var array Configuration for the View |
||
94 | */ |
||
95 | protected $atts = array(); |
||
96 | |||
97 | /** |
||
98 | * @var array Entries for the current result. Single item in array for single entry View |
||
99 | */ |
||
100 | protected $entries = array(); |
||
101 | |||
102 | /** |
||
103 | * @var int Total entries count for the current result. |
||
104 | */ |
||
105 | protected $total_entries = 0; |
||
106 | |||
107 | /** |
||
108 | * @var string The label to display back links |
||
109 | */ |
||
110 | protected $back_link_label = ''; |
||
111 | |||
112 | /** |
||
113 | * @var array Array with `offset` and `page_size` keys |
||
114 | */ |
||
115 | protected $paging = array(); |
||
116 | |||
117 | /** |
||
118 | * @var array Array with `sort_field` and `sort_direction` keys |
||
119 | */ |
||
120 | protected $sorting = array(); |
||
121 | |||
122 | /** |
||
123 | * @var bool Whether to hide the results until a search is performed |
||
124 | * @since 1.5.4 |
||
125 | */ |
||
126 | protected $hide_until_searched = false; |
||
127 | |||
128 | /** |
||
129 | * Current entry in the loop |
||
130 | * @var array |
||
131 | */ |
||
132 | protected $_current_entry = array(); |
||
133 | |||
134 | /** |
||
135 | * @var array |
||
136 | */ |
||
137 | protected $_current_field = array(); |
||
138 | |||
139 | /** |
||
140 | * @var GravityView_View |
||
141 | */ |
||
142 | static $instance = NULL; |
||
143 | |||
144 | /** |
||
145 | * Construct the view object |
||
146 | * @param array $atts Associative array to set the data of |
||
147 | */ |
||
148 | function __construct( $atts = array() ) { |
||
184 | |||
185 | /** |
||
186 | * @param null $passed_post |
||
187 | * |
||
188 | * @return GravityView_View |
||
189 | */ |
||
190 | static function getInstance( $passed_post = NULL ) { |
||
198 | |||
199 | /** |
||
200 | * @param string|null $key The key to a specific attribute of the current field |
||
201 | * @return array|mixed|null If $key is set and attribute exists at $key, return that. If not set, return NULL. Otherwise, return current field array |
||
202 | */ |
||
203 | public function getCurrentField( $key = NULL ) { |
||
214 | |||
215 | public function setCurrentFieldSetting( $key, $value ) { |
||
222 | |||
223 | public function getCurrentFieldSetting( $key ) { |
||
232 | |||
233 | /** |
||
234 | * @param array $passed_field |
||
235 | */ |
||
236 | public function setCurrentField( $passed_field ) { |
||
250 | |||
251 | /** |
||
252 | * @param string|null $key The key to a specific field in the fields array |
||
253 | * @return array|mixed|null If $key is set and field exists at $key, return that. If not set, return NULL. Otherwise, return array of fields. |
||
254 | */ |
||
255 | View Code Duplication | public function getAtts( $key = NULL ) { |
|
266 | |||
267 | /** |
||
268 | * @param array $atts |
||
269 | */ |
||
270 | public function setAtts( $atts ) { |
||
273 | |||
274 | /** |
||
275 | * @return array |
||
276 | */ |
||
277 | public function getForm() { |
||
280 | |||
281 | /** |
||
282 | * @param array $form |
||
283 | */ |
||
284 | public function setForm( $form ) { |
||
287 | |||
288 | /** |
||
289 | * @return int|null |
||
290 | */ |
||
291 | public function getPostId() { |
||
294 | |||
295 | /** |
||
296 | * @param int|null $post_id |
||
297 | */ |
||
298 | public function setPostId( $post_id ) { |
||
301 | |||
302 | /** |
||
303 | * @return string |
||
304 | */ |
||
305 | public function getContext() { |
||
308 | |||
309 | /** |
||
310 | * @param string $context |
||
311 | */ |
||
312 | public function setContext( $context ) { |
||
315 | |||
316 | /** |
||
317 | * @param string|null $key The key to a specific field in the fields array |
||
318 | * @return array|mixed|null If $key is set and field exists at $key, return that. If not set, return NULL. Otherwise, return array of fields. |
||
319 | */ |
||
320 | public function getFields( $key = null ) { |
||
330 | |||
331 | /** |
||
332 | * @param array $fields |
||
333 | */ |
||
334 | public function setFields( $fields ) { |
||
337 | |||
338 | /** |
||
339 | * @param string $key The key to a specific field in the fields array |
||
340 | * @return array|mixed|null If $key is set and field exists at $key, return that. If not set, return NULL. Otherwise, return array of fields. |
||
341 | */ |
||
342 | View Code Duplication | public function getField( $key ) { |
|
352 | |||
353 | /** |
||
354 | * @param string $key The key to a specific field in the fields array |
||
355 | * @param mixed $value The value to set for the field |
||
356 | */ |
||
357 | public function setField( $key, $value ) { |
||
360 | |||
361 | /** |
||
362 | * @return int |
||
363 | */ |
||
364 | public function getViewId() { |
||
367 | |||
368 | /** |
||
369 | * @param int $view_id |
||
370 | */ |
||
371 | public function setViewId( $view_id ) { |
||
374 | |||
375 | /** |
||
376 | * @return int |
||
377 | */ |
||
378 | public function getFormId() { |
||
381 | |||
382 | /** |
||
383 | * @param int $form_id |
||
384 | */ |
||
385 | public function setFormId( $form_id ) { |
||
388 | |||
389 | /** |
||
390 | * @return array |
||
391 | */ |
||
392 | public function getEntries() { |
||
395 | |||
396 | /** |
||
397 | * @param array $entries |
||
398 | */ |
||
399 | public function setEntries( $entries ) { |
||
402 | |||
403 | /** |
||
404 | * @return int |
||
405 | */ |
||
406 | public function getTotalEntries() { |
||
409 | |||
410 | /** |
||
411 | * @param int $total_entries |
||
412 | */ |
||
413 | public function setTotalEntries( $total_entries ) { |
||
416 | |||
417 | /** |
||
418 | * @return array |
||
419 | */ |
||
420 | public function getPaging() { |
||
423 | |||
424 | /** |
||
425 | * @param array $paging |
||
426 | */ |
||
427 | public function setPaging( $paging ) { |
||
430 | |||
431 | /** |
||
432 | * Get an array with pagination information |
||
433 | * |
||
434 | * @since 1.13 |
||
435 | * |
||
436 | * @return array { |
||
437 | * @type int $first The starting entry number (counter, not ID) |
||
438 | * @type int $last The last displayed entry number (counter, not ID) |
||
439 | * @type int $total The total number of entries |
||
440 | * } |
||
441 | */ |
||
442 | public function getPaginationCounts() { |
||
469 | |||
470 | /** |
||
471 | * @return array |
||
472 | */ |
||
473 | public function getSorting() { |
||
476 | |||
477 | /** |
||
478 | * @param array $sorting |
||
479 | */ |
||
480 | public function setSorting( $sorting ) { |
||
483 | |||
484 | /** |
||
485 | * @return string |
||
486 | */ |
||
487 | public function getBackLinkLabel() { |
||
495 | |||
496 | /** |
||
497 | * @param string $back_link_label |
||
498 | */ |
||
499 | public function setBackLinkLabel( $back_link_label ) { |
||
502 | |||
503 | /** |
||
504 | * @return boolean |
||
505 | */ |
||
506 | public function isHideUntilSearched() { |
||
509 | |||
510 | /** |
||
511 | * @param boolean $hide_until_searched |
||
512 | */ |
||
513 | public function setHideUntilSearched( $hide_until_searched ) { |
||
516 | |||
517 | /** |
||
518 | * @return string |
||
519 | */ |
||
520 | public function getTemplatePartSlug() { |
||
523 | |||
524 | /** |
||
525 | * @param string $template_part_slug |
||
526 | */ |
||
527 | public function setTemplatePartSlug( $template_part_slug ) { |
||
530 | |||
531 | /** |
||
532 | * @return string |
||
533 | */ |
||
534 | public function getTemplatePartName() { |
||
537 | |||
538 | /** |
||
539 | * @param string $template_part_name |
||
540 | */ |
||
541 | public function setTemplatePartName( $template_part_name ) { |
||
544 | |||
545 | /** |
||
546 | * Return the current entry. If in the loop, the current entry. If single entry, the currently viewed entry. |
||
547 | * @return array |
||
548 | */ |
||
549 | public function getCurrentEntry() { |
||
565 | |||
566 | /** |
||
567 | * @param array $current_entry |
||
568 | * @return void |
||
569 | */ |
||
570 | public function setCurrentEntry( $current_entry ) { |
||
573 | |||
574 | /** |
||
575 | * Clear the current entry after all entries in the loop have been displayed. |
||
576 | * |
||
577 | * @since 1.7.3 |
||
578 | * @return void |
||
579 | */ |
||
580 | public function clearCurrentEntry() { |
||
583 | |||
584 | /** |
||
585 | * Render an output zone, as configured in the Admin |
||
586 | * |
||
587 | * @param string $zone The zone name, like 'footer-left' |
||
588 | * @param array $atts |
||
589 | * |
||
590 | * @return string|null |
||
591 | */ |
||
592 | public function renderZone( $zone = '', $atts = array() ) { |
||
659 | |||
660 | /** |
||
661 | * In order to improve lookup times, we store located templates in a local array. |
||
662 | * |
||
663 | * This improves performance by up to 1/2 second on a 250 entry View with 7 columns showing |
||
664 | * |
||
665 | * @inheritdoc |
||
666 | * @see Gamajo_Template_Loader::locate_template() |
||
667 | * @return null|string NULL: Template not found; String: path to template |
||
668 | */ |
||
669 | function locate_template( $template_names, $load = false, $require_once = true ) { |
||
691 | |||
692 | /** |
||
693 | * Magic Method: Instead of throwing an error when a variable isn't set, return null. |
||
694 | * @param string $name Key for the data retrieval. |
||
695 | * @return mixed|null The stored data. |
||
696 | */ |
||
697 | public function __get( $name ) { |
||
704 | |||
705 | /** |
||
706 | * Enable overrides of GravityView templates on a granular basis |
||
707 | * |
||
708 | * The loading order is: |
||
709 | * |
||
710 | * - view-[View ID]-table-footer.php |
||
711 | * - form-[Form ID]-table-footer.php |
||
712 | * - page-[ID of post or page where view is embedded]-table-footer.php |
||
713 | * - table-footer.php |
||
714 | * |
||
715 | * @see Gamajo_Template_Loader::get_template_file_names() Where the filter is |
||
716 | * @param array $templates Existing list of templates. |
||
717 | * @param string $slug Name of the template base, example: `table`, `list`, `datatables`, `map` |
||
718 | * @param string $name Name of the template part, example: `body`, `footer`, `head`, `single` |
||
719 | * |
||
720 | * @return array $templates Modified template array, merged with existing $templates values |
||
721 | */ |
||
722 | function add_id_specific_templates( $templates, $slug, $name ) { |
||
745 | |||
746 | // Load the template |
||
747 | public function render( $slug, $name, $require_once = true ) { |
||
767 | |||
768 | /** |
||
769 | * |
||
770 | * @param $view_id |
||
771 | */ |
||
772 | public function render_widget_hooks( $view_id ) { |
||
859 | |||
860 | } |
||
861 | |||
862 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.