Complex classes like Module 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 Module, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | abstract class Module { |
||
19 | /** |
||
20 | * Number of items per chunk when grouping objects for performance reasons. |
||
21 | * |
||
22 | * @access public |
||
23 | * |
||
24 | * @var int |
||
25 | */ |
||
26 | const ARRAY_CHUNK_SIZE = 10; |
||
27 | |||
28 | /** |
||
29 | * Sync module name. |
||
30 | * |
||
31 | * @access public |
||
32 | * |
||
33 | * @return string |
||
34 | */ |
||
35 | abstract public function name(); |
||
36 | |||
37 | /** |
||
38 | * The id field in the database. |
||
39 | * |
||
40 | * @access public |
||
41 | * |
||
42 | * @return string |
||
43 | */ |
||
44 | public function id_field() { |
||
47 | |||
48 | /** |
||
49 | * The table in the database. |
||
50 | * |
||
51 | * @access public |
||
52 | * |
||
53 | * @return string|bool |
||
54 | */ |
||
55 | public function table_name() { |
||
58 | |||
59 | // phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
60 | |||
61 | /** |
||
62 | * Retrieve a sync object by its ID. |
||
63 | * |
||
64 | * @access public |
||
65 | * |
||
66 | * @param string $object_type Type of the sync object. |
||
67 | * @param int $id ID of the sync object. |
||
68 | * @return mixed Object, or false if the object is invalid. |
||
69 | */ |
||
70 | public function get_object_by_id( $object_type, $id ) { |
||
73 | |||
74 | /** |
||
75 | * Initialize callables action listeners. |
||
76 | * Override these to set up listeners and set/reset data/defaults. |
||
77 | * |
||
78 | * @access public |
||
79 | * |
||
80 | * @param callable $callable Action handler callable. |
||
81 | */ |
||
82 | public function init_listeners( $callable ) { |
||
84 | |||
85 | /** |
||
86 | * Initialize module action listeners for full sync. |
||
87 | * |
||
88 | * @access public |
||
89 | * |
||
90 | * @param callable $callable Action handler callable. |
||
91 | */ |
||
92 | public function init_full_sync_listeners( $callable ) { |
||
94 | |||
95 | /** |
||
96 | * Initialize the module in the sender. |
||
97 | * |
||
98 | * @access public |
||
99 | */ |
||
100 | public function init_before_send() { |
||
102 | |||
103 | /** |
||
104 | * Set module defaults. |
||
105 | * |
||
106 | * @access public |
||
107 | */ |
||
108 | public function set_defaults() { |
||
110 | |||
111 | /** |
||
112 | * Perform module cleanup. |
||
113 | * Usually triggered when uninstalling the plugin. |
||
114 | * |
||
115 | * @access public |
||
116 | */ |
||
117 | public function reset_data() { |
||
119 | |||
120 | /** |
||
121 | * Enqueue the module actions for full sync. |
||
122 | * |
||
123 | * @access public |
||
124 | * |
||
125 | * @param array $config Full sync configuration for this sync module. |
||
126 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
127 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
128 | * @return array Number of actions enqueued, and next module state. |
||
129 | */ |
||
130 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
134 | |||
135 | /** |
||
136 | * Retrieve an estimated number of actions that will be enqueued. |
||
137 | * |
||
138 | * @access public |
||
139 | * |
||
140 | * @param array $config Full sync configuration for this sync module. |
||
141 | * @return array Number of items yet to be enqueued. |
||
142 | */ |
||
143 | public function estimate_full_sync_actions( $config ) { |
||
147 | |||
148 | // phpcs:enable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
149 | |||
150 | /** |
||
151 | * Retrieve the actions that will be sent for this module during a full sync. |
||
152 | * |
||
153 | * @access public |
||
154 | * |
||
155 | * @return array Full sync actions of this module. |
||
156 | */ |
||
157 | public function get_full_sync_actions() { |
||
160 | |||
161 | /** |
||
162 | * Get the number of actions that we care about. |
||
163 | * |
||
164 | * @access protected |
||
165 | * |
||
166 | * @param array $action_names Action names we're interested in. |
||
167 | * @param array $actions_to_count Unfiltered list of actions we want to count. |
||
168 | * @return array Number of actions that we're interested in. |
||
169 | */ |
||
170 | protected function count_actions( $action_names, $actions_to_count ) { |
||
173 | |||
174 | /** |
||
175 | * Calculate the checksum of one or more values. |
||
176 | * |
||
177 | * @access protected |
||
178 | * |
||
179 | * @param mixed $values Values to calculate checksum for. |
||
180 | * @return int The checksum. |
||
181 | */ |
||
182 | protected function get_check_sum( $values ) { |
||
185 | |||
186 | /** |
||
187 | * Whether a particular checksum in a set of checksums is valid. |
||
188 | * |
||
189 | * @access protected |
||
190 | * |
||
191 | * @param array $sums_to_check Array of checksums. |
||
192 | * @param string $name Name of the checksum. |
||
193 | * @param int $new_sum Checksum to compare against. |
||
194 | * @return boolean Whether the checksum is valid. |
||
195 | */ |
||
196 | protected function still_valid_checksum( $sums_to_check, $name, $new_sum ) { |
||
203 | |||
204 | /** |
||
205 | * Enqueue all items of a sync type as an action. |
||
206 | * |
||
207 | * @access protected |
||
208 | * |
||
209 | * @param string $action_name Name of the action. |
||
210 | * @param string $table_name Name of the database table. |
||
211 | * @param string $id_field Name of the ID field in the database. |
||
212 | * @param string $where_sql The SQL WHERE clause to filter to the desired items. |
||
213 | * @param int $max_items_to_enqueue Maximum number of items to enqueue in the same time. |
||
214 | * @param boolean $state Whether enqueueing has finished. |
||
215 | * @return array Array, containing the number of chunks and TRUE, indicating enqueueing has finished. |
||
216 | */ |
||
217 | protected function enqueue_all_ids_as_action( $action_name, $table_name, $id_field, $where_sql, $max_items_to_enqueue, $state ) { |
||
263 | |||
264 | /** |
||
265 | * Retrieve chunk IDs with previous interval end. |
||
266 | * |
||
267 | * @access protected |
||
268 | * |
||
269 | * @param array $chunks All remaining items. |
||
270 | * @param int $previous_interval_end The last item from the previous interval. |
||
271 | * @return array Chunk IDs with the previous interval end. |
||
272 | */ |
||
273 | protected function get_chunks_with_preceding_end( $chunks, $previous_interval_end ) { |
||
285 | |||
286 | /** |
||
287 | * Get metadata of a particular object type within the designated meta key whitelist. |
||
288 | * |
||
289 | * @access protected |
||
290 | * |
||
291 | * @todo Refactor to use $wpdb->prepare() on the SQL query. |
||
292 | * |
||
293 | * @param array $ids Object IDs. |
||
294 | * @param string $meta_type Meta type. |
||
295 | * @param array $meta_key_whitelist Meta key whitelist. |
||
296 | * @return array Unserialized meta values. |
||
297 | */ |
||
298 | protected function get_metadata( $ids, $meta_type, $meta_key_whitelist ) { |
||
319 | |||
320 | /** |
||
321 | * Initialize listeners for the particular meta type. |
||
322 | * |
||
323 | * @access public |
||
324 | * |
||
325 | * @param string $meta_type Meta type. |
||
326 | * @param callable $callable Action handler callable. |
||
327 | */ |
||
328 | public function init_listeners_for_meta_type( $meta_type, $callable ) { |
||
333 | |||
334 | /** |
||
335 | * Initialize meta whitelist handler for the particular meta type. |
||
336 | * |
||
337 | * @access public |
||
338 | * |
||
339 | * @param string $meta_type Meta type. |
||
340 | * @param callable $whitelist_handler Action handler callable. |
||
341 | */ |
||
342 | public function init_meta_whitelist_handler( $meta_type, $whitelist_handler ) { |
||
347 | |||
348 | /** |
||
349 | * Retrieve the term relationships for the specified object IDs. |
||
350 | * |
||
351 | * @access protected |
||
352 | * |
||
353 | * @todo This feels too specific to be in the abstract sync Module class. Move it? |
||
354 | * |
||
355 | * @param array $ids Object IDs. |
||
356 | * @return array Term relationships - object ID and term taxonomy ID pairs. |
||
357 | */ |
||
358 | protected function get_term_relationships( $ids ) { |
||
364 | |||
365 | /** |
||
366 | * Unserialize the value of a meta object, if necessary. |
||
367 | * |
||
368 | * @access public |
||
369 | * |
||
370 | * @param object $meta Meta object. |
||
371 | * @return object Meta object with possibly unserialized value. |
||
372 | */ |
||
373 | public function unserialize_meta( $meta ) { |
||
377 | |||
378 | /** |
||
379 | * Retrieve a set of objects by their IDs. |
||
380 | * |
||
381 | * @access public |
||
382 | * |
||
383 | * @param string $object_type Object type. |
||
384 | * @param array $ids Object IDs. |
||
385 | * @return array Array of objects. |
||
386 | */ |
||
387 | public function get_objects_by_id( $object_type, $ids ) { |
||
404 | |||
405 | /** |
||
406 | * Gets a list of minimum and maximum object ids for each batch based on the given batch size. |
||
407 | * |
||
408 | * @access public |
||
409 | * |
||
410 | * @param int $batch_size The batch size for objects. |
||
411 | * @param string|bool $where_sql The sql where clause minus 'WHERE', or false if no where clause is needed. |
||
412 | * |
||
413 | * @return array|bool An array of min and max ids for each batch. FALSE if no table can be found. |
||
414 | */ |
||
415 | public function get_min_max_object_ids_for_batches( $batch_size, $where_sql = false ) { |
||
463 | } |
||
464 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.