Complex classes like Auto_Load_Next_Post 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 Auto_Load_Next_Post, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class Auto_Load_Next_Post { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var Auto_Load_Next_Post - The single instance of the class |
||
| 36 | * |
||
| 37 | * @access protected |
||
| 38 | * @static |
||
| 39 | * @since 1.0.0 |
||
| 40 | */ |
||
| 41 | protected static $_instance = null; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Plugin Version |
||
| 45 | * |
||
| 46 | * @access public |
||
| 47 | * @static |
||
| 48 | * @since 1.5.0 |
||
| 49 | */ |
||
| 50 | public static $version = '1.5.0-beta.1'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Main Auto Load Next Post Instance |
||
| 54 | * |
||
| 55 | * Ensures only one instance of Auto Load Next Post is loaded or can be loaded. |
||
| 56 | * |
||
| 57 | * @access public |
||
| 58 | * @since 1.0.0 |
||
| 59 | * @static |
||
| 60 | * @see Auto_Load_Next_Post() |
||
| 61 | * @return Auto_Load_Next_Post Single instance. |
||
| 62 | */ |
||
| 63 | public static function instance() { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Cloning is forbidden. |
||
| 72 | * |
||
| 73 | * @access public |
||
| 74 | * @since 1.0.0 |
||
| 75 | * @return void |
||
| 76 | */ |
||
| 77 | public function __clone() { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Unserializing instances of this class is forbidden. |
||
| 84 | * |
||
| 85 | * @access public |
||
| 86 | * @since 1.0.0 |
||
| 87 | * @return void |
||
| 88 | */ |
||
| 89 | public function __wakeup() { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Auto_Load_Next_Post Constructor |
||
| 95 | * |
||
| 96 | * @access public |
||
| 97 | * @since 1.0.0 |
||
| 98 | * @version 1.5.0 |
||
| 99 | * @return Auto_Load_Next_Post |
||
|
|
|||
| 100 | */ |
||
| 101 | public function __construct() { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Hooks into action hooks. |
||
| 114 | * |
||
| 115 | * @access public |
||
| 116 | * @since 1.5.0 |
||
| 117 | */ |
||
| 118 | public function init_hooks() { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Setup Constants |
||
| 128 | * |
||
| 129 | * @since 1.4.3 |
||
| 130 | * @version 1.5.0 |
||
| 131 | * @access private |
||
| 132 | */ |
||
| 133 | private function setup_constants() { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Define constant if not already set. |
||
| 153 | * |
||
| 154 | * @param string $name |
||
| 155 | * @param string|bool $value |
||
| 156 | * @access private |
||
| 157 | * @since 1.4.3 |
||
| 158 | */ |
||
| 159 | private function define( $name, $value ) { |
||
| 164 | |||
| 165 | /*-----------------------------------------------------------------------------------*/ |
||
| 166 | /* Load Files */ |
||
| 167 | /*-----------------------------------------------------------------------------------*/ |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Include required core files used in admin and on the frontend. |
||
| 171 | * |
||
| 172 | * @access public |
||
| 173 | * @since 1.0.0 |
||
| 174 | * @version 1.5.0 |
||
| 175 | * @return void |
||
| 176 | */ |
||
| 177 | public function includes() { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Include classes for theme support. |
||
| 201 | * |
||
| 202 | * @access public |
||
| 203 | * @since 1.5.0 |
||
| 204 | */ |
||
| 205 | public function alnp_include_theme_support() { |
||
| 248 | |||
| 249 | /*-----------------------------------------------------------------------------------*/ |
||
| 250 | /* Localization */ |
||
| 251 | /*-----------------------------------------------------------------------------------*/ |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Make the plugin translation ready. |
||
| 255 | * |
||
| 256 | * Translations should be added in the WordPress language directory: |
||
| 257 | * - WP_LANG_DIR/plugins/auto-load-next-post-LOCALE.mo |
||
| 258 | * |
||
| 259 | * @access public |
||
| 260 | * @since 1.0.0 |
||
| 261 | * @version 1.4.10 |
||
| 262 | * @return void |
||
| 263 | */ |
||
| 264 | public function load_plugin_textdomain() { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Registers and enqueues stylesheets and javascripts for the front of the site. |
||
| 270 | * |
||
| 271 | * @access public |
||
| 272 | * @since 1.3.2 |
||
| 273 | * @version 1.5.0 |
||
| 274 | */ |
||
| 275 | public function alnp_enqueue_scripts() { |
||
| 305 | |||
| 306 | /*-----------------------------------------------------------------------------------*/ |
||
| 307 | /* Helper Functions */ |
||
| 308 | /*-----------------------------------------------------------------------------------*/ |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Checks if we are using the theme customizer. |
||
| 312 | * |
||
| 313 | * @access public |
||
| 314 | * @since 1.5.0 |
||
| 315 | * @static |
||
| 316 | * @return string|boolean |
||
| 317 | */ |
||
| 318 | public static function is_alnp_using_customizer() { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Helper function for registering and enqueueing scripts and styles. |
||
| 328 | * |
||
| 329 | * @access public |
||
| 330 | * @since 1.0.0 |
||
| 331 | * @version 1.5.0 |
||
| 332 | * @static |
||
| 333 | * @param string $name The ID to register with WordPress. |
||
| 334 | * @param string $file_path The path to the actual file. |
||
| 335 | * @param bool $is_script Optional, argument for if the incoming file_path is a JavaScript source file. |
||
| 336 | * @param array $support Optional, for requiring other javascripts for the source file you are calling. |
||
| 337 | * @param string $version Optional, can match the version of the plugin or version of the source file. |
||
| 338 | * @param bool $footer Optional, can set the JavaScript to load in the footer instead. |
||
| 339 | * @global string $wp_version |
||
| 340 | */ |
||
| 341 | public static function load_file( $name, $file_path, $is_script = false, $support = array(), $version = '', $footer = false ) { |
||
| 361 | |||
| 362 | } // END Auto_Load_Next_Post() |
||
| 363 | |||
| 367 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.