Conditions | 41 |
Paths | 103 |
Total Lines | 121 |
Code Lines | 76 |
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 |
||
188 | public function get_styles() { |
||
189 | // Get an array of all our fields |
||
190 | $fields = self::$fields; |
||
191 | // Check if we need to exit early |
||
192 | if ( empty( self::$fields ) || ! is_array( $fields ) ) { |
||
193 | return; |
||
194 | } |
||
195 | // initially we're going to format our styles as an array. |
||
196 | // This is going to make processing them a lot easier |
||
197 | // and make sure there are no duplicate styles etc. |
||
198 | $css = array(); |
||
199 | // start parsing our fields |
||
200 | foreach ( $fields as $field ) { |
||
201 | // No need to process fields without an output, or an improperly-formatted output |
||
202 | if ( ! isset( $field['output'] ) || empty( $field['output'] ) || ! is_array( $field['output'] ) ) { |
||
203 | continue; |
||
204 | } |
||
205 | // Get the value of this field |
||
206 | $value = self::get_option( $field['kirki_config'], $field['settings'] ); |
||
207 | // start parsing the output arguments of the field |
||
208 | foreach ( $field['output'] as $output ) { |
||
209 | $defaults = array( |
||
210 | 'element' => '', |
||
211 | 'property' => '', |
||
212 | 'media_query' => 'global', |
||
213 | 'prefix' => '', |
||
214 | 'units' => '', |
||
215 | 'suffix' => '', |
||
216 | 'value_pattern' => '$', |
||
217 | 'choice' => '', |
||
218 | ); |
||
219 | $output = wp_parse_args( $output, $defaults ); |
||
220 | // If element is an array, convert it to a string |
||
221 | if ( is_array( $output['element'] ) ) { |
||
222 | $output['element'] = array_unique( $output['element'] ); |
||
223 | sort( $output['element'] ); |
||
224 | $output['element'] = implode( ',', $output['element'] ); |
||
225 | } |
||
226 | // Simple fields |
||
227 | if ( ! is_array( $value ) ) { |
||
228 | $value = str_replace( '$', $value, $output['value_pattern'] ); |
||
229 | if ( ! empty( $output['element'] ) && ! empty( $output['property'] ) ) { |
||
230 | $css[ $output['media_query'] ][ $output['element'] ][ $output['property'] ] = $output['prefix'] . $value . $output['units'] . $output['suffix']; |
||
231 | } |
||
232 | } else { |
||
233 | if ( 'typography' == $field['type'] ) { |
||
234 | foreach ( $value as $key => $subvalue ) { |
||
235 | // exclude subsets as a property |
||
236 | if ( 'subsets' == $key ) { |
||
237 | continue; |
||
238 | } |
||
239 | // add double quotes if needed to font-families |
||
240 | if ( 'font-family' == $key && false !== strpos( $subvalue, ' ' ) && false === strpos( $subvalue, '"' ) ) { |
||
241 | $css[ $output['media_query'] ][ $output['element'] ]['font-family'] = '"' . $subvalue . '"'; |
||
242 | } |
||
243 | // variants contain both font-weight & italics |
||
244 | if ( 'variant' == $key ) { |
||
245 | $font_weight = str_replace( 'italic', '', $subvalue ); |
||
246 | $font_weight = ( in_array( $font_weight, array( '', 'regular' ) ) ) ? '400' : $font_weight; |
||
247 | $css[ $output['media_query'] ][ $output['element'] ]['font-weight'] = $font_weight; |
||
248 | // Is this italic? |
||
249 | $is_italic = ( false !== strpos( $subvalue, 'italic' ) ); |
||
250 | if ( $is_italic ) { |
||
251 | $css[ $output['media_query'] ][ $output['element'] ]['font-style'] = 'italic'; |
||
252 | } |
||
253 | } else { |
||
254 | $css[ $output['media_query'] ][ $output['element'] ][ $key ] = $subvalue; |
||
255 | } |
||
256 | } |
||
257 | } elseif ( 'multicolor' == $field['type'] ) { |
||
258 | if ( ! empty( $output['element'] ) && ! empty( $output['property'] ) && ! empty( $output['choice'] ) ) { |
||
259 | $css[ $output['media_query'] ][ $output['element'] ][ $output['property'] ] = $output['prefix'] . $value[ $output['choice'] ] . $output['units'] . $output['suffix']; |
||
260 | } |
||
261 | } else { |
||
262 | foreach ( $value as $key => $subvalue ) { |
||
263 | $property = $key; |
||
264 | if ( false !== strpos( $output['property'], '%%' ) ) { |
||
265 | $property = str_replace( '%%', $key, $output['property'] ); |
||
266 | } elseif ( ! empty( $output['property'] ) ) { |
||
267 | $output['property'] = $output['property'] . '-' . $key; |
||
268 | } |
||
269 | if ( 'background-image' === $output['property'] && false === strpos( $subvalue, 'url(' ) ) { |
||
270 | $subvalue = 'url("' . $subvalue . '")'; |
||
271 | } |
||
272 | if ( $subvalue ) { |
||
273 | $css[ $output['media_query'] ][ $output['element'] ][ $property ] = $subvalue; |
||
274 | } |
||
275 | } |
||
276 | } |
||
277 | } |
||
278 | } |
||
279 | } |
||
280 | // Process the array of CSS properties and produce the final CSS |
||
281 | $final_css = ''; |
||
282 | if ( ! is_array( $css ) || empty( $css ) ) { |
||
283 | return ''; |
||
284 | } |
||
285 | // Parse the generated CSS array and create the CSS string for the output. |
||
286 | foreach ( $css as $media_query => $styles ) { |
||
287 | // Handle the media queries |
||
288 | $final_css .= ( 'global' != $media_query ) ? $media_query . '{' : ''; |
||
289 | foreach ( $styles as $style => $style_array ) { |
||
290 | $final_css .= $style . '{'; |
||
291 | foreach ( $style_array as $property => $value ) { |
||
292 | $value = ( is_string( $value ) ) ? $value : ''; |
||
293 | // Make sure background-images are properly formatted |
||
294 | if ( 'background-image' == $property ) { |
||
295 | if ( false === strrpos( $value, 'url(' ) ) { |
||
296 | $value = 'url("' . esc_url_raw( $value ) . '")'; |
||
297 | } |
||
298 | } else { |
||
299 | $value = esc_textarea( $value ); |
||
300 | } |
||
301 | $final_css .= $property . ':' . $value . ';'; |
||
302 | } |
||
303 | $final_css .= '}'; |
||
304 | } |
||
305 | $final_css .= ( 'global' != $media_query ) ? '}' : ''; |
||
306 | } |
||
307 | return $final_css; |
||
308 | } |
||
309 | |||
371 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.