Complex classes like Classy 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 Classy, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Classy { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Singleton instance of plugin |
||
| 21 | * |
||
| 22 | * @var Classy |
||
| 23 | * @since 0.1.0 |
||
| 24 | */ |
||
| 25 | protected static $single_instance = null; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Creates or returns an instance of this class. |
||
| 29 | * |
||
| 30 | * @since 0.1.0 |
||
| 31 | * @return Classy A single instance of this class. |
||
| 32 | */ |
||
| 33 | public static function get_instance() { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Define the core functionality of the them. |
||
| 47 | * |
||
| 48 | * Set the theme name and the theme version that can be used throughout the theme. |
||
| 49 | * |
||
| 50 | * @since 1.0.0 |
||
| 51 | */ |
||
| 52 | protected function __construct() { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Init Appearance class. |
||
| 68 | */ |
||
| 69 | private function init_appearance() { |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Load template functions. |
||
| 75 | */ |
||
| 76 | private function load_template_function() { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Loads custom files specified in custom/config.php. |
||
| 82 | */ |
||
| 83 | private function load_custom_includes() { |
||
| 84 | $include = self::get_config_var( 'include' ); |
||
| 85 | |||
| 86 | if ( is_array( $include ) ) { |
||
| 87 | foreach ( $include as $file ) { |
||
| 88 | $files = (array) glob( CLASSY_THEME_FRAMEWORK_PATH . 'custom/' . $file ); |
||
| 89 | foreach ( $files as $filename ) { |
||
| 90 | if ( is_readable( $filename ) ) { |
||
| 91 | require_once $filename; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Defines plugin constants |
||
| 100 | * |
||
| 101 | * @since 1.0.0 |
||
| 102 | * @access private |
||
| 103 | */ |
||
| 104 | private function define_constants() { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Init Theme Configuration |
||
| 120 | */ |
||
| 121 | private function init_config() { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Filters registered templates and adds custom theme templates. |
||
| 127 | * |
||
| 128 | * @param array $page_templates Available WordPress templates. |
||
| 129 | * |
||
| 130 | * @return array |
||
| 131 | */ |
||
| 132 | public function filter_templates( $page_templates = array() ) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Returns theme config variable. |
||
| 142 | * |
||
| 143 | * @param string $name Variable's name. |
||
| 144 | * |
||
| 145 | * @return mixed|bool Return false if variable not found. |
||
| 146 | */ |
||
| 147 | public static function get_config_var( $name ) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Returns theme textdomain |
||
| 157 | * |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | public static function textdomain() { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Performs view render. |
||
| 170 | * If there is $view attribute presented, it will render requested view. |
||
| 171 | * If it's not it will try to find necessary view based on $wp_query |
||
| 172 | * |
||
| 173 | * @param string|null $view View path in blade format, ex: single, layout.default, single.partials.slider and etc. |
||
| 174 | * @param array|null $data Additional params. |
||
| 175 | * @return void |
||
| 176 | */ |
||
| 177 | public static function render( $view = null, $data = null ) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Minifies html in case the minify_html option is set to true. |
||
| 212 | * |
||
| 213 | * @param string $html HTML string. |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | private static function maybe_minify( $html ) { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Returns minified version of string with removed whitespaces and empty strings. |
||
| 232 | * |
||
| 233 | * @param string $html HTML string. |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | private static function minify_html( $html ) { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Alias for Helper::get_archives_title() |
||
| 260 | * Returns page title for archive page. |
||
| 261 | * Example: Archives, Author: John Doe, Tag: Lorem Ipsum |
||
| 262 | * |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public static function archives_title() { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Returns posts |
||
| 273 | * |
||
| 274 | * @param mixed $args Array of query args. |
||
| 275 | * @param string $return object/id/Post. |
||
| 276 | * |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | public static function get_posts( $args = false, $return = '\Classy\Models\Post' ) { |
||
| 307 | |||
| 308 | |||
| 309 | /** |
||
| 310 | * Returns post. |
||
| 311 | * |
||
| 312 | * @param mixed $args Array of query args. |
||
| 313 | * @param string $return_type Post/object/id. |
||
| 314 | * |
||
| 315 | * @return mixed |
||
| 316 | */ |
||
| 317 | public static function get_post( $args = false, $return_type = '\Classy\Models\Post' ) { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @todo: Write description here. |
||
| 329 | * |
||
| 330 | * @param array $prefs Args for paginate_links. |
||
| 331 | * |
||
| 332 | * @return array mixed |
||
| 333 | */ |
||
| 334 | public static function get_pagination( $prefs = array() ) { |
||
| 392 | } |
||
| 393 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: