Conditions | 16 |
Paths | 18432 |
Total Lines | 133 |
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 |
||
176 | function get_initial_state() { |
||
177 | // Load API endpoint base classes and endpoints for getting the module list fed into the JS Admin Page |
||
178 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-xmlrpc-consumer-endpoint.php'; |
||
179 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php'; |
||
180 | $moduleListEndpoint = new Jetpack_Core_API_Module_List_Endpoint(); |
||
181 | $modules = $moduleListEndpoint->get_modules(); |
||
182 | |||
183 | // Preparing translated fields for JSON encoding by transforming all HTML entities to |
||
184 | // respective characters. |
||
185 | foreach( $modules as $slug => $data ) { |
||
|
|||
186 | $modules[ $slug ]['name'] = html_entity_decode( $data['name'] ); |
||
187 | $modules[ $slug ]['description'] = html_entity_decode( $data['description'] ); |
||
188 | $modules[ $slug ]['short_description'] = html_entity_decode( $data['short_description'] ); |
||
189 | $modules[ $slug ]['long_description'] = html_entity_decode( $data['long_description'] ); |
||
190 | } |
||
191 | |||
192 | // Collecting roles that can view site stats. |
||
193 | $stats_roles = array(); |
||
194 | $enabled_roles = function_exists( 'stats_get_option' ) ? stats_get_option( 'roles' ) : array( 'administrator' ); |
||
195 | |||
196 | if ( ! function_exists( 'get_editable_roles' ) ) { |
||
197 | require_once ABSPATH . 'wp-admin/includes/user.php'; |
||
198 | } |
||
199 | foreach ( get_editable_roles() as $slug => $role ) { |
||
200 | $stats_roles[ $slug ] = array( |
||
201 | 'name' => translate_user_role( $role['name'] ), |
||
202 | 'canView' => is_array( $enabled_roles ) ? in_array( $slug, $enabled_roles, true ) : false, |
||
203 | ); |
||
204 | } |
||
205 | |||
206 | // Get information about current theme. |
||
207 | $current_theme = wp_get_theme(); |
||
208 | |||
209 | // Get all themes that Infinite Scroll provides support for natively. |
||
210 | $inf_scr_support_themes = array(); |
||
211 | foreach ( Jetpack::glob_php( JETPACK__PLUGIN_DIR . 'modules/infinite-scroll/themes' ) as $path ) { |
||
212 | if ( is_readable( $path ) ) { |
||
213 | $inf_scr_support_themes[] = basename( $path, '.php' ); |
||
214 | } |
||
215 | } |
||
216 | |||
217 | // Get last post, to build the link to Customizer in the Related Posts module. |
||
218 | $last_post = get_posts( array( 'posts_per_page' => 1 ) ); |
||
219 | $last_post = isset( $last_post[0] ) && $last_post[0] instanceof WP_Post |
||
220 | ? get_permalink( $last_post[0]->ID ) |
||
221 | : get_home_url(); |
||
222 | |||
223 | $current_user_data = jetpack_current_user_data(); |
||
224 | |||
225 | $status = new Status(); |
||
226 | |||
227 | return array( |
||
228 | 'WP_API_root' => esc_url_raw( rest_url() ), |
||
229 | 'WP_API_nonce' => wp_create_nonce( 'wp_rest' ), |
||
230 | 'pluginBaseUrl' => plugins_url( '', JETPACK__PLUGIN_FILE ), |
||
231 | 'connectionStatus' => array( |
||
232 | 'isActive' => Jetpack::is_active(), |
||
233 | 'isStaging' => $status->is_staging_site(), |
||
234 | 'devMode' => array( |
||
235 | 'isActive' => $status->is_development_mode(), |
||
236 | 'constant' => defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG, |
||
237 | 'url' => site_url() && false === strpos( site_url(), '.' ), |
||
238 | 'filter' => apply_filters( 'jetpack_development_mode', false ), |
||
239 | ), |
||
240 | 'isPublic' => '1' == get_option( 'blog_public' ), // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
||
241 | 'isInIdentityCrisis' => Jetpack::validate_sync_error_idc_option(), |
||
242 | 'sandboxDomain' => JETPACK__SANDBOX_DOMAIN, |
||
243 | ), |
||
244 | 'connectUrl' => false == $current_user_data['isConnected'] // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
||
245 | ? Jetpack::init()->build_connect_url( true, false, false ) |
||
246 | : '', |
||
247 | 'dismissedNotices' => $this->get_dismissed_jetpack_notices(), |
||
248 | 'isDevVersion' => Jetpack::is_development_version(), |
||
249 | 'currentVersion' => JETPACK__VERSION, |
||
250 | 'is_gutenberg_available' => true, |
||
251 | 'getModules' => $modules, |
||
252 | 'rawUrl' => Jetpack::build_raw_urls( get_home_url() ), |
||
253 | 'adminUrl' => esc_url( admin_url() ), |
||
254 | 'stats' => array( |
||
255 | // data is populated asynchronously on page load. |
||
256 | 'data' => array( |
||
257 | 'general' => false, |
||
258 | 'day' => false, |
||
259 | 'week' => false, |
||
260 | 'month' => false, |
||
261 | ), |
||
262 | 'roles' => $stats_roles, |
||
263 | ), |
||
264 | 'aff' => Partner::init()->get_partner_code( Partner::AFFILIATE_CODE ), |
||
265 | 'partnerSubsidiaryId' => Partner::init()->get_partner_code( Partner::SUBSIDIARY_CODE ), |
||
266 | 'settings' => $this->get_flattened_settings( $modules ), |
||
267 | 'userData' => array( |
||
268 | 'currentUser' => $current_user_data, |
||
269 | ), |
||
270 | 'siteData' => array( |
||
271 | 'icon' => has_site_icon() |
||
272 | ? apply_filters( 'jetpack_photon_url', get_site_icon_url(), array( 'w' => 64 ) ) |
||
273 | : '', |
||
274 | 'siteVisibleToSearchEngines' => '1' == get_option( 'blog_public' ), // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
||
275 | /** |
||
276 | * Whether promotions are visible or not. |
||
277 | * |
||
278 | * @since 4.8.0 |
||
279 | * |
||
280 | * @param bool $are_promotions_active Status of promotions visibility. True by default. |
||
281 | */ |
||
282 | 'showPromotions' => apply_filters( 'jetpack_show_promotions', true ), |
||
283 | 'isAtomicSite' => jetpack_is_atomic_site(), |
||
284 | 'plan' => Jetpack_Plan::get(), |
||
285 | 'showBackups' => Jetpack::show_backups_ui(), |
||
286 | 'isMultisite' => is_multisite(), |
||
287 | ), |
||
288 | 'themeData' => array( |
||
289 | 'name' => $current_theme->get( 'Name' ), |
||
290 | 'hasUpdate' => (bool) get_theme_update_available( $current_theme ), |
||
291 | 'support' => array( |
||
292 | 'infinite-scroll' => current_theme_supports( 'infinite-scroll' ) || in_array( $current_theme->get_stylesheet(), $inf_scr_support_themes, true ), |
||
293 | ), |
||
294 | ), |
||
295 | 'locale' => Jetpack::get_i18n_data_json(), |
||
296 | 'localeSlug' => join( '-', explode( '_', get_user_locale() ) ), |
||
297 | 'jetpackStateNotices' => array( |
||
298 | 'messageCode' => Jetpack::state( 'message' ), |
||
299 | 'errorCode' => Jetpack::state( 'error' ), |
||
300 | 'errorDescription' => Jetpack::state( 'error_description' ), |
||
301 | ), |
||
302 | 'tracksUserData' => Jetpack_Tracks_Client::get_connected_user_tracks_identity(), |
||
303 | 'currentIp' => function_exists( 'jetpack_protect_get_ip' ) ? jetpack_protect_get_ip() : false, |
||
304 | 'lastPostUrl' => esc_url( $last_post ), |
||
305 | 'externalServicesConnectUrls' => $this->get_external_services_connect_urls(), |
||
306 | 'calypsoEnv' => Jetpack::get_calypso_env(), |
||
307 | ); |
||
308 | } |
||
309 | |||
383 |
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.