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 | */ |
||
57 | public function __construct( $config_id, $output, $value ) { |
||
65 | |||
66 | /** |
||
67 | * If we have a sanitize_callback defined, apply it to the value. |
||
68 | * |
||
69 | * @param array $output The output args. |
||
70 | * @param string|array $value The value. |
||
71 | * |
||
72 | * @return string|array |
||
73 | */ |
||
74 | protected function apply_sanitize_callback( $output, $value ) { |
||
88 | |||
89 | /** |
||
90 | * If we have a value_pattern defined, apply it to the value. |
||
91 | * |
||
92 | * @param array $output The output args. |
||
93 | * @param string|array $value The value. |
||
94 | * |
||
95 | * @return string|array |
||
96 | */ |
||
97 | protected function apply_value_pattern( $output, $value ) { |
||
98 | |||
99 | if ( isset( $output['value_pattern'] ) && ! empty( $output['value_pattern'] ) && is_string( $output['value_pattern'] ) ) { |
||
100 | if ( ! is_array( $value ) ) { |
||
101 | $value = str_replace( '$', $value, $output['value_pattern'] ); |
||
102 | } |
||
103 | if ( is_array( $value ) ) { |
||
104 | foreach ( array_keys( $value ) as $value_k ) { |
||
105 | if ( ! is_string( $value[ $value_k ] ) ) { |
||
106 | continue; |
||
107 | } |
||
108 | if ( isset( $output['choice'] ) ) { |
||
109 | if ( $output['choice'] === $value_k ) { |
||
110 | $value[ $output['choice'] ] = str_replace( '$', $value[ $output['choice'] ], $output['value_pattern'] ); |
||
111 | } |
||
112 | continue; |
||
113 | } |
||
114 | $value[ $value_k ] = str_replace( '$', $value[ $value_k ], $output['value_pattern'] ); |
||
115 | } |
||
116 | } |
||
117 | if ( isset( $output['pattern_replace'] ) && is_array( $output['pattern_replace'] ) ) { |
||
118 | $option_type = ( '' !== Kirki::get_config_param( $this->config_id, 'option_type' ) ) ? Kirki::get_config_param( $this->config_id, 'option_type' ) : 'theme_mod'; |
||
119 | $option_name = Kirki::get_config_param( $this->config_id, 'option_name' ); |
||
120 | $options = array(); |
||
121 | if ( $option_name ) { |
||
122 | $options = ( 'site_option' === $option_type ) ? get_site_option( $option_name ) : get_option( $option_name ); |
||
123 | } |
||
124 | foreach ( $output['pattern_replace'] as $search => $replace ) { |
||
125 | $replacement = ''; |
||
126 | switch ( $option_type ) { |
||
127 | case 'option': |
||
128 | if ( is_array( $options ) ) { |
||
129 | if ( $option_name ) { |
||
130 | $subkey = str_replace( array( $option_name, '[', ']' ), '', $replace ); |
||
131 | $replacement = ( isset( $options[ $subkey ] ) ) ? $options[ $subkey ] : ''; |
||
132 | break; |
||
133 | } |
||
134 | $replacement = ( isset( $options[ $replace ] ) ) ? $options[ $replace ] : ''; |
||
135 | break; |
||
136 | } |
||
137 | $replacement = get_option( $replace ); |
||
138 | break; |
||
139 | case 'site_option': |
||
140 | $replacement = ( is_array( $options ) && isset( $options[ $replace ] ) ) ? $options[ $replace ] : get_site_option( $replace ); |
||
141 | break; |
||
142 | case 'user_meta': |
||
143 | $user_id = get_current_user_id(); |
||
144 | if ( $user_id ) { |
||
145 | // @codingStandardsIgnoreLine |
||
146 | $replacement = get_user_meta( $user_id, $replace, true ); |
||
147 | } |
||
148 | break; |
||
149 | default: |
||
150 | $replacement = get_theme_mod( $replace ); |
||
151 | } |
||
152 | $replacement = ( false === $replacement ) ? '' : $replacement; |
||
153 | if ( is_array( $value ) ) { |
||
154 | foreach ( $value as $k => $v ) { |
||
155 | $value[ $k ] = str_replace( $search, $replacement, $value[ $v ] ); |
||
156 | } |
||
157 | return $value; |
||
158 | } |
||
159 | $value = str_replace( $search, $replacement, $value ); |
||
160 | } // End foreach(). |
||
161 | } // End if(). |
||
162 | } // End if(). |
||
163 | |||
164 | return $value; |
||
165 | |||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Parses the output arguments. |
||
170 | * Calls the process_output method for each of them. |
||
171 | * |
||
172 | * @access protected |
||
173 | */ |
||
174 | protected function parse_output() { |
||
175 | foreach ( $this->output as $output ) { |
||
176 | $skip = false; |
||
177 | |||
178 | // Apply any sanitization callbacks defined. |
||
179 | $value = $this->apply_sanitize_callback( $output, $this->value ); |
||
180 | |||
181 | // Skip if value is empty. |
||
182 | if ( '' === $this->value ) { |
||
183 | $skip = true; |
||
184 | } |
||
185 | |||
186 | // No need to proceed this if the current value is the same as in the "exclude" value. |
||
187 | if ( isset( $output['exclude'] ) && is_array( $output['exclude'] ) ) { |
||
188 | foreach ( $output['exclude'] as $exclude ) { |
||
189 | if ( is_array( $value ) ) { |
||
190 | if ( is_array( $exclude ) ) { |
||
191 | $diff1 = array_diff( $value, $exclude ); |
||
192 | $diff2 = array_diff( $exclude, $value ); |
||
193 | |||
194 | if ( empty( $diff1 ) && empty( $diff2 ) ) { |
||
195 | $skip = true; |
||
196 | } |
||
197 | } |
||
198 | // If 'choice' is defined check for sub-values too. |
||
199 | // Fixes https://github.com/aristath/kirki/issues/1416. |
||
200 | if ( isset( $output['choice'] ) && isset( $value[ $output['choice'] ] ) && $exclude == $value[ $output['choice'] ] ) { |
||
201 | $skip = true; |
||
202 | } |
||
203 | } |
||
204 | if ( $skip ) { |
||
205 | continue; |
||
206 | } |
||
207 | |||
208 | // Skip if value is defined as excluded. |
||
209 | if ( $exclude === $value ) { |
||
210 | $skip = true; |
||
211 | } |
||
212 | } |
||
213 | } |
||
214 | if ( $skip ) { |
||
215 | continue; |
||
216 | } |
||
217 | |||
218 | // Apply any value patterns defined. |
||
219 | $value = $this->apply_value_pattern( $output, $value ); |
||
220 | |||
221 | if ( isset( $output['element'] ) && is_array( $output['element'] ) ) { |
||
222 | $output['element'] = array_unique( $output['element'] ); |
||
223 | sort( $output['element'] ); |
||
224 | $output['element'] = implode( ',', $output['element'] ); |
||
225 | } |
||
226 | |||
227 | $value = $this->process_value( $value, $output ); |
||
228 | $this->process_output( $output, $value ); |
||
|
|||
229 | } // End foreach(). |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Parses an output and creates the styles array for it. |
||
234 | * |
||
235 | * @access protected |
||
236 | * @param array $output The field output. |
||
237 | * @param string $value The value. |
||
238 | * |
||
239 | * @return void |
||
240 | */ |
||
241 | protected function process_output( $output, $value ) { |
||
242 | if ( ! isset( $output['element'] ) || ! isset( $output['property'] ) ) { |
||
243 | return; |
||
244 | } |
||
245 | $output['media_query'] = ( isset( $output['media_query'] ) ) ? $output['media_query'] : 'global'; |
||
246 | $output['prefix'] = ( isset( $output['prefix'] ) ) ? $output['prefix'] : ''; |
||
247 | $output['units'] = ( isset( $output['units'] ) ) ? $output['units'] : ''; |
||
248 | $output['suffix'] = ( isset( $output['suffix'] ) ) ? $output['suffix'] : ''; |
||
249 | |||
250 | // Properties that can accept multiple values. |
||
251 | // Useful for example for gradients where all browsers use the "background-image" property |
||
252 | // and the browser prefixes go in the value_pattern arg. |
||
253 | $accepts_multiple = array( |
||
254 | 'background-image', |
||
255 | 'background', |
||
256 | ); |
||
257 | if ( in_array( $output['property'], $accepts_multiple, true ) ) { |
||
258 | if ( isset( $this->styles[ $output['media_query'] ][ $output['element'] ][ $output['property'] ] ) && ! is_array( $this->styles[ $output['media_query'] ][ $output['element'] ][ $output['property'] ] ) ) { |
||
259 | $this->styles[ $output['media_query'] ][ $output['element'] ][ $output['property'] ] = (array) $this->styles[ $output['media_query'] ][ $output['element'] ][ $output['property'] ]; |
||
260 | } |
||
261 | $this->styles[ $output['media_query'] ][ $output['element'] ][ $output['property'] ][] = $output['prefix'] . $value . $output['units'] . $output['suffix']; |
||
262 | return; |
||
263 | } |
||
264 | $this->styles[ $output['media_query'] ][ $output['element'] ][ $output['property'] ] = $output['prefix'] . $value . $output['units'] . $output['suffix']; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Some CSS properties are unique. |
||
269 | * We need to tweak the value to make everything works as expected. |
||
270 | * |
||
271 | * @access protected |
||
272 | * @param string $property The CSS property. |
||
273 | * @param string $value The value. |
||
274 | * |
||
275 | * @return array |
||
276 | */ |
||
277 | protected function process_property_value( $property, $value ) { |
||
290 | |||
291 | /** |
||
292 | * Returns the value. |
||
293 | * |
||
294 | * @access protected |
||
295 | * @param string|array $value The value. |
||
296 | * @param array $output The field "output". |
||
297 | * @return string|array |
||
298 | */ |
||
299 | protected function process_value( $value, $output ) { |
||
305 | |||
306 | /** |
||
307 | * Exploses the private $styles property to the world |
||
308 | * |
||
309 | * @access protected |
||
310 | * @return array |
||
311 | */ |
||
312 | public function get_styles() { |
||
315 | } |
||
316 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.