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 | const ALWAYS_SEND_UPDATES_TO_THESE_OPTIONS_NEXT_TICK = array( |
||
60 | 'stylesheet', |
||
61 | ); |
||
62 | /** |
||
63 | * Setting this value to true will make it so that the callables will not be unlocked |
||
64 | * but the lock will be removed after content is send so that callables will be |
||
65 | * sent in the next request. |
||
66 | * |
||
67 | * @var bool |
||
68 | */ |
||
69 | private $force_send_callables_on_next_tick = false; |
||
70 | |||
71 | /** |
||
72 | * For some options, the callable key differs from the option name/key |
||
73 | * |
||
74 | * @access public |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | const OPTION_NAMES_TO_CALLABLE_NAMES = array( |
||
79 | // @TODO: Audit the other option names for differences between the option names and callable names. |
||
80 | 'home' => 'home_url', |
||
81 | 'siteurl' => 'site_url', |
||
82 | ); |
||
83 | |||
84 | /** |
||
85 | * Sync module name. |
||
86 | * |
||
87 | * @access public |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | public function name() { |
||
94 | |||
95 | /** |
||
96 | * Set module defaults. |
||
97 | * Define the callable whitelist based on whether this is a single site or a multisite installation. |
||
98 | * |
||
99 | * @access public |
||
100 | */ |
||
101 | public function set_defaults() { |
||
109 | |||
110 | /** |
||
111 | * Initialize callables action listeners. |
||
112 | * |
||
113 | * @access public |
||
114 | * |
||
115 | * @param callable $callable Action handler callable. |
||
116 | */ |
||
117 | public function init_listeners( $callable ) { |
||
118 | add_action( 'jetpack_sync_callable', $callable, 10, 2 ); |
||
119 | add_action( 'current_screen', array( $this, 'set_plugin_action_links' ), 9999 ); // Should happen very late. |
||
120 | |||
121 | View Code Duplication | foreach ( self::ALWAYS_SEND_UPDATES_TO_THESE_OPTIONS as $option ) { |
|
122 | add_action( "update_option_{$option}", array( $this, 'unlock_sync_callable' ) ); |
||
123 | add_action( "delete_option_{$option}", array( $this, 'unlock_sync_callable' ) ); |
||
124 | } |
||
125 | |||
126 | View Code Duplication | foreach ( self::ALWAYS_SEND_UPDATES_TO_THESE_OPTIONS_NEXT_TICK as $option ) { |
|
127 | add_action( "update_option_{$option}", array( $this, 'unlock_sync_callable_next_tick' ) ); |
||
128 | add_action( "delete_option_{$option}", array( $this, 'unlock_sync_callable_next_tick' ) ); |
||
129 | } |
||
130 | |||
131 | // Provide a hook so that hosts can send changes to certain callables right away. |
||
132 | // Especially useful when a host uses constants to change home and siteurl. |
||
133 | add_action( 'jetpack_sync_unlock_sync_callable', array( $this, 'unlock_sync_callable' ) ); |
||
134 | |||
135 | // get_plugins and wp_version |
||
136 | // gets fired when new code gets installed, updates etc. |
||
137 | add_action( 'upgrader_process_complete', array( $this, 'unlock_plugin_action_link_and_callables' ) ); |
||
138 | add_action( 'update_option_active_plugins', array( $this, 'unlock_plugin_action_link_and_callables' ) ); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Initialize callables action listeners for full sync. |
||
143 | * |
||
144 | * @access public |
||
145 | * |
||
146 | * @param callable $callable Action handler callable. |
||
147 | */ |
||
148 | public function init_full_sync_listeners( $callable ) { |
||
151 | |||
152 | /** |
||
153 | * Initialize the module in the sender. |
||
154 | * |
||
155 | * @access public |
||
156 | */ |
||
157 | public function init_before_send() { |
||
163 | |||
164 | /** |
||
165 | * Perform module cleanup. |
||
166 | * Deletes any transients and options that this module uses. |
||
167 | * Usually triggered when uninstalling the plugin. |
||
168 | * |
||
169 | * @access public |
||
170 | */ |
||
171 | public function reset_data() { |
||
180 | |||
181 | /** |
||
182 | * Set the callable whitelist. |
||
183 | * |
||
184 | * @access public |
||
185 | * |
||
186 | * @param array $callables The new callables whitelist. |
||
187 | */ |
||
188 | public function set_callable_whitelist( $callables ) { |
||
191 | |||
192 | /** |
||
193 | * Get the callable whitelist. |
||
194 | * |
||
195 | * @access public |
||
196 | * |
||
197 | * @return array The callables whitelist. |
||
198 | */ |
||
199 | public function get_callable_whitelist() { |
||
202 | |||
203 | /** |
||
204 | * Retrieve all callables as per the current callables whitelist. |
||
205 | * |
||
206 | * @access public |
||
207 | * |
||
208 | * @return array All callables. |
||
209 | */ |
||
210 | public function get_all_callables() { |
||
221 | |||
222 | /** |
||
223 | * Invoke a particular callable. |
||
224 | * Used as a wrapper to standartize invocation. |
||
225 | * |
||
226 | * @access private |
||
227 | * |
||
228 | * @param callable $callable Callable to invoke. |
||
229 | * @return mixed Return value of the callable, null if not callable. |
||
230 | */ |
||
231 | private function get_callable( $callable ) { |
||
238 | |||
239 | /** |
||
240 | * Enqueue the callable actions for full sync. |
||
241 | * |
||
242 | * @access public |
||
243 | * |
||
244 | * @param array $config Full sync configuration for this sync module. |
||
245 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
246 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
247 | * @return array Number of actions enqueued, and next module state. |
||
248 | */ |
||
249 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
262 | |||
263 | /** |
||
264 | * Send the callable actions for full sync. |
||
265 | * |
||
266 | * @access public |
||
267 | * |
||
268 | * @param array $config Full sync configuration for this sync module. |
||
269 | * @param int $send_until The timestamp until the current request can send. |
||
270 | * @param array $status This Module Full Sync Status. |
||
271 | * |
||
272 | * @return array This Module Full Sync Status. |
||
273 | */ |
||
274 | public function send_full_sync_actions( $config, $send_until, $status ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
281 | |||
282 | /** |
||
283 | * Retrieve an estimated number of actions that will be enqueued. |
||
284 | * |
||
285 | * @access public |
||
286 | * |
||
287 | * @param array $config Full sync configuration for this sync module. |
||
288 | * @return array Number of items yet to be enqueued. |
||
289 | */ |
||
290 | public function estimate_full_sync_actions( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
293 | |||
294 | /** |
||
295 | * Retrieve the actions that will be sent for this module during a full sync. |
||
296 | * |
||
297 | * @access public |
||
298 | * |
||
299 | * @return array Full sync actions of this module. |
||
300 | */ |
||
301 | public function get_full_sync_actions() { |
||
304 | |||
305 | /** |
||
306 | * Unlock callables so they would be available for syncing again. |
||
307 | * |
||
308 | * @access public |
||
309 | */ |
||
310 | public function unlock_sync_callable() { |
||
313 | |||
314 | /** |
||
315 | * Unlock callables on the next tick. |
||
316 | * Sometime the true callable values are only present on the next tick. |
||
317 | * When switching themes for example. |
||
318 | * |
||
319 | * @access public |
||
320 | */ |
||
321 | public function unlock_sync_callable_next_tick() { |
||
324 | |||
325 | /** |
||
326 | * Unlock callables and plugin action links. |
||
327 | * |
||
328 | * @access public |
||
329 | */ |
||
330 | public function unlock_plugin_action_link_and_callables() { |
||
335 | |||
336 | /** |
||
337 | * Parse and store the plugin action links if on the plugins page. |
||
338 | * |
||
339 | * @uses \DOMDocument |
||
340 | * @uses libxml_use_internal_errors |
||
341 | * @uses mb_convert_encoding |
||
342 | * |
||
343 | * @access public |
||
344 | */ |
||
345 | public function set_plugin_action_links() { |
||
423 | |||
424 | /** |
||
425 | * Whether a certain callable should be sent. |
||
426 | * |
||
427 | * @access public |
||
428 | * |
||
429 | * @param array $callable_checksums Callable checksums. |
||
430 | * @param string $name Name of the callable. |
||
431 | * @param string $checksum A checksum of the callable. |
||
432 | * @return boolean Whether to send the callable. |
||
433 | */ |
||
434 | public function should_send_callable( $callable_checksums, $name, $checksum ) { |
||
446 | |||
447 | /** |
||
448 | * Sync the callables if we're supposed to. |
||
449 | * |
||
450 | * @access public |
||
451 | */ |
||
452 | public function maybe_sync_callables() { |
||
516 | |||
517 | /** |
||
518 | * Get the callables that should always be sent, e.g. on cron. |
||
519 | * |
||
520 | * @return array Callables that should always be sent |
||
521 | */ |
||
522 | protected function get_always_sent_callables() { |
||
541 | |||
542 | /** |
||
543 | * Expand the callables within a hook before they are serialized and sent to the server. |
||
544 | * |
||
545 | * @access public |
||
546 | * |
||
547 | * @param array $args The hook parameters. |
||
548 | * @return array $args The hook parameters. |
||
549 | */ |
||
550 | public function expand_callables( $args ) { |
||
563 | |||
564 | /** |
||
565 | * Return Total number of objects. |
||
566 | * |
||
567 | * @param array $config Full Sync config. |
||
568 | * |
||
569 | * @return int total |
||
570 | */ |
||
571 | public function total( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
574 | |||
575 | /** |
||
576 | * Retrieve a set of callables by their IDs. |
||
577 | * |
||
578 | * @access public |
||
579 | * |
||
580 | * @param string $object_type Object type. |
||
581 | * @param array $ids Object IDs. |
||
582 | * @return array Array of objects. |
||
583 | */ |
||
584 | View Code Duplication | public function get_objects_by_id( $object_type, $ids ) { |
|
585 | if ( empty( $ids ) || empty( $object_type ) || 'callable' !== $object_type ) { |
||
586 | return array(); |
||
587 | } |
||
588 | |||
589 | $objects = array(); |
||
590 | foreach ( (array) $ids as $id ) { |
||
591 | $object = $this->get_object_by_id( $object_type, $id ); |
||
592 | |||
593 | if ( 'CALLABLE-DOES-NOT-EXIST' !== $object ) { |
||
594 | if ( 'all' === $id ) { |
||
595 | // If all was requested it contains all options and can simply be returned. |
||
596 | return $object; |
||
597 | } |
||
598 | $objects[ $id ] = $object; |
||
599 | } |
||
600 | } |
||
601 | |||
602 | return $objects; |
||
603 | } |
||
604 | |||
605 | /** |
||
606 | * Retrieve a callable by its name. |
||
607 | * |
||
608 | * @access public |
||
609 | * |
||
610 | * @param string $object_type Type of the sync object. |
||
611 | * @param string $id ID of the sync object. |
||
612 | * @return mixed Value of Callable. |
||
613 | */ |
||
614 | public function get_object_by_id( $object_type, $id ) { |
||
632 | |||
633 | } |
||
634 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.