Complex classes like Kirki_Output 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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_Output, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Kirki_Output { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The Kirki configuration used in the field. |
||
| 19 | * |
||
| 20 | * @access protected |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $config_id = 'global'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The field's `output` argument. |
||
| 27 | * |
||
| 28 | * @access protected |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $output = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * An array of the generated styles. |
||
| 35 | * |
||
| 36 | * @access protected |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $styles = array(); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The value. |
||
| 43 | * |
||
| 44 | * @access protected |
||
| 45 | * @var string|array |
||
| 46 | */ |
||
| 47 | protected $value; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The class constructor. |
||
| 51 | * |
||
| 52 | * @access public |
||
| 53 | * @param string $config_id The config ID. |
||
| 54 | * @param array $output The output argument. |
||
| 55 | * @param string|array $value The value. |
||
| 56 | * @param array $field The field. |
||
| 57 | */ |
||
| 58 | public function __construct( $config_id, $output, $value, $field ) { |
||
| 59 | |||
| 60 | $this->config_id = $config_id; |
||
| 61 | $this->value = $value; |
||
| 62 | $this->output = $output; |
||
| 63 | $this->field = $field; |
||
|
|
|||
| 64 | |||
| 65 | $this->parse_output(); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * If we have a sanitize_callback defined, apply it to the value. |
||
| 70 | * |
||
| 71 | * @param array $output The output args. |
||
| 72 | * @param string|array $value The value. |
||
| 73 | * |
||
| 74 | * @return string|array |
||
| 75 | */ |
||
| 76 | protected function apply_sanitize_callback( $output, $value ) { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * If we have a value_pattern defined, apply it to the value. |
||
| 93 | * |
||
| 94 | * @param array $output The output args. |
||
| 95 | * @param string|array $value The value. |
||
| 96 | * @return string|array |
||
| 97 | */ |
||
| 98 | protected function apply_value_pattern( $output, $value ) { |
||
| 99 | |||
| 100 | if ( isset( $output['value_pattern'] ) && ! empty( $output['value_pattern'] ) && is_string( $output['value_pattern'] ) ) { |
||
| 101 | if ( ! is_array( $value ) ) { |
||
| 102 | $value = str_replace( '$', $value, $output['value_pattern'] ); |
||
| 103 | } |
||
| 104 | if ( is_array( $value ) ) { |
||
| 105 | foreach ( array_keys( $value ) as $value_k ) { |
||
| 106 | if ( ! is_string( $value[ $value_k ] ) ) { |
||
| 107 | continue; |
||
| 108 | } |
||
| 109 | if ( isset( $output['choice'] ) ) { |
||
| 110 | if ( $output['choice'] === $value_k ) { |
||
| 111 | $value[ $output['choice'] ] = str_replace( '$', $value[ $output['choice'] ], $output['value_pattern'] ); |
||
| 112 | } |
||
| 113 | continue; |
||
| 114 | } |
||
| 115 | $value[ $value_k ] = str_replace( '$', $value[ $value_k ], $output['value_pattern'] ); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | $value = $this->apply_pattern_replace( $output, $value ); |
||
| 119 | } // End if(). |
||
| 120 | return $value; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * If we have a value_pattern defined, apply it to the value. |
||
| 125 | * |
||
| 126 | * @param array $output The output args. |
||
| 127 | * @param string|array $value The value. |
||
| 128 | * @return string|array |
||
| 129 | */ |
||
| 130 | protected function apply_pattern_replace( $output, $value ) { |
||
| 131 | if ( isset( $output['pattern_replace'] ) && is_array( $output['pattern_replace'] ) ) { |
||
| 132 | $option_type = ( '' !== Kirki::get_config_param( $this->config_id, 'option_type' ) ) ? Kirki::get_config_param( $this->config_id, 'option_type' ) : 'theme_mod'; |
||
| 133 | $option_name = Kirki::get_config_param( $this->config_id, 'option_name' ); |
||
| 134 | $options = array(); |
||
| 135 | if ( $option_name ) { |
||
| 136 | $options = ( 'site_option' === $option_type ) ? get_site_option( $option_name ) : get_option( $option_name ); |
||
| 137 | } |
||
| 138 | foreach ( $output['pattern_replace'] as $search => $replace ) { |
||
| 139 | $replacement = ''; |
||
| 140 | switch ( $option_type ) { |
||
| 141 | case 'option': |
||
| 142 | if ( is_array( $options ) ) { |
||
| 143 | if ( $option_name ) { |
||
| 144 | $subkey = str_replace( array( $option_name, '[', ']' ), '', $replace ); |
||
| 145 | $replacement = ( isset( $options[ $subkey ] ) ) ? $options[ $subkey ] : ''; |
||
| 146 | break; |
||
| 147 | } |
||
| 148 | $replacement = ( isset( $options[ $replace ] ) ) ? $options[ $replace ] : ''; |
||
| 149 | break; |
||
| 150 | } |
||
| 151 | $replacement = get_option( $replace ); |
||
| 152 | break; |
||
| 153 | case 'site_option': |
||
| 154 | $replacement = ( is_array( $options ) && isset( $options[ $replace ] ) ) ? $options[ $replace ] : get_site_option( $replace ); |
||
| 155 | break; |
||
| 156 | case 'user_meta': |
||
| 157 | $user_id = get_current_user_id(); |
||
| 158 | if ( $user_id ) { |
||
| 159 | // @codingStandardsIgnoreLine |
||
| 160 | $replacement = get_user_meta( $user_id, $replace, true ); |
||
| 161 | } |
||
| 162 | break; |
||
| 163 | default: |
||
| 164 | $replacement = get_theme_mod( $replace ); |
||
| 165 | } |
||
| 166 | $replacement = ( false === $replacement ) ? '' : $replacement; |
||
| 167 | if ( is_array( $value ) ) { |
||
| 168 | foreach ( $value as $k => $v ) { |
||
| 169 | if ( isset( $value[ $v ] ) ) { |
||
| 170 | $value[ $k ] = str_replace( $search, $replacement, $value[ $v ] ); |
||
| 171 | } else { |
||
| 172 | $value[ $k ] = str_replace( $search, $replacement, $v ); |
||
| 173 | } |
||
| 174 | } |
||
| 175 | return $value; |
||
| 176 | } |
||
| 177 | $value = str_replace( $search, $replacement, $value ); |
||
| 178 | } // End foreach(). |
||
| 179 | } // End if(). |
||
| 180 | return $value; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Parses the output arguments. |
||
| 185 | * Calls the process_output method for each of them. |
||
| 186 | * |
||
| 187 | * @access protected |
||
| 188 | */ |
||
| 189 | protected function parse_output() { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Parses an output and creates the styles array for it. |
||
| 249 | * |
||
| 250 | * @access protected |
||
| 251 | * @param array $output The field output. |
||
| 252 | * @param string|array $value The value. |
||
| 253 | * |
||
| 254 | * @return null |
||
| 255 | */ |
||
| 256 | protected function process_output( $output, $value ) { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Some CSS properties are unique. |
||
| 284 | * We need to tweak the value to make everything works as expected. |
||
| 285 | * |
||
| 286 | * @access protected |
||
| 287 | * @param string $property The CSS property. |
||
| 288 | * @param string|array $value The value. |
||
| 289 | * |
||
| 290 | * @return array |
||
| 291 | */ |
||
| 292 | protected function process_property_value( $property, $value ) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Returns the value. |
||
| 310 | * |
||
| 311 | * @access protected |
||
| 312 | * @param string|array $value The value. |
||
| 313 | * @param array $output The field "output". |
||
| 314 | * @return string|array |
||
| 315 | */ |
||
| 316 | protected function process_value( $value, $output ) { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Exploses the private $styles property to the world |
||
| 325 | * |
||
| 326 | * @access protected |
||
| 327 | * @return array |
||
| 328 | */ |
||
| 329 | public function get_styles() { |
||
| 332 | } |
||
| 333 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: