Complex classes like MslsPlugin 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 MslsPlugin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class MslsPlugin { |
||
16 | |||
17 | /** |
||
18 | * Injected MslsOptions object |
||
19 | * |
||
20 | * @var MslsOptions |
||
21 | */ |
||
22 | protected $options; |
||
23 | |||
24 | /** |
||
25 | * MslsPlugin constructor. |
||
26 | * |
||
27 | * @param MslsOptions $options |
||
28 | */ |
||
29 | public function __construct( MslsOptions $options ) { |
||
32 | |||
33 | /** |
||
34 | * Factory |
||
35 | * |
||
36 | * @codeCoverageIgnore |
||
37 | * |
||
38 | * @return MslsPlugin |
||
39 | */ |
||
40 | public static function init() { |
||
99 | |||
100 | /** |
||
101 | * Gets MslsOutput object |
||
102 | * |
||
103 | * @return MslsOutput |
||
104 | */ |
||
105 | public static function get_output() { |
||
114 | |||
115 | /** |
||
116 | * @param $wp_admin_bar |
||
117 | */ |
||
118 | function update_adminbar( \WP_Admin_Bar $wp_admin_bar ) { |
||
133 | |||
134 | /** |
||
135 | * Callback for action wp_head |
||
136 | */ |
||
137 | public static function print_alternate_links() { |
||
140 | |||
141 | /** |
||
142 | * Filter for the_content() |
||
143 | * |
||
144 | * @param string $content |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | function content_filter( $content ) { |
||
159 | |||
160 | /** |
||
161 | * Create filterstring for msls_content_filter() |
||
162 | * |
||
163 | * @param string $pref |
||
164 | * @param string $post |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | function filter_string( $pref = '<p id="msls">', $post = '</p>' ) { |
||
206 | |||
207 | /** |
||
208 | * Register block and shortcode. |
||
209 | */ |
||
210 | public function block_init() { |
||
234 | |||
235 | /** |
||
236 | * Loads styles and some js if needed |
||
237 | * |
||
238 | * The method returns true if JS is loaded or false if not |
||
239 | * |
||
240 | * @return boolean |
||
241 | */ |
||
242 | public function admin_menu() { |
||
257 | |||
258 | /** |
||
259 | * Wrapper for plugins_url |
||
260 | * |
||
261 | * @param string $path |
||
262 | * |
||
263 | * @return string |
||
264 | */ |
||
265 | public static function plugins_url( string $path ): string { |
||
268 | |||
269 | /** |
||
270 | * Wrapper for plugin_dir_path |
||
271 | * |
||
272 | * @param string $path |
||
273 | * |
||
274 | * @return string |
||
275 | */ |
||
276 | public static function plugin_dir_path( string $path ): string { |
||
279 | |||
280 | /** |
||
281 | * @param string $path |
||
282 | * |
||
283 | * @return string |
||
284 | */ |
||
285 | public static function dirname( string $path ): string { |
||
288 | |||
289 | /** |
||
290 | * @return string |
||
291 | */ |
||
292 | public static function file(): string { |
||
295 | |||
296 | /** |
||
297 | * @return string |
||
298 | */ |
||
299 | public static function path(): string { |
||
302 | |||
303 | /** |
||
304 | * Register widget |
||
305 | * |
||
306 | * The widget will only be registered if the current blog is not |
||
307 | * excluded in the configuration of the plugin. |
||
308 | * @return boolean |
||
309 | */ |
||
310 | public function init_widget() { |
||
319 | |||
320 | /** |
||
321 | * Render widget output |
||
322 | * |
||
323 | * @return string |
||
324 | */ |
||
325 | public function block_render() { |
||
336 | |||
337 | /** |
||
338 | * Load textdomain |
||
339 | * |
||
340 | * The method should be executed always on init because we have some |
||
341 | * translatable string in the frontend too. |
||
342 | * |
||
343 | * @return boolean |
||
344 | */ |
||
345 | public function init_i18n_support() { |
||
348 | |||
349 | /** |
||
350 | * Message handler |
||
351 | * |
||
352 | * Prints a message box to the screen. |
||
353 | * |
||
354 | * @param string $message |
||
355 | * @param string $css_class |
||
356 | * |
||
357 | * @return boolean |
||
358 | */ |
||
359 | public static function message_handler( $message, $css_class = 'error' ) { |
||
368 | |||
369 | /** |
||
370 | * Activate plugin |
||
371 | */ |
||
372 | public static function activate() { |
||
375 | |||
376 | /** |
||
377 | * Uninstall plugin |
||
378 | * |
||
379 | * The plugin data in all blogs of the current network will be |
||
380 | * deleted after the uninstall procedure. |
||
381 | * |
||
382 | * @return boolean |
||
383 | */ |
||
384 | public static function uninstall() { |
||
410 | |||
411 | /** |
||
412 | * Cleanup the options |
||
413 | * |
||
414 | * Removes all values of the current blogs which are stored in the |
||
415 | * options-table and returns true if it was successful. |
||
416 | * |
||
417 | * @return boolean |
||
418 | */ |
||
419 | public static function cleanup() { |
||
432 | |||
433 | /** |
||
434 | * Get specific vars from $_POST and $_GET in a safe way |
||
435 | * |
||
436 | * @param array $list |
||
437 | * |
||
438 | * @return array |
||
439 | */ |
||
440 | public function get_superglobals( array $list ) { |
||
455 | |||
456 | } |
||
457 |