| Conditions | 10 |
| Paths | 64 |
| Total Lines | 53 |
| Code Lines | 31 |
| 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 |
||
| 67 | protected function _script( $args ) { |
||
| 68 | $script = ''; |
||
| 69 | $property_script = ''; |
||
| 70 | |||
| 71 | $value_key = 'newval' . $args['index_key']; |
||
| 72 | $property_script .= $value_key . '=newval;'; |
||
| 73 | |||
| 74 | // Make sure everything is defined to avoid "undefined index" errors. |
||
| 75 | $args = wp_parse_args( $args, array( |
||
| 76 | 'element' => '', |
||
| 77 | 'property' => '', |
||
| 78 | 'prefix' => '', |
||
| 79 | 'suffix' => '', |
||
| 80 | 'units' => '', |
||
| 81 | 'js_callback' => array( '', '' ), |
||
| 82 | 'value_pattern' => '', |
||
| 83 | )); |
||
| 84 | |||
| 85 | // Element should be a string. |
||
| 86 | if ( is_array( $args['element'] ) ) { |
||
| 87 | $args['element'] = implode( ',', $args['element'] ); |
||
| 88 | } |
||
| 89 | |||
| 90 | // Make sure arguments that are passed-on to callbacks are strings. |
||
| 91 | if ( is_array( $args['js_callback'] ) && isset( $args['js_callback'][1] ) && is_array( $args['js_callback'][1] ) ) { |
||
| 92 | $args['js_callback'][1] = wp_json_encode( $args['js_callback'][1] ); |
||
| 93 | } |
||
| 94 | |||
| 95 | // Apply callback to the value if a callback is defined. |
||
| 96 | if ( ! empty( $args['js_callback'][0] ) ) { |
||
| 97 | $script .= $value_key . '=' . $args['js_callback'][0] . '(' . $value_key . ',' . $args['js_callback'][1] . ');'; |
||
| 98 | } |
||
| 99 | |||
| 100 | // Apply the value_pattern. |
||
| 101 | if ( '' !== $args['value_pattern'] ) { |
||
| 102 | $value_pattern = str_replace( '$', '\'+' . $value_key . '+\'', $value_key ); |
||
| 103 | $script .= $value_key . '=' . trim( $value_pattern, '\'+' ) . ';'; |
||
| 104 | } |
||
| 105 | |||
| 106 | // Apply prefix, units, suffix. |
||
| 107 | $value = $value_key; |
||
| 108 | if ( '' !== $args['prefix'] ) { |
||
| 109 | $value = $args['prefix'] . '+' . $value_key; |
||
| 110 | } |
||
| 111 | if ( '' !== $args['units'] || '' !== $args['suffix'] ) { |
||
| 112 | $value .= '+' . $args['units'] . $args['suffix']; |
||
| 113 | } |
||
| 114 | $scripts_array = array(); |
||
| 115 | $scripts_array[ sanitize_key( $args['element'] ) ][ sanitize_key( $args['property'] ) ]['script'] = $property_script . $script; |
||
| 116 | $scripts_array[ sanitize_key( $args['element'] ) ][ sanitize_key( $args['property'] ) ]['css'] = $args['element'] . '{' . $args['property'] . ':\'+' . $value_key . '+\';}'; |
||
| 117 | |||
| 118 | return $scripts_array; |
||
| 119 | } |
||
| 120 | |||
| 156 |
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.