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 | 'siteurl' => 'site_url', |
||
69 | ); |
||
70 | |||
71 | /** |
||
72 | * Sync module name. |
||
73 | * |
||
74 | * @access public |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | public function name() { |
||
81 | |||
82 | /** |
||
83 | * Set module defaults. |
||
84 | * Define the callable whitelist based on whether this is a single site or a multisite installation. |
||
85 | * |
||
86 | * @access public |
||
87 | */ |
||
88 | public function set_defaults() { |
||
95 | |||
96 | /** |
||
97 | * Initialize callables action listeners. |
||
98 | * |
||
99 | * @access public |
||
100 | * |
||
101 | * @param callable $callable Action handler callable. |
||
102 | */ |
||
103 | public function init_listeners( $callable ) { |
||
121 | |||
122 | /** |
||
123 | * Initialize callables action listeners for full sync. |
||
124 | * |
||
125 | * @access public |
||
126 | * |
||
127 | * @param callable $callable Action handler callable. |
||
128 | */ |
||
129 | public function init_full_sync_listeners( $callable ) { |
||
132 | |||
133 | /** |
||
134 | * Initialize the module in the sender. |
||
135 | * |
||
136 | * @access public |
||
137 | */ |
||
138 | public function init_before_send() { |
||
144 | |||
145 | /** |
||
146 | * Perform module cleanup. |
||
147 | * Deletes any transients and options that this module uses. |
||
148 | * Usually triggered when uninstalling the plugin. |
||
149 | * |
||
150 | * @access public |
||
151 | */ |
||
152 | public function reset_data() { |
||
161 | |||
162 | /** |
||
163 | * Set the callable whitelist. |
||
164 | * |
||
165 | * @access public |
||
166 | * |
||
167 | * @param array $callables The new callables whitelist. |
||
168 | */ |
||
169 | public function set_callable_whitelist( $callables ) { |
||
172 | |||
173 | /** |
||
174 | * Get the callable whitelist. |
||
175 | * |
||
176 | * @access public |
||
177 | * |
||
178 | * @return array The callables whitelist. |
||
179 | */ |
||
180 | public function get_callable_whitelist() { |
||
183 | |||
184 | /** |
||
185 | * Retrieve all callables as per the current callables whitelist. |
||
186 | * |
||
187 | * @access public |
||
188 | * |
||
189 | * @return array All callables. |
||
190 | */ |
||
191 | public function get_all_callables() { |
||
202 | |||
203 | /** |
||
204 | * Invoke a particular callable. |
||
205 | * Used as a wrapper to standartize invocation. |
||
206 | * |
||
207 | * @access private |
||
208 | * |
||
209 | * @param callable $callable Callable to invoke. |
||
210 | * @return mixed Return value of the callable. |
||
211 | */ |
||
212 | private function get_callable( $callable ) { |
||
215 | |||
216 | /** |
||
217 | * Enqueue the callable actions for full sync. |
||
218 | * |
||
219 | * @access public |
||
220 | * |
||
221 | * @param array $config Full sync configuration for this sync module. |
||
222 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
223 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
224 | * @return array Number of actions enqueued, and next module state. |
||
225 | */ |
||
226 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
239 | |||
240 | /** |
||
241 | * Send the callable actions for full sync. |
||
242 | * |
||
243 | * @access public |
||
244 | * |
||
245 | * @param array $config Full sync configuration for this sync module. |
||
246 | * @param int $send_until The timestamp until the current request can send. |
||
247 | * @param array $status This Module Full Sync Status. |
||
248 | * |
||
249 | * @return array This Module Full Sync Status. |
||
250 | */ |
||
251 | public function send_full_sync_actions( $config, $send_until, $status ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
258 | |||
259 | /** |
||
260 | * Retrieve an estimated number of actions that will be enqueued. |
||
261 | * |
||
262 | * @access public |
||
263 | * |
||
264 | * @param array $config Full sync configuration for this sync module. |
||
265 | * @return array Number of items yet to be enqueued. |
||
266 | */ |
||
267 | public function estimate_full_sync_actions( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
270 | |||
271 | /** |
||
272 | * Retrieve the actions that will be sent for this module during a full sync. |
||
273 | * |
||
274 | * @access public |
||
275 | * |
||
276 | * @return array Full sync actions of this module. |
||
277 | */ |
||
278 | public function get_full_sync_actions() { |
||
281 | |||
282 | /** |
||
283 | * Unlock callables so they would be available for syncing again. |
||
284 | * |
||
285 | * @access public |
||
286 | */ |
||
287 | public function unlock_sync_callable() { |
||
290 | |||
291 | /** |
||
292 | * Unlock callables and plugin action links. |
||
293 | * |
||
294 | * @access public |
||
295 | */ |
||
296 | public function unlock_plugin_action_link_and_callables() { |
||
301 | |||
302 | /** |
||
303 | * Parse and store the plugin action links if on the plugins page. |
||
304 | * |
||
305 | * @uses \DOMDocument |
||
306 | * @uses libxml_use_internal_errors |
||
307 | * @uses mb_convert_encoding |
||
308 | * |
||
309 | * @access public |
||
310 | */ |
||
311 | public function set_plugin_action_links() { |
||
384 | |||
385 | /** |
||
386 | * Whether a certain callable should be sent. |
||
387 | * |
||
388 | * @access public |
||
389 | * |
||
390 | * @param array $callable_checksums Callable checksums. |
||
391 | * @param string $name Name of the callable. |
||
392 | * @param string $checksum A checksum of the callable. |
||
393 | * @return boolean Whether to send the callable. |
||
394 | */ |
||
395 | public function should_send_callable( $callable_checksums, $name, $checksum ) { |
||
407 | |||
408 | /** |
||
409 | * Sync the callables if we're supposed to. |
||
410 | * |
||
411 | * @access public |
||
412 | */ |
||
413 | public function maybe_sync_callables() { |
||
463 | |||
464 | /** |
||
465 | * Get the callables that should always be sent, e.g. on cron. |
||
466 | * |
||
467 | * @return array Callables that should always be sent |
||
468 | */ |
||
469 | protected function get_always_sent_callables() { |
||
488 | |||
489 | /** |
||
490 | * Expand the callables within a hook before they are serialized and sent to the server. |
||
491 | * |
||
492 | * @access public |
||
493 | * |
||
494 | * @param array $args The hook parameters. |
||
495 | * @return array $args The hook parameters. |
||
496 | */ |
||
497 | public function expand_callables( $args ) { |
||
510 | |||
511 | /** |
||
512 | * Return Total number of objects. |
||
513 | * |
||
514 | * @param array $config Full Sync config. |
||
515 | * |
||
516 | * @return int total |
||
517 | */ |
||
518 | public function total( $config ) { |
||
521 | |||
522 | } |
||
523 |
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.