Conditions | 11 |
Paths | 136 |
Total Lines | 113 |
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 |
||
110 | function get_modules() { |
||
111 | include_once JETPACK__PLUGIN_DIR . 'modules/module-info.php'; |
||
112 | $available_modules = Jetpack::get_available_modules(); |
||
113 | $active_modules = Jetpack::get_active_modules(); |
||
114 | $modules = array(); |
||
115 | $jetpack_active = Jetpack::is_active() || ( new Status() )->is_offline_mode(); |
||
116 | $overrides = Jetpack_Modules_Overrides::instance(); |
||
117 | foreach ( $available_modules as $module ) { |
||
118 | if ( $module_array = Jetpack::get_module( $module ) ) { |
||
119 | /** |
||
120 | * Filters each module's short description. |
||
121 | * |
||
122 | * @since 3.0.0 |
||
123 | * |
||
124 | * @param string $module_array['description'] Module description. |
||
125 | * @param string $module Module slug. |
||
126 | */ |
||
127 | $short_desc = apply_filters( 'jetpack_short_module_description', $module_array['description'], $module ); |
||
128 | // Fix: correct multibyte strings truncate with checking for mbstring extension |
||
129 | $short_desc_trunc = ( function_exists( 'mb_strlen' ) ) |
||
130 | ? ( ( mb_strlen( $short_desc ) > 143 ) |
||
131 | ? mb_substr( $short_desc, 0, 140 ) . '...' |
||
132 | : $short_desc ) |
||
133 | : ( ( strlen( $short_desc ) > 143 ) |
||
134 | ? substr( $short_desc, 0, 140 ) . '...' |
||
135 | : $short_desc ); |
||
136 | |||
137 | $module_array['module'] = $module; |
||
138 | $module_array['activated'] = ( $jetpack_active ? in_array( $module, $active_modules ) : false ); |
||
139 | $module_array['deactivate_nonce'] = wp_create_nonce( 'jetpack_deactivate-' . $module ); |
||
140 | $module_array['activate_nonce'] = wp_create_nonce( 'jetpack_activate-' . $module ); |
||
141 | $module_array['available'] = self::is_module_available( $module_array ); |
||
142 | $module_array['short_description'] = $short_desc_trunc; |
||
143 | $module_array['configure_url'] = Jetpack::module_configuration_url( $module ); |
||
144 | $module_array['override'] = $overrides->get_module_override( $module ); |
||
145 | |||
146 | ob_start(); |
||
147 | /** |
||
148 | * Allow the display of a "Learn More" button. |
||
149 | * The dynamic part of the action, $module, is the module slug. |
||
150 | * |
||
151 | * @since 3.0.0 |
||
152 | */ |
||
153 | do_action( 'jetpack_learn_more_button_' . $module ); |
||
154 | $module_array['learn_more_button'] = ob_get_clean(); |
||
155 | |||
156 | ob_start(); |
||
157 | /** |
||
158 | * Allow the display of information text when Jetpack is connected to WordPress.com. |
||
159 | * The dynamic part of the action, $module, is the module slug. |
||
160 | * |
||
161 | * @since 3.0.0 |
||
162 | */ |
||
163 | do_action( 'jetpack_module_more_info_' . $module ); |
||
164 | |||
165 | /** |
||
166 | * Filter the long description of a module. |
||
167 | * |
||
168 | * @since 3.5.0 |
||
169 | * |
||
170 | * @param string ob_get_clean() The module long description. |
||
171 | * @param string $module The module name. |
||
172 | */ |
||
173 | $module_array['long_description'] = apply_filters( 'jetpack_long_module_description', ob_get_clean(), $module ); |
||
174 | |||
175 | ob_start(); |
||
176 | /** |
||
177 | * Filter the search terms for a module |
||
178 | * |
||
179 | * Search terms are typically added to the module headers, under "Additional Search Queries". |
||
180 | * |
||
181 | * Use syntax: |
||
182 | * function jetpack_$module_search_terms( $terms ) { |
||
183 | * $terms = _x( 'term 1, term 2', 'search terms', 'jetpack' ); |
||
184 | * return $terms; |
||
185 | * } |
||
186 | * add_filter( 'jetpack_search_terms_$module', 'jetpack_$module_search_terms' ); |
||
187 | * |
||
188 | * @since 3.5.0 |
||
189 | * |
||
190 | * @param string The search terms (comma separated). |
||
191 | */ |
||
192 | echo apply_filters( 'jetpack_search_terms_' . $module, $module_array['additional_search_queries'] ); |
||
193 | $module_array['search_terms'] = ob_get_clean(); |
||
194 | |||
195 | $module_array['configurable'] = false; |
||
196 | if ( |
||
197 | current_user_can( 'manage_options' ) && |
||
198 | /** |
||
199 | * Allow the display of a configuration link in the Jetpack Settings screen. |
||
200 | * |
||
201 | * @since 3.0.0 |
||
202 | * |
||
203 | * @param string $module Module name. |
||
204 | * @param bool false Should the Configure module link be displayed? Default to false. |
||
205 | */ |
||
206 | apply_filters( 'jetpack_module_configurable_' . $module, false ) |
||
207 | ) { |
||
208 | $module_array['configurable'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( $module_array['configure_url'] ), __( 'Configure', 'jetpack' ) ); |
||
209 | } |
||
210 | |||
211 | $modules[ $module ] = $module_array; |
||
212 | } |
||
213 | } |
||
214 | |||
215 | uasort( $modules, array( 'Jetpack', 'sort_modules' ) ); |
||
216 | |||
217 | if ( ! Jetpack::is_active() ) { |
||
218 | uasort( $modules, array( __CLASS__, 'sort_requires_connection_last' ) ); |
||
219 | } |
||
220 | |||
221 | return $modules; |
||
222 | } |
||
223 | |||
369 |
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: