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() { |
||
142 | return wp_list_pluck( $this->payment_gateways, 'id' ); |
||
143 | } |
||
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 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.