Complex classes like WC_Payment_Gateways 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 WC_Payment_Gateways, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class WC_Payment_Gateways { |
||
19 | |||
20 | /** @var array Array of payment gateway classes. */ |
||
21 | public $payment_gateways; |
||
22 | |||
23 | /** |
||
24 | * @var WC_Payment_Gateways The single instance of the class |
||
25 | * @since 2.1 |
||
26 | */ |
||
27 | protected static $_instance = null; |
||
28 | |||
29 | /** |
||
30 | * Main WC_Payment_Gateways Instance. |
||
31 | * |
||
32 | * Ensures only one instance of WC_Payment_Gateways is loaded or can be loaded. |
||
33 | * |
||
34 | * @since 2.1 |
||
35 | * @static |
||
36 | * @return WC_Payment_Gateways Main instance |
||
37 | */ |
||
38 | public static function instance() { |
||
44 | |||
45 | /** |
||
46 | * Cloning is forbidden. |
||
47 | * |
||
48 | * @since 2.1 |
||
49 | */ |
||
50 | public function __clone() { |
||
53 | |||
54 | /** |
||
55 | * Unserializing instances of this class is forbidden. |
||
56 | * |
||
57 | * @since 2.1 |
||
58 | */ |
||
59 | public function __wakeup() { |
||
62 | |||
63 | /** |
||
64 | * Initialize payment gateways. |
||
65 | */ |
||
66 | public function __construct() { |
||
69 | |||
70 | /** |
||
71 | * Load gateways and hook in functions. |
||
72 | */ |
||
73 | public function init() { |
||
119 | |||
120 | /** |
||
121 | * Get gateways. |
||
122 | * @return array |
||
123 | */ |
||
124 | public function payment_gateways() { |
||
135 | |||
136 | /** |
||
137 | * Get array of registered gateway ids |
||
138 | * @since 2.6.0 |
||
139 | * @return array of strings |
||
140 | */ |
||
141 | public function get_payment_gateway_ids() { |
||
144 | |||
145 | /** |
||
146 | * Get available gateways. |
||
147 | * |
||
148 | * @return array |
||
149 | */ |
||
150 | public function get_available_payment_gateways() { |
||
167 | |||
168 | /** |
||
169 | * Set the current, active gateway. |
||
170 | * |
||
171 | * @param array $gateway Available payment gateways. |
||
172 | */ |
||
173 | public function set_current_gateway( $gateways ) { |
||
200 | |||
201 | /** |
||
202 | * Save options in admin. |
||
203 | */ |
||
204 | public function process_admin_options() { |
||
218 | } |
||
219 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.