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() { |
||
102 | |||
103 | /** |
||
104 | * Gets MslsOutput object |
||
105 | * |
||
106 | * @return MslsOutput |
||
107 | */ |
||
108 | public static function get_output() { |
||
117 | |||
118 | /** |
||
119 | * @param $wp_admin_bar |
||
120 | */ |
||
121 | public static function update_adminbar( \WP_Admin_Bar $wp_admin_bar ) { |
||
134 | |||
135 | /** |
||
136 | * Callback for action wp_head |
||
137 | */ |
||
138 | public static function print_alternate_links() { |
||
141 | |||
142 | /** |
||
143 | * Filter for the_content() |
||
144 | * |
||
145 | * @param string $content |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | function content_filter( $content ) { |
||
160 | |||
161 | /** |
||
162 | * Create filterstring for msls_content_filter() |
||
163 | * |
||
164 | * @param string $pref |
||
165 | * @param string $post |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | function filter_string( $pref = '<p id="msls">', $post = '</p>' ) { |
||
207 | |||
208 | /** |
||
209 | * Register block and shortcode. |
||
210 | */ |
||
211 | public function block_init() { |
||
235 | |||
236 | /** |
||
237 | * Loads styles and some js if needed |
||
238 | * |
||
239 | * The method returns true if JS is loaded or false if not |
||
240 | * |
||
241 | * @return boolean |
||
242 | */ |
||
243 | public function admin_menu() { |
||
258 | |||
259 | /** |
||
260 | * Wrapper for plugins_url |
||
261 | * |
||
262 | * @param string $path |
||
263 | * |
||
264 | * @return string |
||
265 | */ |
||
266 | public static function plugins_url( string $path ): string { |
||
269 | |||
270 | /** |
||
271 | * Wrapper for plugin_dir_path |
||
272 | * |
||
273 | * @param string $path |
||
274 | * |
||
275 | * @return string |
||
276 | */ |
||
277 | public static function plugin_dir_path( string $path ): string { |
||
280 | |||
281 | /** |
||
282 | * @param string $path |
||
283 | * |
||
284 | * @return string |
||
285 | */ |
||
286 | public static function dirname( string $path ): string { |
||
289 | |||
290 | /** |
||
291 | * @return string |
||
292 | */ |
||
293 | public static function file(): string { |
||
296 | |||
297 | /** |
||
298 | * @return string |
||
299 | */ |
||
300 | public static function path(): string { |
||
303 | |||
304 | /** |
||
305 | * Register widget |
||
306 | * |
||
307 | * The widget will only be registered if the current blog is not |
||
308 | * excluded in the configuration of the plugin. |
||
309 | * @return boolean |
||
310 | */ |
||
311 | public function init_widget() { |
||
320 | |||
321 | /** |
||
322 | * Render widget output |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | public function block_render() { |
||
337 | |||
338 | /** |
||
339 | * Load textdomain |
||
340 | * |
||
341 | * The method should be executed always on init because we have some |
||
342 | * translatable string in the frontend too. |
||
343 | * |
||
344 | * @return boolean |
||
345 | */ |
||
346 | public function init_i18n_support() { |
||
349 | |||
350 | /** |
||
351 | * Message handler |
||
352 | * |
||
353 | * Prints a message box to the screen. |
||
354 | * |
||
355 | * @param string $message |
||
356 | * @param string $css_class |
||
357 | * |
||
358 | * @return boolean |
||
359 | */ |
||
360 | public static function message_handler( $message, $css_class = 'error' ) { |
||
369 | |||
370 | /** |
||
371 | * Activate plugin |
||
372 | */ |
||
373 | public static function activate() { |
||
376 | |||
377 | /** |
||
378 | * Uninstall plugin |
||
379 | * |
||
380 | * The plugin data in all blogs of the current network will be |
||
381 | * deleted after the uninstall procedure. |
||
382 | * |
||
383 | * @return boolean |
||
384 | */ |
||
385 | public static function uninstall() { |
||
411 | |||
412 | /** |
||
413 | * Cleanup the options |
||
414 | * |
||
415 | * Removes all values of the current blogs which are stored in the |
||
416 | * options-table and returns true if it was successful. |
||
417 | * |
||
418 | * @return boolean |
||
419 | */ |
||
420 | public static function cleanup() { |
||
433 | |||
434 | /** |
||
435 | * Get specific vars from $_POST and $_GET in a safe way |
||
436 | * |
||
437 | * @param array $list |
||
438 | * |
||
439 | * @return array |
||
440 | */ |
||
441 | public function get_superglobals( array $list ) { |
||
456 | |||
457 | } |
||
458 |