| Conditions | 10 |
| Paths | 131 |
| Total Lines | 64 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 2 | Features | 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 |
||
| 43 | public static function add_font( $family = '', $weight = 400, $style = '', $subsets = array( 'latin' ) ) { |
||
| 44 | // Early exit if font-family is empty |
||
| 45 | if ( '' == $family ) { |
||
| 46 | return; |
||
| 47 | } |
||
| 48 | |||
| 49 | $google_fonts = Kirki_Fonts::get_google_fonts(); |
||
| 50 | |||
| 51 | // Make sure the class is instantiated |
||
| 52 | self::get_instance(); |
||
| 53 | |||
| 54 | $family = esc_attr( $family ); |
||
| 55 | |||
| 56 | // Determine if this is indeed a google font or not. |
||
| 57 | $is_google_font = false; |
||
| 58 | if ( array_key_exists( $family, $google_fonts ) ) { |
||
| 59 | $is_google_font = true; |
||
| 60 | } |
||
| 61 | |||
| 62 | // If we're not dealing with a valid google font then we can exit. |
||
| 63 | if ( ! $is_google_font ) { |
||
| 64 | return; |
||
| 65 | } |
||
| 66 | |||
| 67 | // Get all valid font variants for this font |
||
| 68 | $font_variants = array(); |
||
| 69 | if ( isset( $google_fonts[ $family ]['variants'] ) ) { |
||
| 70 | $font_variants = $google_fonts[ $family ]['variants']; |
||
| 71 | } |
||
| 72 | |||
| 73 | // format the requested variant |
||
| 74 | $requested_variant = $weight . $style; |
||
| 75 | |||
| 76 | // Is this a valid variant for this font? |
||
| 77 | $variant_is_valid = false; |
||
| 78 | if ( in_array( $requested_variant, $font_variants ) ) { |
||
| 79 | $variant_is_valid = true; |
||
| 80 | } |
||
| 81 | |||
| 82 | // Get all available subsets for this font |
||
| 83 | $font_subsets = array(); |
||
| 84 | if ( isset( $google_fonts[ $family ]['subsets'] ) ) { |
||
| 85 | $font_subsets = $google_fonts[ $family ]['subsets']; |
||
| 86 | } |
||
| 87 | |||
| 88 | // Get the valid subsets for this font |
||
| 89 | $requested_subsets = array_intersect( $subsets, $font_subsets ); |
||
| 90 | |||
| 91 | // If this font only has 1 subset, then use it. |
||
| 92 | if ( 1 == count( $font_subsets ) ) { |
||
| 93 | $requested_subsets = $font_subsets; |
||
| 94 | } |
||
| 95 | |||
| 96 | self::$fonts[ $family ] = array( |
||
| 97 | 'subsets' => $requested_subsets, |
||
| 98 | ); |
||
| 99 | if ( ! isset( self::$fonts[ $family ]['variants'] ) ) { |
||
| 100 | self::$fonts[ $family ]['variants'] = array(); |
||
| 101 | } |
||
| 102 | if ( $variant_is_valid ) { |
||
| 103 | self::$fonts[ $family ]['variants'][] =$requested_variant; |
||
|
|
|||
| 104 | } |
||
| 105 | |||
| 106 | } |
||
| 107 | |||
| 109 |