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 | * @return string|array |
||
95 | */ |
||
96 | protected function apply_value_pattern( $output, $value ) { |
||
120 | |||
121 | /** |
||
122 | * If we have a value_pattern defined, apply it to the value. |
||
123 | * |
||
124 | * @param array $output The output args. |
||
125 | * @param string|array $value The value. |
||
126 | * @return string|array |
||
127 | */ |
||
128 | protected function apply_pattern_replace( $output, $value ) { |
||
176 | |||
177 | /** |
||
178 | * Parses the output arguments. |
||
179 | * Calls the process_output method for each of them. |
||
180 | * |
||
181 | * @access protected |
||
182 | */ |
||
183 | protected function parse_output() { |
||
184 | foreach ( $this->output as $output ) { |
||
185 | $skip = false; |
||
186 | |||
187 | // Apply any sanitization callbacks defined. |
||
188 | $value = $this->apply_sanitize_callback( $output, $this->value ); |
||
189 | |||
190 | // Skip if value is empty. |
||
191 | if ( '' === $this->value ) { |
||
192 | $skip = true; |
||
193 | } |
||
194 | |||
195 | // No need to proceed this if the current value is the same as in the "exclude" value. |
||
196 | if ( isset( $output['exclude'] ) && is_array( $output['exclude'] ) ) { |
||
197 | foreach ( $output['exclude'] as $exclude ) { |
||
198 | if ( is_array( $value ) ) { |
||
199 | if ( is_array( $exclude ) ) { |
||
200 | $diff1 = array_diff( $value, $exclude ); |
||
201 | $diff2 = array_diff( $exclude, $value ); |
||
202 | |||
203 | if ( empty( $diff1 ) && empty( $diff2 ) ) { |
||
204 | $skip = true; |
||
205 | } |
||
206 | } |
||
207 | // If 'choice' is defined check for sub-values too. |
||
208 | // Fixes https://github.com/aristath/kirki/issues/1416. |
||
209 | if ( isset( $output['choice'] ) && isset( $value[ $output['choice'] ] ) && $exclude == $value[ $output['choice'] ] ) { |
||
210 | $skip = true; |
||
211 | } |
||
212 | } |
||
213 | if ( $skip ) { |
||
214 | continue; |
||
215 | } |
||
216 | |||
217 | // Skip if value is defined as excluded. |
||
218 | if ( $exclude === $value || ( '' === $exclude && empty( $value ) ) ) { |
||
219 | $skip = true; |
||
220 | } |
||
221 | } |
||
222 | } |
||
223 | if ( $skip ) { |
||
224 | continue; |
||
225 | } |
||
226 | |||
227 | // Apply any value patterns defined. |
||
228 | $value = $this->apply_value_pattern( $output, $value ); |
||
229 | |||
230 | if ( isset( $output['element'] ) && is_array( $output['element'] ) ) { |
||
231 | $output['element'] = array_unique( $output['element'] ); |
||
232 | sort( $output['element'] ); |
||
233 | $output['element'] = implode( ',', $output['element'] ); |
||
234 | } |
||
235 | |||
236 | $value = $this->process_value( $value, $output ); |
||
237 | $this->process_output( $output, $value ); |
||
238 | } // End foreach(). |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Parses an output and creates the styles array for it. |
||
243 | * |
||
244 | * @access protected |
||
245 | * @param array $output The field output. |
||
246 | * @param string|array $value The value. |
||
247 | * |
||
248 | * @return void |
||
249 | */ |
||
250 | protected function process_output( $output, $value ) { |
||
275 | |||
276 | /** |
||
277 | * Some CSS properties are unique. |
||
278 | * We need to tweak the value to make everything works as expected. |
||
279 | * |
||
280 | * @access protected |
||
281 | * @param string $property The CSS property. |
||
282 | * @param string|array $value The value. |
||
283 | * |
||
284 | * @return array |
||
285 | */ |
||
286 | protected function process_property_value( $property, $value ) { |
||
299 | |||
300 | /** |
||
301 | * Returns the value. |
||
302 | * |
||
303 | * @access protected |
||
304 | * @param string|array $value The value. |
||
305 | * @param array $output The field "output". |
||
306 | * @return string|array |
||
307 | */ |
||
308 | protected function process_value( $value, $output ) { |
||
314 | |||
315 | /** |
||
316 | * Exploses the private $styles property to the world |
||
317 | * |
||
318 | * @access protected |
||
319 | * @return array |
||
320 | */ |
||
321 | public function get_styles() { |
||
324 | } |
||
325 |