Complex classes like Kirki_Active_Callback 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_Active_Callback, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Kirki_Active_Callback { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Figure out whether the current object should be displayed or not. |
||
| 19 | * |
||
| 20 | * @param WP_Customize_Setting $object The current field. |
||
| 21 | * @return boolean |
||
| 22 | */ |
||
| 23 | public static function evaluate( $object ) { |
||
| 24 | |||
| 25 | $show = true; |
||
| 26 | |||
| 27 | // Get all fields. |
||
| 28 | $fields = Kirki::$fields; |
||
| 29 | |||
| 30 | // Make sure the current object matches a registered field. |
||
| 31 | if ( ! isset( $object->setting->id ) || ! isset( $fields[ $object->setting->id ] ) ) { |
||
| 32 | return true; |
||
| 33 | } |
||
| 34 | |||
| 35 | $field = $fields[ $object->setting->id ]; |
||
| 36 | |||
| 37 | if ( isset( $field['required'] ) ) { |
||
| 38 | |||
| 39 | foreach ( $field['required'] as $requirement ) { |
||
| 40 | // Handles "AND" functionality. |
||
| 41 | $show = self::evaluate_requirement( $object, $field, $requirement ); |
||
| 42 | // No need to process further if one requirement returns false. |
||
| 43 | if ( ! $show ) { |
||
| 44 | return false; |
||
| 45 | } |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | return true; |
||
| 50 | |||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Figure out whether the current object should be displayed or not. |
||
| 55 | * We're only parsing a single requirement here from the array of requirements. |
||
| 56 | * This is a proxy function that facilitates evaluating and/or conditions. |
||
| 57 | * |
||
| 58 | * @param WP_Customize_Setting $object The current field. |
||
| 59 | * @param object $field The current object. |
||
| 60 | * @param array $requirement A single requirement. |
||
| 61 | * @return boolean |
||
| 62 | */ |
||
| 63 | private static function evaluate_requirement( $object, $field, $requirement ) { |
||
| 64 | |||
| 65 | // Test for callables first. |
||
| 66 | if ( is_callable( $requirement ) ) { |
||
| 67 | return call_user_func_array( $requirement, array( $field, $object ) ); |
||
| 68 | } |
||
| 69 | |||
| 70 | // Look for comparison array. |
||
| 71 | if ( is_array( $requirement ) && isset( $requirement['operator'], $requirement['value'], $requirement['setting'] ) ) { |
||
| 72 | |||
| 73 | if ( isset( $field['option_name'] ) && '' !== $field['option_name'] ) { |
||
| 74 | if ( false === strpos( $requirement['setting'], '[' ) ) { |
||
| 75 | $requirement['setting'] = $field['option_name'] . '[' . $requirement['setting'] . ']'; |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | $current_setting = $object->manager->get_setting( $requirement['setting'] ); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Depending on the 'operator' argument we use, |
||
| 83 | * we'll need to perform the appropriate comparison |
||
| 84 | * and figure out if the control will be shown or not. |
||
| 85 | */ |
||
| 86 | $show = self::compare( $requirement['value'], $current_setting->value(), $requirement['operator'] ); |
||
| 87 | |||
| 88 | } else { |
||
| 89 | if ( ! is_array( $requirement ) ) { |
||
| 90 | return true; |
||
| 91 | } |
||
| 92 | |||
| 93 | // Handles "OR" functionality. |
||
| 94 | $show = false; |
||
| 95 | foreach ( $requirement as $sub_requirement ) { |
||
| 96 | $show = self::evaluate_requirement( $object, $field, $sub_requirement ); |
||
| 97 | // No need to go on if one sub_requirement returns true. |
||
| 98 | if ( $show ) { |
||
| 99 | return true; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | return $show; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Compares the 2 values given the condition |
||
| 109 | * |
||
| 110 | * @param mixed $value1 The 1st value in the comparison. |
||
| 111 | * @param mixed $value2 The 2nd value in the comparison. |
||
| 112 | * @param string $operator The operator we'll use for the comparison. |
||
| 113 | * @return boolean whether The comparison has succeded (true) or failed (false). |
||
| 114 | */ |
||
| 115 | public static function compare( $value1, $value2, $operator ) { |
||
| 181 | } |
||
| 182 |