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 ) { |
||
| 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 ) { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Get the event's actor. |
||
| 387 | * |
||
| 388 | * @param string $current_filter Current wp-admin page. |
||
| 389 | * @param object $args Sync event. |
||
| 390 | * @return array Actor information. |
||
| 391 | */ |
||
| 392 | public function get_actor( $current_filter, $args ) { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Should user data be sent as the actor? |
||
| 430 | * |
||
| 431 | * @param string $current_filter The current WordPress filter being executed. |
||
| 432 | * @return bool |
||
| 433 | */ |
||
| 434 | public function should_send_user_data_with_actor( $current_filter ) { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Sets Listener defaults. |
||
| 451 | */ |
||
| 452 | public function set_defaults() { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Get the request URL. |
||
| 461 | * |
||
| 462 | * @return string Request URL, if known. Otherwise, wp-admin or home_url. |
||
| 463 | */ |
||
| 464 | public function get_request_url() { |
||
| 470 | } |
||
| 471 |
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: