| Total Complexity | 48 |
| Total Lines | 203 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Kirki_Field_Typography often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Kirki_Field_Typography, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Kirki_Field_Typography extends Kirki_Field { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Sets the control type. |
||
| 19 | * |
||
| 20 | * @access protected |
||
| 21 | */ |
||
| 22 | protected function set_type() { |
||
| 23 | |||
| 24 | $this->type = 'kirki-typography'; |
||
| 25 | |||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The class constructor. |
||
| 30 | * Parses and sanitizes all field arguments. |
||
| 31 | * Then it adds the field to Kirki::$fields. |
||
| 32 | * |
||
| 33 | * @access public |
||
| 34 | * @param string $config_id The ID of the config we want to use. |
||
| 35 | * Defaults to "global". |
||
| 36 | * Configs are handled by the Kirki_Config class. |
||
| 37 | * @param array $args The arguments of the field. |
||
| 38 | */ |
||
| 39 | public function __construct( $config_id = 'global', $args = array() ) { |
||
| 40 | parent::__construct( $config_id, $args ); |
||
| 41 | $this->set_default(); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Sets the default value. |
||
| 46 | * |
||
| 47 | * @access protected |
||
| 48 | */ |
||
| 49 | protected function set_default() { |
||
| 50 | |||
| 51 | // Accomodate the use of font-weight and convert to variant. |
||
| 52 | if ( isset( $this->default['font-weight'] ) ) { |
||
| 53 | $this->default['variant'] = ( 'regular' === $this->default['font-weight'] ) ? 400 : (string) intval( $this->default['font-weight'] ); |
||
| 54 | } |
||
| 55 | |||
| 56 | // Make sure letter-spacing has units. |
||
| 57 | if ( isset( $this->default['letter-spacing'] ) && is_numeric( $this->default['letter-spacing'] ) && $this->default['letter-spacing'] ) { |
||
| 58 | $this->default['letter-spacing'] .= 'px'; |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Sets the $sanitize_callback |
||
| 64 | * |
||
| 65 | * @access protected |
||
| 66 | */ |
||
| 67 | protected function set_sanitize_callback() { |
||
| 68 | |||
| 69 | // If a custom sanitize_callback has been defined, |
||
| 70 | // then we don't need to proceed any further. |
||
| 71 | if ( ! empty( $this->sanitize_callback ) ) { |
||
| 72 | return; |
||
| 73 | } |
||
| 74 | $this->sanitize_callback = array( __CLASS__, 'sanitize' ); |
||
| 75 | |||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Sets the $js_vars |
||
| 80 | * |
||
| 81 | * @access protected |
||
| 82 | */ |
||
| 83 | protected function set_js_vars() { |
||
| 127 | |||
| 128 | } |
||
| 129 | |||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Sanitizes typography controls |
||
| 134 | * |
||
| 135 | * @static |
||
| 136 | * @since 2.2.0 |
||
| 137 | * @param array $value The value. |
||
| 138 | * @return array |
||
| 139 | */ |
||
| 140 | public static function sanitize( $value ) { |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Sets the $choices |
||
| 203 | * |
||
| 204 | * @access protected |
||
| 205 | * @since 3.0.0 |
||
| 206 | */ |
||
| 207 | protected function set_choices() { |
||
| 223 |