Complex classes like Listener 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 Listener, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Listener { |
||
16 | const QUEUE_STATE_CHECK_TRANSIENT = 'jetpack_sync_last_checked_queue_state'; |
||
17 | const QUEUE_STATE_CHECK_TIMEOUT = 300; // 5 minutes. |
||
18 | |||
19 | /** |
||
20 | * Sync queue. |
||
21 | * |
||
22 | * @var object |
||
23 | */ |
||
24 | private $sync_queue; |
||
25 | |||
26 | /** |
||
27 | * Full sync queue. |
||
28 | * |
||
29 | * @var object |
||
30 | */ |
||
31 | private $full_sync_queue; |
||
32 | |||
33 | /** |
||
34 | * Sync queue size limit. |
||
35 | * |
||
36 | * @var int size limit. |
||
37 | */ |
||
38 | private $sync_queue_size_limit; |
||
39 | |||
40 | /** |
||
41 | * Sync queue lag limit. |
||
42 | * |
||
43 | * @var int Lag limit. |
||
44 | */ |
||
45 | private $sync_queue_lag_limit; |
||
46 | |||
47 | /** |
||
48 | * Singleton implementation. |
||
49 | * |
||
50 | * @var Listener |
||
51 | */ |
||
52 | private static $instance; |
||
53 | |||
54 | /** |
||
55 | * Get the Listener instance. |
||
56 | * |
||
57 | * @return Listener |
||
58 | */ |
||
59 | public static function get_instance() { |
||
66 | |||
67 | /** |
||
68 | * Listener constructor. |
||
69 | * |
||
70 | * This is necessary because you can't use "new" when you declare instance properties >:( |
||
71 | */ |
||
72 | protected function __construct() { |
||
76 | |||
77 | /** |
||
78 | * Sync Listener init. |
||
79 | */ |
||
80 | private function init() { |
||
99 | |||
100 | /** |
||
101 | * Get incremental sync queue. |
||
102 | */ |
||
103 | public function get_sync_queue() { |
||
106 | |||
107 | /** |
||
108 | * Gets the full sync queue. |
||
109 | */ |
||
110 | public function get_full_sync_queue() { |
||
113 | |||
114 | /** |
||
115 | * Sets queue size limit. |
||
116 | * |
||
117 | * @param int $limit Queue size limit. |
||
118 | */ |
||
119 | public function set_queue_size_limit( $limit ) { |
||
122 | |||
123 | /** |
||
124 | * Get queue size limit. |
||
125 | */ |
||
126 | public function get_queue_size_limit() { |
||
129 | |||
130 | /** |
||
131 | * Sets the queue lag limit. |
||
132 | * |
||
133 | * @param int $age Queue lag limit. |
||
134 | */ |
||
135 | public function set_queue_lag_limit( $age ) { |
||
138 | |||
139 | /** |
||
140 | * Return value of queue lag limit. |
||
141 | */ |
||
142 | public function get_queue_lag_limit() { |
||
145 | |||
146 | /** |
||
147 | * Force a recheck of the queue limit. |
||
148 | */ |
||
149 | public function force_recheck_queue_limit() { |
||
153 | |||
154 | /** |
||
155 | * Determine if an item can be added to the queue. |
||
156 | * |
||
157 | * Prevent adding items to the queue if it hasn't sent an item for 15 mins |
||
158 | * AND the queue is over 1000 items long (by default). |
||
159 | * |
||
160 | * @param object $queue Sync queue. |
||
161 | * @return bool |
||
162 | */ |
||
163 | public function can_add_to_queue( $queue ) { |
||
183 | |||
184 | /** |
||
185 | * Full sync action handler. |
||
186 | * |
||
187 | * @param mixed ...$args Args passed to the action. |
||
188 | */ |
||
189 | public function full_sync_action_handler( ...$args ) { |
||
192 | |||
193 | /** |
||
194 | * Action handler. |
||
195 | * |
||
196 | * @param mixed ...$args Args passed to the action. |
||
197 | */ |
||
198 | public function action_handler( ...$args ) { |
||
201 | |||
202 | // add many actions to the queue directly, without invoking them. |
||
203 | |||
204 | /** |
||
205 | * Bulk add action to the queue. |
||
206 | * |
||
207 | * @param string $action_name The name the full sync action. |
||
208 | * @param array $args_array Array of chunked arguments. |
||
209 | */ |
||
210 | public function bulk_enqueue_full_sync_actions( $action_name, $args_array ) { |
||
260 | |||
261 | /** |
||
262 | * Enqueue the action. |
||
263 | * |
||
264 | * @param string $current_filter Current WordPress filter. |
||
265 | * @param object $args Sync args. |
||
266 | * @param string $queue Sync queue. |
||
267 | */ |
||
268 | public function enqueue_action( $current_filter, $args, $queue ) { |
||
269 | // don't enqueue an action during the outbound http request - this prevents recursion. |
||
270 | if ( Settings::is_sending() ) { |
||
271 | return; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Add an action hook to execute when anything on the whitelist gets sent to the queue to sync. |
||
276 | * |
||
277 | * @module sync |
||
278 | * |
||
279 | * @since 5.9.0 |
||
280 | */ |
||
281 | do_action( 'jetpack_sync_action_before_enqueue' ); |
||
282 | |||
283 | /** |
||
284 | * Modify or reject the data within an action before it is enqueued locally. |
||
285 | * |
||
286 | * @since 4.2.0 |
||
287 | * |
||
288 | * @param array The action parameters |
||
289 | */ |
||
290 | $args = apply_filters( "jetpack_sync_before_enqueue_$current_filter", $args ); |
||
291 | |||
292 | // allow listeners to abort. |
||
293 | if ( false === $args ) { |
||
294 | return; |
||
295 | } |
||
296 | |||
297 | /* |
||
298 | * Periodically check the size of the queue, and disable adding to it if |
||
299 | * it exceeds some limit AND the oldest item exceeds the age limit (i.e. sending has stopped). |
||
300 | */ |
||
301 | if ( ! $this->can_add_to_queue( $queue ) && Settings::is_sync_enabled() ) { |
||
302 | if ( 'sync' === $queue->id ) { |
||
303 | $this->sync_data_loss( $queue ); |
||
304 | } |
||
305 | return; |
||
306 | } |
||
307 | |||
308 | /* |
||
309 | * If we add any items to the queue, we should try to ensure that our script |
||
310 | * can't be killed before they are sent. |
||
311 | */ |
||
312 | if ( function_exists( 'ignore_user_abort' ) ) { |
||
313 | ignore_user_abort( true ); |
||
314 | } |
||
315 | |||
316 | if ( |
||
317 | 'sync' === $queue->id || |
||
318 | in_array( |
||
319 | $current_filter, |
||
320 | array( |
||
321 | 'jetpack_full_sync_start', |
||
322 | 'jetpack_full_sync_end', |
||
323 | 'jetpack_full_sync_cancel', |
||
324 | ), |
||
325 | true |
||
326 | ) |
||
327 | ) { |
||
328 | $queue->add( |
||
329 | array( |
||
330 | $current_filter, |
||
331 | $args, |
||
332 | get_current_user_id(), |
||
333 | microtime( true ), |
||
334 | Settings::is_importing(), |
||
335 | $this->get_actor( $current_filter, $args ), |
||
336 | ) |
||
337 | ); |
||
338 | } else { |
||
339 | $queue->add( |
||
340 | array( |
||
341 | $current_filter, |
||
342 | $args, |
||
343 | get_current_user_id(), |
||
344 | microtime( true ), |
||
345 | Settings::is_importing(), |
||
346 | ) |
||
347 | ); |
||
348 | } |
||
349 | |||
350 | // since we've added some items, let's try to load the sender so we can send them as quickly as possible. |
||
351 | if ( ! Actions::$sender ) { |
||
352 | add_filter( 'jetpack_sync_sender_should_load', '__return_true' ); |
||
353 | if ( did_action( 'init' ) ) { |
||
354 | Actions::add_sender_shutdown(); |
||
355 | } |
||
356 | } |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * Sync Data Loss Handler |
||
361 | * |
||
362 | * @param Queue $queue Sync queue. |
||
363 | * @return boolean was send successful |
||
364 | */ |
||
365 | public function sync_data_loss( $queue ) { |
||
377 | |||
378 | /** |
||
379 | * Get the event's actor. |
||
380 | * |
||
381 | * @param string $current_filter Current wp-admin page. |
||
382 | * @param object $args Sync event. |
||
383 | * @return array Actor information. |
||
384 | */ |
||
385 | public function get_actor( $current_filter, $args ) { |
||
420 | |||
421 | /** |
||
422 | * Should user data be sent as the actor? |
||
423 | * |
||
424 | * @param string $current_filter The current WordPress filter being executed. |
||
425 | * @return bool |
||
426 | */ |
||
427 | public function should_send_user_data_with_actor( $current_filter ) { |
||
441 | |||
442 | /** |
||
443 | * Sets Listener defaults. |
||
444 | */ |
||
445 | public function set_defaults() { |
||
451 | |||
452 | /** |
||
453 | * Get the request URL. |
||
454 | * |
||
455 | * @return string Request URL, if known. Otherwise, wp-admin or home_url. |
||
456 | */ |
||
457 | public function get_request_url() { |
||
463 | } |
||
464 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: