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 ) { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Parses the output arguments. |
||
| 156 | * Calls the process_output method for each of them. |
||
| 157 | * |
||
| 158 | * @access protected |
||
| 159 | */ |
||
| 160 | protected function parse_output() { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Parses an output and creates the styles array for it. |
||
| 213 | * |
||
| 214 | * @access protected |
||
| 215 | * @param array $output The field output. |
||
| 216 | * @param string $value The value. |
||
| 217 | * |
||
| 218 | * @return void |
||
| 219 | */ |
||
| 220 | protected function process_output( $output, $value ) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Some CSS properties are unique. |
||
| 247 | * We need to tweak the value to make everything works as expected. |
||
| 248 | * |
||
| 249 | * @access protected |
||
| 250 | * @param string $property The CSS property. |
||
| 251 | * @param string $value The value. |
||
| 252 | * |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | protected function process_property_value( $property, $value ) { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Returns the value. |
||
| 271 | * |
||
| 272 | * @access protected |
||
| 273 | * @param string|array $value The value. |
||
| 274 | * @param array $output The field "output". |
||
| 275 | * @return string|array |
||
| 276 | */ |
||
| 277 | protected function process_value( $value, $output ) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Exploses the private $styles property to the world |
||
| 286 | * |
||
| 287 | * @access protected |
||
| 288 | * @return array |
||
| 289 | */ |
||
| 290 | public function get_styles() { |
||
| 293 | } |
||
| 294 |
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: