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