Complex classes like GravityView_View_Data 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_Data, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class GravityView_View_Data { |
||
9 | |||
10 | static $instance = NULL; |
||
11 | |||
12 | protected $views = array(); |
||
13 | |||
14 | /** |
||
15 | * |
||
16 | * @param null $passed_post |
||
17 | */ |
||
18 | 2 | private function __construct( $passed_post = NULL ) { |
|
30 | |||
31 | /** |
||
32 | * @return boolean |
||
33 | */ |
||
34 | 2 | public function has_multiple_views() { |
|
39 | |||
40 | |||
41 | /** |
||
42 | * Figure out what the View ID is for a variable, if any. |
||
43 | * |
||
44 | * Can be: |
||
45 | * - WP_Post (Either a `gravityview` post type or not) |
||
46 | * - Multi-dimensional array of WP_Post objects |
||
47 | * - Array with `view_id` or `id` key(s) set |
||
48 | * - String of content that may include GravityView shortcode |
||
49 | * - Number representing the Post ID or View ID |
||
50 | * |
||
51 | * @param mixed $passed_post See method description |
||
52 | * |
||
53 | * @deprecated |
||
54 | * @see \GV\View_Collection::from_post and \GV\Shortcode::parse |
||
55 | * |
||
56 | * @return int|null|array ID of the View. If there are multiple views in the content, array of IDs parsed. |
||
57 | */ |
||
58 | 3 | public function maybe_get_view_id( $passed_post ) { |
|
59 | 3 | $ids = array(); |
|
60 | |||
61 | 3 | if( ! empty( $passed_post ) ) { |
|
62 | |||
63 | 3 | if( is_numeric( $passed_post ) ) { |
|
64 | 1 | $passed_post = get_post( $passed_post ); |
|
65 | } |
||
66 | |||
67 | // Convert WP_Posts into WP_Posts[] array |
||
68 | 3 | if( $passed_post instanceof WP_Post ) { |
|
69 | 3 | $passed_post = array( $passed_post ); |
|
70 | } |
||
71 | |||
72 | 3 | if( is_array( $passed_post ) ) { |
|
73 | |||
74 | 3 | foreach ( $passed_post as &$post) { |
|
75 | 3 | if ( false /** Do not use for now. See issue #848 */ && function_exists( 'gravityview' ) && $post instanceof WP_Post ) { |
|
76 | $views = \GV\View_Collection::from_post( $post ); |
||
77 | foreach ( $views->all() as $view ) { |
||
78 | $ids []= $view->ID; |
||
79 | } |
||
80 | } else { |
||
81 | /** Deprecated, see \GV\View_Collection::from_post */ |
||
82 | 3 | if( ( get_post_type( $post ) === 'gravityview' ) ) { |
|
83 | 3 | $ids[] = $post->ID; |
|
84 | } else{ |
||
85 | // Parse the Post Content |
||
86 | 1 | $id = $this->parse_post_content( $post->post_content ); |
|
87 | 1 | if( $id ) { |
|
88 | 1 | $ids = array_merge( $ids, (array) $id ); |
|
89 | } |
||
90 | |||
91 | // Parse the Post Meta |
||
92 | 1 | $id = $this->parse_post_meta( $post->ID ); |
|
93 | 1 | if( $id ) { |
|
94 | 3 | $ids = array_merge( $ids, (array) $id ); |
|
95 | } |
||
96 | } |
||
97 | } |
||
98 | |||
99 | } |
||
100 | |||
101 | } else { |
||
102 | |||
103 | 1 | if ( is_string( $passed_post ) ) { |
|
104 | |||
105 | 1 | if ( false /** Do not use for now. See issue #848 */ && function_exists( 'gravityview' ) ) { |
|
106 | $shortcodes = \GV\Shortcode::parse( $passed_post ); |
||
107 | foreach ( $shortcodes as $shortcode ) { |
||
108 | if ( $shortcode->name == 'gravityview' && !empty( $shortcode->atts['id'] ) ) |
||
109 | $ids []= $shortcode->atts['id']; |
||
110 | } |
||
111 | } else { |
||
112 | /** Deprecated, use \GV\Shortcode::parse. */ |
||
113 | 1 | $id = $this->parse_post_content( $passed_post ); |
|
114 | 1 | if( $id ) { |
|
115 | 1 | $ids = array_merge( $ids, (array) $id ); |
|
116 | } |
||
117 | } |
||
118 | |||
119 | } else { |
||
120 | $id = $this->get_id_from_atts( $passed_post ); |
||
121 | $ids[] = intval( $id ); |
||
122 | } |
||
123 | } |
||
124 | } |
||
125 | |||
126 | 3 | if( empty($ids) ) { |
|
127 | return NULL; |
||
128 | } |
||
129 | |||
130 | // If it's just one ID, return that. |
||
131 | // Otherwise, return array of IDs |
||
132 | 3 | return ( sizeof( $ids ) === 1 ) ? $ids[0] : $ids; |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * @return GravityView_View_Data |
||
137 | */ |
||
138 | 2 | public static function getInstance( $passed_post = NULL ) { |
|
146 | |||
147 | 2 | function get_views() { |
|
150 | |||
151 | function get_view( $view_id, $atts = NULL ) { |
||
171 | |||
172 | /** |
||
173 | * Determines if a post, identified by the specified ID, exist |
||
174 | * within the WordPress database. |
||
175 | * |
||
176 | * @see http://tommcfarlin.com/wordpress-post-exists-by-id/ Fastest check available |
||
177 | * @param int $view_id The ID of the post to check |
||
178 | * @return bool True if the post exists; otherwise, false. |
||
179 | * @since 1.0.0 |
||
180 | */ |
||
181 | 2 | function view_exists( $view_id ) { |
|
184 | |||
185 | /** |
||
186 | * |
||
187 | * Add a view to the views array |
||
188 | * |
||
189 | * @param int|array $view_id View ID or array of View IDs |
||
190 | * @param array|string $atts Combine other attributes (eg. from shortcode) with the view settings (optional) |
||
191 | * @return array |
||
192 | */ |
||
193 | 2 | function add_view( $view_id, $atts = NULL ) { |
|
270 | |||
271 | /** |
||
272 | * Get the visible fields for a View |
||
273 | * @uses gravityview_get_directory_fields() Fetch the configured fields for a View |
||
274 | * @uses GravityView_View_Data::filter_fields() Only show visible fields |
||
275 | * @param int $view_id View ID |
||
276 | * @return array Array of fields as passed by `gravityview_get_directory_fields()` |
||
277 | */ |
||
278 | 2 | function get_fields( $view_id ) { |
|
289 | |||
290 | /** |
||
291 | * Filter area fields based on specified conditions |
||
292 | * |
||
293 | * @access public |
||
294 | * @param array $dir_fields |
||
295 | * @return array |
||
296 | */ |
||
297 | 2 | private function filter_fields( $dir_fields ) { |
|
317 | |||
318 | |||
319 | /** |
||
320 | * Check whether a certain field should not be presented based on its own properties. |
||
321 | * |
||
322 | * @access public |
||
323 | * @param array $properties |
||
324 | * @return boolean True: (field should be hidden) or False: (field should be presented) |
||
325 | */ |
||
326 | 2 | private function hide_field_check_conditions( $properties ) { |
|
335 | |||
336 | /** |
||
337 | * @deprecated Also dead code, was probably superceded by GravityView_View_Data::parse_post_content |
||
338 | */ |
||
339 | function get_id_from_atts( $atts ) { |
||
359 | |||
360 | /** |
||
361 | * Parse content to determine if there is a GV shortcode to allow for enqueing necessary files in the head. |
||
362 | * |
||
363 | * @uses gravityview_has_shortcode_r() Check whether shortcode exists (recursively) |
||
364 | * @uses shortcode_parse_atts() Parse each GV shortcode |
||
365 | * @uses gravityview_get_template_settings() Get the settings for the View ID |
||
366 | * @param string $content $post->post_content content |
||
367 | * @return int|null|array If a single View is found, the ID of the View. If there are multiple views in the content, array of IDs parsed. If not found, NULL |
||
368 | */ |
||
369 | public function parse_post_content( $content ) { |
||
417 | |||
418 | /** |
||
419 | * Parse specific custom fields (Post Meta) to determine if there is a GV shortcode to allow for enqueuing necessary files in the head. |
||
420 | * @since 1.15.1 |
||
421 | * @uses \GravityView_View_Data::parse_post_content |
||
422 | * @param int $post_id WP_Post ID |
||
423 | * @return int|null|array If a single View is found, the ID of the View. If there are multiple views in the content, array of IDs parsed. If not found, or meta not parsed, NULL |
||
424 | */ |
||
425 | private function parse_post_meta( $post_id ) { |
||
461 | |||
462 | /** |
||
463 | * Checks if the passed post id has the passed View id embedded. |
||
464 | * |
||
465 | * Returns |
||
466 | * |
||
467 | * @since 1.6.1 |
||
468 | * |
||
469 | * @param string $post_id Post ID where the View is embedded |
||
470 | * @param string $view_id View ID |
||
471 | * |
||
472 | * @return bool|WP_Error If valid, returns true. If invalid, returns WP_Error containing error message. |
||
473 | */ |
||
474 | 1 | public static function is_valid_embed_id( $post_id = '', $view_id = '', $empty_is_valid = true ) { |
|
475 | |||
476 | 1 | $message = NULL; |
|
477 | |||
478 | // Not invalid if not set! |
||
479 | 1 | if( empty( $post_id ) || empty( $view_id ) ) { |
|
480 | |||
481 | if( $empty_is_valid ) { |
||
482 | return true; |
||
483 | } |
||
484 | |||
485 | $message = esc_html__( 'The ID is required.', 'gravityview' ); |
||
486 | } |
||
487 | |||
488 | 1 | if( ! $message ) { |
|
489 | 1 | $status = get_post_status( $post_id ); |
|
490 | |||
491 | // Nothing exists with that post ID. |
||
492 | 1 | if ( ! is_numeric( $post_id ) ) { |
|
493 | $message = esc_html__( 'You did not enter a number. The value entered should be a number, representing the ID of the post or page the View is embedded on.', 'gravityview' ); |
||
494 | |||
495 | // @todo Convert to generic article about Embed IDs |
||
496 | $message .= ' ' . gravityview_get_link( 'http://docs.gravityview.co/article/222-the-search-widget', __( 'Learn more…', 'gravityview' ), 'target=_blank' ); |
||
497 | } |
||
498 | } |
||
499 | |||
500 | 1 | if( ! $message ) { |
|
501 | |||
502 | // Nothing exists with that post ID. |
||
503 | 1 | if ( empty( $status ) || in_array( $status, array( 'revision', 'attachment' ) ) ) { |
|
504 | $message = esc_html__( 'There is no post or page with that ID.', 'gravityview' ); |
||
505 | } |
||
506 | |||
507 | } |
||
508 | |||
509 | 1 | if( ! $message ) { |
|
510 | 1 | if ( false /** Do not use for now. See issue #848 */ && function_exists( 'gravityview' ) && $post = get_post( $post_id ) ) { |
|
511 | $views = GV\View_Collection::from_post( $post ); |
||
512 | $view_ids_in_post = array_map( function( $view ) { return $view->ID; }, $views->all() ); |
||
513 | } else { |
||
514 | /** ::maybe_get_view_id deprecated. */ |
||
515 | 1 | $view_ids_in_post = GravityView_View_Data::getInstance()->maybe_get_view_id( $post_id ); |
|
516 | } |
||
517 | |||
518 | // The post or page specified does not contain the shortcode. |
||
519 | 1 | if ( false === in_array( $view_id, (array) $view_ids_in_post ) ) { |
|
520 | 1 | $message = sprintf( esc_html__( 'The Post ID entered is not valid. You may have entered a post or page that does not contain the selected View. Make sure the post contains the following shortcode: %s', 'gravityview' ), '<br /><code>[gravityview id="' . intval( $view_id ) . '"]</code>' ); |
|
521 | } |
||
522 | } |
||
523 | |||
524 | 1 | if( ! $message ) { |
|
525 | |||
526 | // It's a View |
||
527 | 1 | if( 'gravityview' === get_post_type( $post_id ) ) { |
|
528 | $message = esc_html__( 'The ID is already a View.', 'gravityview' );; |
||
529 | } |
||
530 | |||
531 | } |
||
532 | |||
533 | 1 | if( $message ) { |
|
534 | 1 | return new WP_Error( 'invalid_embed_id', $message ); |
|
535 | } |
||
536 | |||
537 | 1 | return true; |
|
538 | } |
||
539 | |||
540 | /** |
||
541 | * Get a specific default setting |
||
542 | * @param string $key The key of the setting array item |
||
543 | * @param boolean $with_details Include details |
||
544 | * @return mixed|array If using $with_details, return array. Otherwise, mixed. |
||
545 | */ |
||
546 | public static function get_default_arg( $key, $with_details = false ) { |
||
554 | |||
555 | /** |
||
556 | * Retrieve the default args for shortcode and theme function |
||
557 | * |
||
558 | * @param boolean $with_details True: Return array with full default settings information, including description, name, etc. False: Return an array with only key => value pairs. |
||
559 | * @param string $group Only fetch |
||
560 | * |
||
561 | * @return array $args Associative array of default settings for a View |
||
562 | * @param[out] string $label Setting label shown in admin |
||
563 | * @param[out] string $type Gravity Forms field type |
||
564 | * @param[out] string $group The field group the setting is associated with. Default: "default" |
||
565 | * @param[out] mixed $value The default value for the setting |
||
566 | * @param[out] string $tooltip Tooltip displayed for the setting |
||
567 | * @param[out] boolean $show_in_shortcode Whether to show the setting in the shortcode configuration modal |
||
568 | * @param[out] array $options Array of values to use when generating select, multiselect, radio, or checkboxes fields |
||
569 | * @param[out] boolean $full_width True: Display the input and label together when rendering. False: Display label and input in separate columns when rendering. |
||
570 | */ |
||
571 | 5 | public static function get_default_args( $with_details = false, $group = NULL ) { |
|
800 | |||
801 | |||
802 | } |
||
803 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.