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