Complex classes like Kirki_Util 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_Util, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Kirki_Util { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Constructor. |
||
| 20 | * |
||
| 21 | * @since 3.0.9 |
||
| 22 | * @access public |
||
| 23 | */ |
||
| 24 | public function __construct() { |
||
| 25 | add_action( 'after_setup_theme', array( $this, 'acf_pro_compatibility' ) ); |
||
| 26 | add_filter( 'http_request_args', array( $this, 'http_request' ), 10, 2 ); |
||
| 27 | // add_filter( 'option_active_plugins', array( $this, 'is_plugin_active' ) ); |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Changes select2 version in ACF. |
||
| 32 | * Fixes a plugin conflict that was causing select fields to crash |
||
| 33 | * because of a version mismatch between ACF's and Kirki's select2 scripts. |
||
| 34 | * Props @hellor0bot |
||
| 35 | * |
||
| 36 | * @see https://github.com/aristath/kirki/issues/1302 |
||
| 37 | * @access public |
||
| 38 | * @since 3.0.0 |
||
| 39 | */ |
||
| 40 | public function acf_pro_compatibility() { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Determine if Kirki is installed as a plugin. |
||
| 48 | * |
||
| 49 | * @static |
||
| 50 | * @access public |
||
| 51 | * @since 3.0.0 |
||
| 52 | * @return bool |
||
| 53 | */ |
||
| 54 | public static function is_plugin() { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Build the variables. |
||
| 90 | * |
||
| 91 | * @static |
||
| 92 | * @access public |
||
| 93 | * @since 3.0.9 |
||
| 94 | * @return array Formatted as array( 'variable-name' => value ). |
||
| 95 | */ |
||
| 96 | public static function get_variables() { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Plugin is active. |
||
| 137 | * |
||
| 138 | * @since 3.0.0 |
||
| 139 | * @access public |
||
| 140 | * @param array $plugins An array of active plugins. |
||
| 141 | * @return array Active plugins. |
||
| 142 | */ |
||
| 143 | public function is_plugin_active( $plugins ) { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * HTTP Request injection. |
||
| 172 | * |
||
| 173 | * @access public |
||
| 174 | * @since 3.0.0 |
||
| 175 | * @param array $r The request params. |
||
| 176 | * @param string $url The request URL. |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | public function http_request( $r = array(), $url = '' ) { |
||
| 203 | } |
||
| 204 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: