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 | public static function update_adminbar( \WP_Admin_Bar $wp_admin_bar ) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Callback for action wp_head |
||
| 132 | */ |
||
| 133 | public static function print_alternate_links() { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Filter for the_content() |
||
| 139 | * |
||
| 140 | * @param string $content |
||
| 141 | * |
||
| 142 | * @return string |
||
| 143 | */ |
||
| 144 | function content_filter( $content ) { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Create filterstring for msls_content_filter() |
||
| 158 | * |
||
| 159 | * @param string $pref |
||
| 160 | * @param string $post |
||
| 161 | * |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | function filter_string( $pref = '<p id="msls">', $post = '</p>' ) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Register block and shortcode. |
||
| 205 | */ |
||
| 206 | public function block_init() { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Loads styles and some js if needed |
||
| 233 | * |
||
| 234 | * The method returns true if JS is loaded or false if not |
||
| 235 | * |
||
| 236 | * @return boolean |
||
| 237 | */ |
||
| 238 | public function admin_menu() { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Wrapper for plugins_url |
||
| 256 | * |
||
| 257 | * @param string $path |
||
| 258 | * |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | public static function plugins_url( string $path ): string { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Wrapper for plugin_dir_path |
||
| 267 | * |
||
| 268 | * @param string $path |
||
| 269 | * |
||
| 270 | * @return string |
||
| 271 | */ |
||
| 272 | public static function plugin_dir_path( string $path ): string { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param string $path |
||
| 278 | * |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | public static function dirname( string $path ): string { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public static function file(): string { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | public static function path(): string { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Register widget |
||
| 301 | * |
||
| 302 | * The widget will only be registered if the current blog is not |
||
| 303 | * excluded in the configuration of the plugin. |
||
| 304 | * @return boolean |
||
| 305 | */ |
||
| 306 | public function init_widget() { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Render widget output |
||
| 318 | * |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | public function block_render() { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Load textdomain |
||
| 335 | * |
||
| 336 | * The method should be executed always on init because we have some |
||
| 337 | * translatable string in the frontend too. |
||
| 338 | * |
||
| 339 | * @return boolean |
||
| 340 | */ |
||
| 341 | public function init_i18n_support() { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Message handler |
||
| 347 | * |
||
| 348 | * Prints a message box to the screen. |
||
| 349 | * |
||
| 350 | * @param string $message |
||
| 351 | * @param string $css_class |
||
| 352 | * |
||
| 353 | * @return boolean |
||
| 354 | */ |
||
| 355 | public static function message_handler( $message, $css_class = 'error' ) { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Activate plugin |
||
| 367 | */ |
||
| 368 | public static function activate() { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Uninstall plugin |
||
| 374 | * |
||
| 375 | * The plugin data in all blogs of the current network will be |
||
| 376 | * deleted after the uninstall procedure. |
||
| 377 | * |
||
| 378 | * @return boolean |
||
| 379 | */ |
||
| 380 | public static function uninstall() { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Cleanup the options |
||
| 409 | * |
||
| 410 | * Removes all values of the current blogs which are stored in the |
||
| 411 | * options-table and returns true if it was successful. |
||
| 412 | * |
||
| 413 | * @return boolean |
||
| 414 | */ |
||
| 415 | public static function cleanup() { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Get specific vars from $_POST and $_GET in a safe way |
||
| 431 | * |
||
| 432 | * @param array $list |
||
| 433 | * |
||
| 434 | * @return array |
||
| 435 | */ |
||
| 436 | public function get_superglobals( array $list ) { |
||
| 451 | |||
| 452 | } |
||
| 453 |