| Conditions | 13 |
| Paths | 264 |
| Total Lines | 83 |
| Code Lines | 43 |
| Lines | 17 |
| Ratio | 20.48 % |
| 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 |
||
| 60 | function get_modules() { |
||
| 61 | include_once( JETPACK__PLUGIN_DIR . 'modules/module-info.php' ); |
||
| 62 | $available_modules = $this->jetpack->get_available_modules(); |
||
| 63 | $active_modules = $this->jetpack->get_active_modules(); |
||
| 64 | $modules = array(); |
||
| 65 | $jetpack_active = Jetpack::is_active() || Jetpack::is_development_mode(); |
||
| 66 | foreach ( $available_modules as $module ) { |
||
| 67 | if ( $module_array = $this->jetpack->get_module( $module ) ) { |
||
| 68 | $short_desc = apply_filters( 'jetpack_short_module_description', $module_array['description'], $module ); |
||
| 69 | // Fix: correct multibyte strings truncate with checking for mbstring extension |
||
| 70 | $short_desc_trunc = ( function_exists( 'mb_strlen' ) ) |
||
| 71 | ? ( ( mb_strlen( $short_desc ) > 143 ) |
||
| 72 | ? mb_substr( $short_desc, 0, 140 ) . '...' |
||
| 73 | : $short_desc ) |
||
| 74 | : ( ( strlen( $short_desc ) > 143 ) |
||
| 75 | ? substr( $short_desc, 0, 140 ) . '...' |
||
| 76 | : $short_desc ); |
||
| 77 | |||
| 78 | $module_array['module'] = $module; |
||
| 79 | $module_array['activated'] = ( $jetpack_active ? in_array( $module, $active_modules ) : false ); |
||
| 80 | $module_array['deactivate_nonce'] = wp_create_nonce( 'jetpack_deactivate-' . $module ); |
||
| 81 | $module_array['activate_nonce'] = wp_create_nonce( 'jetpack_activate-' . $module ); |
||
| 82 | $module_array['available'] = self::is_module_available( $module_array ); |
||
| 83 | $module_array['short_description'] = $short_desc_trunc; |
||
| 84 | $module_array['configure_url'] = Jetpack::module_configuration_url( $module ); |
||
| 85 | |||
| 86 | ob_start(); |
||
| 87 | do_action( 'jetpack_learn_more_button_' . $module ); |
||
| 88 | $module_array['learn_more_button'] = ob_get_clean(); |
||
| 89 | |||
| 90 | ob_start(); |
||
| 91 | View Code Duplication | if ( Jetpack::is_active() && has_action( 'jetpack_module_more_info_connected_' . $module ) ) { |
|
| 92 | do_action( 'jetpack_module_more_info_connected_' . $module ); |
||
| 93 | } else { |
||
| 94 | do_action( 'jetpack_module_more_info_' . $module ); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Filter the long description of a module. |
||
| 99 | * |
||
| 100 | * @since 3.5.0 |
||
| 101 | * |
||
| 102 | * @param string ob_get_clean() The module long description. |
||
| 103 | * @param string $module The module name. |
||
| 104 | */ |
||
| 105 | $module_array['long_description'] = apply_filters( 'jetpack_long_module_description', ob_get_clean(), $module ); |
||
| 106 | |||
| 107 | ob_start(); |
||
| 108 | /** |
||
| 109 | * Filter the search terms for a module |
||
| 110 | * |
||
| 111 | * Search terms are be typically added to a module in module-info.php. |
||
| 112 | * |
||
| 113 | * Use syntax: |
||
| 114 | * function jetpack_$module_search_terms( $terms ) { |
||
| 115 | * $terms = _x( 'term 1, term 2', 'search terms', 'jetpack' ); |
||
| 116 | * return $terms; |
||
| 117 | * } |
||
| 118 | * add_filter( 'jetpack_search_terms_$module', 'jetpack_$module_search_terms' ); |
||
| 119 | * |
||
| 120 | * @since 3.5.0 |
||
| 121 | * @param string The search terms (comma separated) |
||
| 122 | */ |
||
| 123 | echo apply_filters( 'jetpack_search_terms_' . $module, '' ); |
||
| 124 | $module_array['search_terms'] = ob_get_clean(); |
||
| 125 | |||
| 126 | $module_array['configurable'] = false; |
||
| 127 | if ( current_user_can( 'manage_options' ) && apply_filters( 'jetpack_module_configurable_' . $module, false ) ) { |
||
| 128 | $module_array['configurable'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( Jetpack::module_configuration_url( $module ) ), __( 'Configure', 'jetpack' ) ); |
||
| 129 | } |
||
| 130 | |||
| 131 | $modules[ $module ] = $module_array; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | uasort( $modules, array( $this->jetpack, 'sort_modules' ) ); |
||
| 136 | |||
| 137 | if ( ! Jetpack::is_active() ) { |
||
| 138 | uasort( $modules, array( __CLASS__, 'sort_requires_connection_last' ) ); |
||
| 139 | } |
||
| 140 | |||
| 141 | return $modules; |
||
| 142 | } |
||
| 143 | |||
| 223 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: