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 Jetpack_React_Page 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 Jetpack_React_Page, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Jetpack_React_Page extends Jetpack_Admin_Page { |
||
10 | |||
11 | protected $dont_show_if_not_active = false; |
||
12 | |||
13 | protected $is_redirecting = false; |
||
14 | |||
15 | function get_page_hook() { |
||
16 | // Add the main admin Jetpack menu |
||
17 | return add_menu_page( 'Jetpack', 'Jetpack', 'jetpack_admin_page', 'jetpack', array( $this, 'render' ), 'div' ); |
||
18 | } |
||
19 | |||
20 | function add_page_actions( $hook ) { |
||
21 | /** This action is documented in class.jetpack.php */ |
||
22 | do_action( 'jetpack_admin_menu', $hook ); |
||
23 | |||
24 | // Place the Jetpack menu item on top and others in the order they appear |
||
25 | add_filter( 'custom_menu_order', '__return_true' ); |
||
26 | add_filter( 'menu_order', array( $this, 'jetpack_menu_order' ) ); |
||
27 | |||
28 | if ( ! isset( $_GET['page'] ) || 'jetpack' !== $_GET['page'] ) { |
||
29 | return; // No need to handle the fallback redirection if we are not on the Jetpack page |
||
30 | } |
||
31 | |||
32 | // Adding a redirect meta tag if the REST API is disabled |
||
33 | if ( ! $this->is_rest_api_enabled() ) { |
||
34 | $this->is_redirecting = true; |
||
35 | add_action( 'admin_head', array( $this, 'add_fallback_head_meta' ) ); |
||
36 | } |
||
37 | |||
38 | // Adding a redirect meta tag wrapped in noscript tags for all browsers in case they have JavaScript disabled |
||
39 | add_action( 'admin_head', array( $this, 'add_noscript_head_meta' ) ); |
||
40 | |||
41 | // If this is the first time the user is viewing the admin, don't show JITMs. |
||
42 | // This filter is added just in time because this function is called on admin_menu |
||
43 | // and JITMs are initialized on admin_init |
||
44 | if ( Jetpack::is_active() && ! Jetpack_Options::get_option( 'first_admin_view', false ) ) { |
||
45 | Jetpack_Options::update_option( 'first_admin_view', true ); |
||
46 | add_filter( 'jetpack_just_in_time_msgs', '__return_false' ); |
||
47 | } |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Add Jetpack Setup sub-link for eligible users |
||
52 | */ |
||
53 | function jetpack_add_set_up_sub_nav_item() { |
||
54 | if ( $this->show_setup_wizard() ) { |
||
55 | global $submenu; |
||
56 | $submenu['jetpack'][] = array( __( 'Set up', 'jetpack' ), 'jetpack_admin_page', 'admin.php?page=jetpack#/setup' ); |
||
57 | } |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Add Jetpack Dashboard sub-link and point it to AAG if the user can view stats, manage modules or if Protect is active. |
||
62 | * |
||
63 | * Works in Dev Mode or when user is connected. |
||
64 | * |
||
65 | * @since 4.3.0 |
||
66 | */ |
||
67 | function jetpack_add_dashboard_sub_nav_item() { |
||
68 | View Code Duplication | if ( ( new Status() )->is_development_mode() || Jetpack::is_active() ) { |
|
69 | global $submenu; |
||
70 | if ( current_user_can( 'jetpack_admin_page' ) ) { |
||
71 | $submenu['jetpack'][] = array( __( 'Dashboard', 'jetpack' ), 'jetpack_admin_page', 'admin.php?page=jetpack#/dashboard' ); |
||
72 | } |
||
73 | } |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * If user is allowed to see the Jetpack Admin, add Settings sub-link. |
||
78 | * |
||
79 | * @since 4.3.0 |
||
80 | */ |
||
81 | function jetpack_add_settings_sub_nav_item() { |
||
82 | View Code Duplication | if ( ( ( new Status() )->is_development_mode() || Jetpack::is_active() ) && current_user_can( 'jetpack_admin_page' ) && current_user_can( 'edit_posts' ) ) { |
|
83 | global $submenu; |
||
84 | $submenu['jetpack'][] = array( __( 'Settings', 'jetpack' ), 'jetpack_admin_page', 'admin.php?page=jetpack#/settings' ); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | function add_fallback_head_meta() { |
||
89 | echo '<meta http-equiv="refresh" content="0; url=?page=jetpack_modules">'; |
||
90 | } |
||
91 | |||
92 | function add_noscript_head_meta() { |
||
93 | echo '<noscript>'; |
||
94 | $this->add_fallback_head_meta(); |
||
95 | echo '</noscript>'; |
||
96 | } |
||
97 | |||
98 | View Code Duplication | function jetpack_menu_order( $menu_order ) { |
|
99 | $jp_menu_order = array(); |
||
100 | |||
101 | foreach ( $menu_order as $index => $item ) { |
||
102 | if ( $item != 'jetpack' ) |
||
103 | $jp_menu_order[] = $item; |
||
104 | |||
105 | if ( $index == 0 ) |
||
106 | $jp_menu_order[] = 'jetpack'; |
||
107 | } |
||
108 | |||
109 | return $jp_menu_order; |
||
110 | } |
||
111 | |||
112 | function page_render() { |
||
113 | /** This action is already documented in views/admin/admin-page.php */ |
||
114 | do_action( 'jetpack_notices' ); |
||
115 | |||
116 | // Try fetching by patch |
||
117 | $static_html = @file_get_contents( JETPACK__PLUGIN_DIR . '_inc/build/static.html' ); |
||
118 | |||
119 | if ( false === $static_html ) { |
||
120 | |||
121 | // If we still have nothing, display an error |
||
122 | echo '<p>'; |
||
123 | esc_html_e( 'Error fetching static.html. Try running: ', 'jetpack' ); |
||
124 | echo '<code>yarn distclean && yarn build</code>'; |
||
125 | echo '</p>'; |
||
126 | } else { |
||
127 | |||
128 | // We got the static.html so let's display it |
||
129 | echo $static_html; |
||
130 | } |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Gets array of any Jetpack notices that have been dismissed. |
||
135 | * |
||
136 | * @since 4.0.1 |
||
137 | * @return mixed|void |
||
138 | */ |
||
139 | function get_dismissed_jetpack_notices() { |
||
151 | |||
152 | function additional_styles() { |
||
155 | |||
156 | function page_admin_scripts() { |
||
157 | if ( $this->is_redirecting ) { |
||
158 | return; // No need for scripts on a fallback page |
||
159 | } |
||
160 | |||
161 | |||
162 | $is_development_mode = ( new Status() )->is_development_mode(); |
||
163 | $script_deps_path = JETPACK__PLUGIN_DIR . '_inc/build/admin.asset.php'; |
||
187 | |||
188 | function get_initial_state() { |
||
326 | |||
327 | function get_external_services_connect_urls() { |
||
335 | |||
336 | /** |
||
337 | * Returns an array of modules and settings both as first class members of the object. |
||
338 | * |
||
339 | * @param array $modules the result of an API request to get all modules. |
||
340 | * |
||
341 | * @return array flattened settings with modules. |
||
342 | */ |
||
343 | function get_flattened_settings( $modules ) { |
||
348 | |||
349 | |||
350 | /** |
||
351 | * Returns a boolean for whether the Setup Wizard should be displayed or not. |
||
352 | * |
||
353 | * @return bool True if the Setup Wizard should be displayed, false otherwise. |
||
354 | */ |
||
355 | public function show_setup_wizard() { |
||
358 | |||
359 | /** |
||
360 | * Returns the release post content and image data as an associative array. |
||
361 | * This data is used to create the update modal. |
||
362 | */ |
||
363 | public function get_update_modal_data() { |
||
415 | |||
416 | /** |
||
417 | * Temporarily allow post content to contain iframes, e.g. for videopress. |
||
418 | * |
||
419 | * @param string $tags The tags. |
||
420 | * @param string $context The context. |
||
421 | */ |
||
422 | public function allow_post_embed_iframe( $tags, $context ) { |
||
435 | |||
436 | /** |
||
437 | * Obtains the release post from the Jetpack release post blog. A release post will be displayed in the |
||
438 | * update modal when a post has a tag equal to the Jetpack version number. |
||
439 | * |
||
440 | * The response parameters for the post array can be found here: |
||
441 | * https://developer.wordpress.com/docs/api/1.1/get/sites/%24site/posts/%24post_ID/#apidoc-response |
||
442 | * |
||
443 | * @return array|null Returns an associative array containing the release post data at index ['posts'][0]. |
||
444 | * Returns null if the release post data is not available. |
||
445 | */ |
||
446 | private function get_release_post_data() { |
||
468 | } |
||
469 | |||
520 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.