Conditions | 16 |
Paths | 18432 |
Total Lines | 152 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
24 | public static function get_initial_state() { |
||
25 | global $is_safari; |
||
26 | |||
27 | // Load API endpoint base classes and endpoints for getting the module list fed into the JS Admin Page. |
||
28 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-xmlrpc-consumer-endpoint.php'; |
||
29 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php'; |
||
30 | $module_list_endpoint = new Jetpack_Core_API_Module_List_Endpoint(); |
||
31 | $modules = $module_list_endpoint->get_modules(); |
||
32 | |||
33 | // Preparing translated fields for JSON encoding by transforming all HTML entities to |
||
34 | // respective characters. |
||
35 | foreach ( $modules as $slug => $data ) { |
||
|
|||
36 | $modules[ $slug ]['name'] = html_entity_decode( $data['name'] ); |
||
37 | $modules[ $slug ]['description'] = html_entity_decode( $data['description'] ); |
||
38 | $modules[ $slug ]['short_description'] = html_entity_decode( $data['short_description'] ); |
||
39 | $modules[ $slug ]['long_description'] = html_entity_decode( $data['long_description'] ); |
||
40 | } |
||
41 | |||
42 | // Collecting roles that can view site stats. |
||
43 | $stats_roles = array(); |
||
44 | $enabled_roles = function_exists( 'stats_get_option' ) ? stats_get_option( 'roles' ) : array( 'administrator' ); |
||
45 | |||
46 | if ( ! function_exists( 'get_editable_roles' ) ) { |
||
47 | require_once ABSPATH . 'wp-admin/includes/user.php'; |
||
48 | } |
||
49 | foreach ( get_editable_roles() as $slug => $role ) { |
||
50 | $stats_roles[ $slug ] = array( |
||
51 | 'name' => translate_user_role( $role['name'] ), |
||
52 | 'canView' => is_array( $enabled_roles ) ? in_array( $slug, $enabled_roles, true ) : false, |
||
53 | ); |
||
54 | } |
||
55 | |||
56 | // Get information about current theme. |
||
57 | $current_theme = wp_get_theme(); |
||
58 | |||
59 | // Get all themes that Infinite Scroll provides support for natively. |
||
60 | $inf_scr_support_themes = array(); |
||
61 | foreach ( Jetpack::glob_php( JETPACK__PLUGIN_DIR . 'modules/infinite-scroll/themes' ) as $path ) { |
||
62 | if ( is_readable( $path ) ) { |
||
63 | $inf_scr_support_themes[] = basename( $path, '.php' ); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | // Get last post, to build the link to Customizer in the Related Posts module. |
||
68 | $last_post = get_posts( array( 'posts_per_page' => 1 ) ); |
||
69 | $last_post = isset( $last_post[0] ) && $last_post[0] instanceof WP_Post |
||
70 | ? get_permalink( $last_post[0]->ID ) |
||
71 | : get_home_url(); |
||
72 | |||
73 | $current_user_data = jetpack_current_user_data(); |
||
74 | |||
75 | /** |
||
76 | * Adds information to the `connectionStatus` API field that is unique to the Jetpack React dashboard. |
||
77 | */ |
||
78 | $connection_status = array( |
||
79 | 'isInIdentityCrisis' => Jetpack::validate_sync_error_idc_option(), |
||
80 | 'sandboxDomain' => JETPACK__SANDBOX_DOMAIN, |
||
81 | |||
82 | /** |
||
83 | * Filter to add connection errors |
||
84 | * Format: array( array( 'code' => '...', 'message' => '...', 'action' => '...' ), ... ) |
||
85 | * |
||
86 | * @since 8.7.0 |
||
87 | * |
||
88 | * @param array $errors Connection errors. |
||
89 | */ |
||
90 | 'errors' => apply_filters( 'react_connection_errors_initial_state', array() ), |
||
91 | ); |
||
92 | |||
93 | $connection_status = array_merge( REST_Connector::connection_status( false ), $connection_status ); |
||
94 | |||
95 | return array( |
||
96 | 'WP_API_root' => esc_url_raw( rest_url() ), |
||
97 | 'WP_API_nonce' => wp_create_nonce( 'wp_rest' ), |
||
98 | 'purchaseToken' => self::get_purchase_token(), |
||
99 | 'pluginBaseUrl' => plugins_url( '', JETPACK__PLUGIN_FILE ), |
||
100 | 'connectionStatus' => $connection_status, |
||
101 | 'connectUrl' => false == $current_user_data['isConnected'] // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
||
102 | ? Jetpack::init()->build_connect_url( true, false, false ) |
||
103 | : '', |
||
104 | 'dismissedNotices' => self::get_dismissed_jetpack_notices(), |
||
105 | 'isDevVersion' => Jetpack::is_development_version(), |
||
106 | 'currentVersion' => JETPACK__VERSION, |
||
107 | 'is_gutenberg_available' => true, |
||
108 | 'getModules' => $modules, |
||
109 | 'rawUrl' => ( new Status() )->get_site_suffix(), |
||
110 | 'adminUrl' => esc_url( admin_url() ), |
||
111 | 'siteTitle' => (string) htmlspecialchars_decode( get_option( 'blogname' ), ENT_QUOTES ), |
||
112 | 'stats' => array( |
||
113 | // data is populated asynchronously on page load. |
||
114 | 'data' => array( |
||
115 | 'general' => false, |
||
116 | 'day' => false, |
||
117 | 'week' => false, |
||
118 | 'month' => false, |
||
119 | ), |
||
120 | 'roles' => $stats_roles, |
||
121 | ), |
||
122 | 'aff' => Partner::init()->get_partner_code( Partner::AFFILIATE_CODE ), |
||
123 | 'partnerSubsidiaryId' => Partner::init()->get_partner_code( Partner::SUBSIDIARY_CODE ), |
||
124 | 'settings' => self::get_flattened_settings( $modules ), |
||
125 | 'userData' => array( |
||
126 | 'currentUser' => $current_user_data, |
||
127 | ), |
||
128 | 'siteData' => array( |
||
129 | 'icon' => has_site_icon() |
||
130 | ? apply_filters( 'jetpack_photon_url', get_site_icon_url(), array( 'w' => 64 ) ) |
||
131 | : '', |
||
132 | 'siteVisibleToSearchEngines' => '1' == get_option( 'blog_public' ), // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
||
133 | /** |
||
134 | * Whether promotions are visible or not. |
||
135 | * |
||
136 | * @since 4.8.0 |
||
137 | * |
||
138 | * @param bool $are_promotions_active Status of promotions visibility. True by default. |
||
139 | */ |
||
140 | 'showPromotions' => apply_filters( 'jetpack_show_promotions', true ), |
||
141 | 'isAtomicSite' => jetpack_is_atomic_site(), |
||
142 | 'plan' => Jetpack_Plan::get(), |
||
143 | 'showBackups' => Jetpack::show_backups_ui(), |
||
144 | 'showRecommendations' => Jetpack_Recommendations::is_enabled(), |
||
145 | 'isMultisite' => is_multisite(), |
||
146 | 'dateFormat' => get_option( 'date_format' ), |
||
147 | ), |
||
148 | 'themeData' => array( |
||
149 | 'name' => $current_theme->get( 'Name' ), |
||
150 | 'hasUpdate' => (bool) get_theme_update_available( $current_theme ), |
||
151 | 'support' => array( |
||
152 | 'infinite-scroll' => current_theme_supports( 'infinite-scroll' ) || in_array( $current_theme->get_stylesheet(), $inf_scr_support_themes, true ), |
||
153 | ), |
||
154 | ), |
||
155 | 'jetpackStateNotices' => array( |
||
156 | 'messageCode' => Jetpack::state( 'message' ), |
||
157 | 'errorCode' => Jetpack::state( 'error' ), |
||
158 | 'errorDescription' => Jetpack::state( 'error_description' ), |
||
159 | 'messageContent' => Jetpack::state( 'display_update_modal' ) ? self::get_update_modal_data() : null, |
||
160 | ), |
||
161 | 'tracksUserData' => Jetpack_Tracks_Client::get_connected_user_tracks_identity(), |
||
162 | 'currentIp' => function_exists( 'jetpack_protect_get_ip' ) ? jetpack_protect_get_ip() : false, |
||
163 | 'lastPostUrl' => esc_url( $last_post ), |
||
164 | 'externalServicesConnectUrls' => self::get_external_services_connect_urls(), |
||
165 | 'calypsoEnv' => Jetpack::get_calypso_env(), |
||
166 | 'products' => Jetpack::get_products_for_purchase(), |
||
167 | 'recommendationsStep' => Jetpack_Core_Json_Api_Endpoints::get_recommendations_step()['step'], |
||
168 | 'isSafari' => $is_safari || User_Agent_Info::is_opera_desktop(), // @todo Rename isSafari everywhere. |
||
169 | 'doNotUseConnectionIframe' => Constants::is_true( 'JETPACK_SHOULD_NOT_USE_CONNECTION_IFRAME' ), |
||
170 | 'licensing' => array( |
||
171 | 'error' => Licensing::instance()->last_error(), |
||
172 | 'showLicensingUi' => Licensing::instance()->is_licensing_input_enabled(), |
||
173 | ), |
||
174 | ); |
||
175 | } |
||
176 | |||
415 |
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.