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 ) { |
||
50 | |||
51 | /** |
||
52 | * Figure out whether the current object should be displayed or not. |
||
53 | * We're only parsing a single requirement here from the array of requirements. |
||
54 | * This is a proxy function that facilitates evaluating and/or conditions. |
||
55 | * |
||
56 | * @param WP_Customize_Setting $object The current field. |
||
57 | * @param object $field The current object. |
||
58 | * @param array $requirement A single requirement. |
||
59 | * @return boolean |
||
60 | */ |
||
61 | private static function evaluate_requirement( $object, $field, $requirement ) { |
||
62 | |||
63 | // Test for callables first. |
||
64 | if ( is_callable( $requirement ) ) { |
||
65 | return call_user_func_array( $requirement, array( $field, $object ) ); |
||
66 | } |
||
67 | |||
68 | // Look for comparison array. |
||
69 | if ( is_array( $requirement ) && isset( $requirement['operator'], $requirement['value'], $requirement['setting'] ) ) { |
||
70 | |||
71 | if ( isset( $field['option_name'] ) && '' !== $field['option_name'] ) { |
||
72 | if ( false === strpos( $requirement['setting'], '[' ) ) { |
||
73 | $requirement['setting'] = $field['option_name'] . '[' . $requirement['setting'] . ']'; |
||
74 | } |
||
75 | } |
||
76 | |||
77 | $current_setting = $object->manager->get_setting( $requirement['setting'] ); |
||
78 | |||
79 | /** |
||
80 | * Depending on the 'operator' argument we use, |
||
81 | * we'll need to perform the appropriate comparison |
||
82 | * and figure out if the control will be shown or not. |
||
83 | */ |
||
84 | $show = self::compare( $requirement['value'], $current_setting->value(), $requirement['operator'] ); |
||
85 | |||
86 | } else { |
||
87 | if ( ! is_array( $requirement ) ) { |
||
88 | return true; |
||
89 | } |
||
90 | |||
91 | if ( is_array( $requirement ) ) { |
||
92 | // Handles "OR" functionality. |
||
93 | $show = false; |
||
94 | foreach ( $requirement as $sub_requirement ) { |
||
95 | $show = self::evaluate_requirement( $object, $field, $sub_requirement ); |
||
96 | // No need to go on if one sub_requirement returns true. |
||
97 | if ( $show ) { |
||
98 | return true; |
||
99 | } |
||
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 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: