Complex classes like Kirki_Fonts_Google 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_Fonts_Google, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | final class Kirki_Fonts_Google { |
||
18 | |||
19 | /** |
||
20 | * The Kirki_Fonts_Google instance. |
||
21 | * We use the singleton pattern here to avoid loading the google-font array multiple times. |
||
22 | * This is mostly a performance tweak. |
||
23 | * |
||
24 | * @access private |
||
25 | * @var null|object |
||
26 | */ |
||
27 | private static $instance = null; |
||
28 | |||
29 | /** |
||
30 | * If set to true, forces loading ALL variants. |
||
31 | * |
||
32 | * @static |
||
33 | * @access public |
||
34 | * @var bool |
||
35 | */ |
||
36 | public static $force_load_all_variants = false; |
||
37 | |||
38 | /** |
||
39 | * If set to true, forces loading ALL subsets. |
||
40 | * |
||
41 | * @static |
||
42 | * @access public |
||
43 | * @var bool |
||
44 | */ |
||
45 | public static $force_load_all_subsets = false; |
||
46 | |||
47 | /** |
||
48 | * The array of fonts |
||
49 | * |
||
50 | * @access public |
||
51 | * @var array |
||
52 | */ |
||
53 | public $fonts = array(); |
||
54 | |||
55 | /** |
||
56 | * An array of all google fonts. |
||
57 | * |
||
58 | * @access private |
||
59 | * @var array |
||
60 | */ |
||
61 | private $google_fonts = array(); |
||
62 | |||
63 | /** |
||
64 | * The array of subsets |
||
65 | * |
||
66 | * @access public |
||
67 | * @var array |
||
68 | */ |
||
69 | public $subsets = array(); |
||
70 | |||
71 | /** |
||
72 | * The class constructor. |
||
73 | */ |
||
74 | private function __construct() { |
||
87 | |||
88 | /** |
||
89 | * Get the one, true instance of this class. |
||
90 | * Prevents performance issues since this is only loaded once. |
||
91 | * |
||
92 | * @return object Kirki_Fonts_Google |
||
93 | */ |
||
94 | public static function get_instance() { |
||
100 | |||
101 | /** |
||
102 | * Processes the arguments of a field |
||
103 | * determines if it's a typography field |
||
104 | * and if it is, then takes appropriate actions. |
||
105 | * |
||
106 | * @param array $args The field arguments. |
||
107 | */ |
||
108 | public function generate_google_font( $args ) { |
||
192 | |||
193 | /** |
||
194 | * Determines the vbalidity of the selected font as well as its properties. |
||
195 | * This is vital to make sure that the google-font script that we'll generate later |
||
196 | * does not contain any invalid options. |
||
197 | */ |
||
198 | public function process_fonts() { |
||
243 | } |
||
244 |