Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Callables 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 Callables, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class Callables extends Module { |
||
19 | /** |
||
20 | * Name of the callables checksum option. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | const CALLABLES_CHECKSUM_OPTION_NAME = 'jetpack_callables_sync_checksum'; |
||
25 | |||
26 | /** |
||
27 | * Name of the transient for locking callables. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | const CALLABLES_AWAIT_TRANSIENT_NAME = 'jetpack_sync_callables_await'; |
||
32 | |||
33 | /** |
||
34 | * Whitelist for callables we want to sync. |
||
35 | * |
||
36 | * @access private |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | private $callable_whitelist; |
||
41 | |||
42 | /** |
||
43 | * For some options, we should always send the change right away! |
||
44 | * |
||
45 | * @access public |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | const ALWAYS_SEND_UPDATES_TO_THESE_OPTIONS = array( |
||
50 | 'jetpack_active_modules', |
||
51 | 'home', // option is home, callable is home_url. |
||
52 | 'siteurl', |
||
53 | 'jetpack_sync_error_idc', |
||
54 | 'paused_plugins', |
||
55 | 'paused_themes', |
||
56 | ); |
||
57 | |||
58 | /** |
||
59 | * For some options, the callable key differs from the option name/key |
||
60 | * |
||
61 | * @access public |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | const OPTION_NAMES_TO_CALLABLE_NAMES = array( |
||
66 | // @TODO: Audit the other option names for differences between the option names and callable names. |
||
67 | 'home' => 'home_url', |
||
68 | ); |
||
69 | |||
70 | /** |
||
71 | * Sync module name. |
||
72 | * |
||
73 | * @access public |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | public function name() { |
||
80 | |||
81 | /** |
||
82 | * Set module defaults. |
||
83 | * Define the callable whitelist based on whether this is a single site or a multisite installation. |
||
84 | * |
||
85 | * @access public |
||
86 | */ |
||
87 | public function set_defaults() { |
||
94 | |||
95 | /** |
||
96 | * Initialize callables action listeners. |
||
97 | * |
||
98 | * @access public |
||
99 | * |
||
100 | * @param callable $callable Action handler callable. |
||
101 | */ |
||
102 | public function init_listeners( $callable ) { |
||
120 | |||
121 | /** |
||
122 | * Initialize callables action listeners for full sync. |
||
123 | * |
||
124 | * @access public |
||
125 | * |
||
126 | * @param callable $callable Action handler callable. |
||
127 | */ |
||
128 | public function init_full_sync_listeners( $callable ) { |
||
131 | |||
132 | /** |
||
133 | * Initialize the module in the sender. |
||
134 | * |
||
135 | * @access public |
||
136 | */ |
||
137 | public function init_before_send() { |
||
143 | |||
144 | /** |
||
145 | * Perform module cleanup. |
||
146 | * Deletes any transients and options that this module uses. |
||
147 | * Usually triggered when uninstalling the plugin. |
||
148 | * |
||
149 | * @access public |
||
150 | */ |
||
151 | public function reset_data() { |
||
160 | |||
161 | /** |
||
162 | * Set the callable whitelist. |
||
163 | * |
||
164 | * @access public |
||
165 | * |
||
166 | * @param array $callables The new callables whitelist. |
||
167 | */ |
||
168 | public function set_callable_whitelist( $callables ) { |
||
171 | |||
172 | /** |
||
173 | * Get the callable whitelist. |
||
174 | * |
||
175 | * @access public |
||
176 | * |
||
177 | * @return array The callables whitelist. |
||
178 | */ |
||
179 | public function get_callable_whitelist() { |
||
182 | |||
183 | /** |
||
184 | * Retrieve all callables as per the current callables whitelist. |
||
185 | * |
||
186 | * @access public |
||
187 | * |
||
188 | * @return array All callables. |
||
189 | */ |
||
190 | public function get_all_callables() { |
||
201 | |||
202 | /** |
||
203 | * Invoke a particular callable. |
||
204 | * Used as a wrapper to standartize invocation. |
||
205 | * |
||
206 | * @access private |
||
207 | * |
||
208 | * @param callable $callable Callable to invoke. |
||
209 | * @return mixed Return value of the callable. |
||
210 | */ |
||
211 | private function get_callable( $callable ) { |
||
214 | |||
215 | /** |
||
216 | * Enqueue the callable actions for full sync. |
||
217 | * |
||
218 | * @access public |
||
219 | * |
||
220 | * @param array $config Full sync configuration for this sync module. |
||
221 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
222 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
223 | * @return array Number of actions enqueued, and next module state. |
||
224 | */ |
||
225 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
238 | |||
239 | /** |
||
240 | * Retrieve an estimated number of actions that will be enqueued. |
||
241 | * |
||
242 | * @access public |
||
243 | * |
||
244 | * @param array $config Full sync configuration for this sync module. |
||
245 | * @return array Number of items yet to be enqueued. |
||
246 | */ |
||
247 | public function estimate_full_sync_actions( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
250 | |||
251 | /** |
||
252 | * Retrieve the actions that will be sent for this module during a full sync. |
||
253 | * |
||
254 | * @access public |
||
255 | * |
||
256 | * @return array Full sync actions of this module. |
||
257 | */ |
||
258 | public function get_full_sync_actions() { |
||
261 | |||
262 | /** |
||
263 | * Unlock callables so they would be available for syncing again. |
||
264 | * |
||
265 | * @access public |
||
266 | */ |
||
267 | public function unlock_sync_callable() { |
||
270 | |||
271 | /** |
||
272 | * Unlock callables and plugin action links. |
||
273 | * |
||
274 | * @access public |
||
275 | */ |
||
276 | public function unlock_plugin_action_link_and_callables() { |
||
281 | |||
282 | /** |
||
283 | * Parse and store the plugin action links if on the plugins page. |
||
284 | * |
||
285 | * @uses \DOMDocument |
||
286 | * @uses libxml_use_internal_errors |
||
287 | * @uses mb_convert_encoding |
||
288 | * |
||
289 | * @access public |
||
290 | */ |
||
291 | public function set_plugin_action_links() { |
||
364 | |||
365 | /** |
||
366 | * Whether a certain callable should be sent. |
||
367 | * |
||
368 | * @access public |
||
369 | * |
||
370 | * @param array $callable_checksums Callable checksums. |
||
371 | * @param string $name Name of the callable. |
||
372 | * @param string $checksum A checksum of the callable. |
||
373 | * @return boolean Whether to send the callable. |
||
374 | */ |
||
375 | public function should_send_callable( $callable_checksums, $name, $checksum ) { |
||
387 | |||
388 | /** |
||
389 | * Sync the callables if we're supposed to. |
||
390 | * |
||
391 | * @access public |
||
392 | */ |
||
393 | public function maybe_sync_callables() { |
||
443 | |||
444 | /** |
||
445 | * Get the callables that should always be sent, e.g. on cron. |
||
446 | * |
||
447 | * @return array Callables that should always be sent |
||
448 | */ |
||
449 | protected function get_always_sent_callables() { |
||
468 | |||
469 | /** |
||
470 | * Expand the callables within a hook before they are serialized and sent to the server. |
||
471 | * |
||
472 | * @access public |
||
473 | * |
||
474 | * @param array $args The hook parameters. |
||
475 | * @return array $args The hook parameters. |
||
476 | */ |
||
477 | public function expand_callables( $args ) { |
||
490 | } |
||
491 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.