| Conditions | 34 |
| Paths | 75 |
| Total Lines | 78 |
| Code Lines | 56 |
| 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 |
||
| 145 | public static function sanitize( $value ) { |
||
| 146 | |||
| 147 | if ( ! is_array( $value ) ) { |
||
| 148 | return array(); |
||
| 149 | } |
||
| 150 | |||
| 151 | foreach ( $value as $key => $val ) { |
||
| 152 | switch ( $key ) { |
||
| 153 | case 'font-family': |
||
| 154 | $value['font-family'] = esc_attr( $val ); |
||
| 155 | break; |
||
| 156 | case 'font-weight': |
||
| 157 | if ( isset( $value['variant'] ) ) { |
||
| 158 | break; |
||
| 159 | } |
||
| 160 | $value['variant'] = $val; |
||
| 161 | if ( isset( $value['font-style'] ) && 'italic' === $value['font-style'] ) { |
||
| 162 | $value['variant'] = ( '400' !== $val || 400 !== $val ) ? $value['variant'] . 'italic' : 'italic'; |
||
| 163 | } |
||
| 164 | break; |
||
| 165 | case 'variant': |
||
| 166 | // Use 'regular' instead of 400 for font-variant. |
||
| 167 | $value['variant'] = ( 400 === $val || '400' === $val ) ? 'regular' : $val; |
||
| 168 | // Get font-weight from variant. |
||
| 169 | $value['font-weight'] = filter_var( $value['variant'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
||
| 170 | $value['font-weight'] = ( 'regular' === $value['variant'] || 'italic' === $value['variant'] ) ? 400 : absint( $value['font-weight'] ); |
||
| 171 | // Get font-style from variant. |
||
| 172 | if ( ! isset( $value['font-style'] ) ) { |
||
| 173 | $value['font-style'] = ( false === strpos( $value['variant'], 'italic' ) ) ? 'normal' : 'italic'; |
||
| 174 | } |
||
| 175 | break; |
||
| 176 | case 'subset': |
||
| 177 | // Make sure the saved value is "subsets" (plural) and not "subset". |
||
| 178 | // This is for compatibility with older versions. |
||
| 179 | if ( ! empty( $value['subset'] ) && ! isset( $value['subsets'] ) || empty( $value['subset'] ) ) { |
||
| 180 | $value['subsets'] = $value['subset']; |
||
| 181 | } |
||
| 182 | unset( $value['subset'] ); |
||
| 183 | // Make sure we're using a valid subset. |
||
| 184 | $valid_subsets = Kirki_Fonts::get_google_font_subsets(); |
||
| 185 | $subsets_ok = array(); |
||
| 186 | $value['subsets'] = (array) $value['subsets']; |
||
| 187 | foreach ( $value['subsets'] as $subset ) { |
||
| 188 | if ( array_key_exists( $subset, $valid_subsets ) ) { |
||
| 189 | $subsets_ok[] = $subset; |
||
| 190 | } |
||
| 191 | } |
||
| 192 | $value['subsets'] = $subsets_ok; |
||
| 193 | break; |
||
| 194 | case 'font-size': |
||
| 195 | case 'letter-spacing': |
||
| 196 | case 'word-spacing': |
||
| 197 | case 'line-height': |
||
| 198 | $value[ $key ] = Kirki_Sanitize_Values::css_dimension( $val ); |
||
| 199 | break; |
||
| 200 | case 'text-align': |
||
| 201 | if ( ! in_array( $val, array( 'inherit', 'left', 'center', 'right', 'justify' ), true ) ) { |
||
| 202 | $value['text-align'] = 'inherit'; |
||
| 203 | } |
||
| 204 | break; |
||
| 205 | case 'text-transform': |
||
| 206 | if ( ! in_array( $val, array( 'none', 'capitalize', 'uppercase', 'lowercase', 'initial', 'inherit' ), true ) ) { |
||
| 207 | $value['text-transform'] = 'none'; |
||
| 208 | } |
||
| 209 | break; |
||
| 210 | case 'text-decoration': |
||
| 211 | if ( ! in_array( $val, array( 'none', 'underline', 'overline', 'line-through', 'initial', 'inherit' ), true ) ) { |
||
| 212 | $value['text-transform'] = 'none'; |
||
| 213 | } |
||
| 214 | break; |
||
| 215 | case 'color': |
||
| 216 | $value['color'] = ariColor::newColor( $val )->toCSS( 'hex' ); |
||
| 217 | break; |
||
| 218 | } // End switch(). |
||
| 219 | } // End foreach(). |
||
| 220 | |||
| 221 | return $value; |
||
| 222 | } |
||
| 223 | |||
| 246 |