Complex classes like Timber 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 Timber, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Timber { |
||
| 19 | |||
| 20 | public static $locations; |
||
| 21 | public static $dirname; |
||
| 22 | public static $twig_cache = false; |
||
| 23 | public static $cache = false; |
||
| 24 | public static $auto_meta = true; |
||
| 25 | public static $autoescape = false; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @codeCoverageIgnore |
||
| 29 | */ |
||
| 30 | public function __construct() { |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Tests whether we can use Timber |
||
| 41 | * @codeCoverageIgnore |
||
| 42 | * @return |
||
| 43 | */ |
||
| 44 | protected function test_compatibility() { |
||
| 55 | |||
| 56 | function init_constants() { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @codeCoverageIgnore |
||
| 62 | */ |
||
| 63 | protected function init() { |
||
| 70 | |||
| 71 | /* Post Retrieval Routine |
||
| 72 | ================================ */ |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Get post. |
||
| 76 | * |
||
| 77 | * @param mixed $query |
||
| 78 | * @param string $PostClass |
||
| 79 | * @return array|bool|null |
||
| 80 | */ |
||
| 81 | public static function get_post( $query = false, $PostClass = 'TimberPost' ) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get posts. |
||
| 87 | * |
||
| 88 | * @param mixed $query |
||
| 89 | * @param string $PostClass |
||
| 90 | * @return array|bool|null |
||
| 91 | */ |
||
| 92 | public static function get_posts( $query = false, $PostClass = 'TimberPost', $return_collection = false ) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Query post. |
||
| 98 | * |
||
| 99 | * @param mixed $query |
||
| 100 | * @param string $PostClass |
||
| 101 | * @return array|bool|null |
||
| 102 | */ |
||
| 103 | public static function query_post( $query = false, $PostClass = 'TimberPost' ) { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Query posts. |
||
| 109 | * |
||
| 110 | * @param mixed $query |
||
| 111 | * @param string $PostClass |
||
| 112 | * @return array|bool|null |
||
| 113 | */ |
||
| 114 | public static function query_posts( $query = false, $PostClass = 'TimberPost' ) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get pids. |
||
| 120 | * |
||
| 121 | * @param array|string $query |
||
| 122 | * @return array |
||
| 123 | * @deprecated since 0.20.0 |
||
| 124 | */ |
||
| 125 | static function get_pids( $query = null ) { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get posts from loop. |
||
| 131 | * |
||
| 132 | * @param string $PostClass |
||
| 133 | * @return array |
||
| 134 | * @deprecated since 0.20.0 |
||
| 135 | */ |
||
| 136 | static function get_posts_from_loop( $PostClass ) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Get posts from slug. |
||
| 142 | * |
||
| 143 | * @param string $slug |
||
| 144 | * @param string $PostClass |
||
| 145 | * @return array |
||
| 146 | * @deprecated since 0.20.0 |
||
| 147 | */ |
||
| 148 | static function get_posts_from_slug( $slug, $PostClass = 'TimberPost' ) { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get posts from WP_Query. |
||
| 154 | * |
||
| 155 | * @param array $query |
||
| 156 | * @param string $PostClass |
||
| 157 | * @return array |
||
| 158 | * @deprecated since 0.20.0 |
||
| 159 | */ |
||
| 160 | static function get_posts_from_wp_query( $query = array(), $PostClass = 'TimberPost' ) { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get posts from array of ids. |
||
| 166 | * |
||
| 167 | * @param array $query |
||
| 168 | * @param string $PostClass |
||
| 169 | * @return array|null |
||
| 170 | * @deprecated since 0.20.0 |
||
| 171 | */ |
||
| 172 | static function get_posts_from_array_of_ids( $query = array(), $PostClass = 'TimberPost' ) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get pid. |
||
| 178 | * |
||
| 179 | * @param unknown $query |
||
| 180 | * @return int |
||
| 181 | * @deprecated since 0.20.0 |
||
| 182 | */ |
||
| 183 | static function get_pid( $query ) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * WP_Query has posts. |
||
| 192 | * |
||
| 193 | * @return bool |
||
| 194 | * @deprecated since 0.20.0 |
||
| 195 | */ |
||
| 196 | static function wp_query_has_posts() { |
||
| 199 | |||
| 200 | /* Term Retrieval |
||
| 201 | ================================ */ |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Get terms. |
||
| 205 | * |
||
| 206 | * @param string|array $args |
||
| 207 | * @param array $maybe_args |
||
| 208 | * @param string $TermClass |
||
| 209 | * @return mixed |
||
| 210 | */ |
||
| 211 | public static function get_terms( $args = null, $maybe_args = array(), $TermClass = 'TimberTerm' ) { |
||
| 214 | |||
| 215 | /* Site Retrieval |
||
| 216 | ================================ */ |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get sites. |
||
| 220 | * |
||
| 221 | * @param array|bool $blog_ids |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | public static function get_sites( $blog_ids = false ) { |
||
| 235 | |||
| 236 | |||
| 237 | /* Template Setup and Display |
||
| 238 | ================================ */ |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Get context. |
||
| 242 | * |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | public static function get_context() { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Compile function. |
||
| 277 | * |
||
| 278 | * @param array $filenames |
||
| 279 | * @param array $data |
||
| 280 | * @param bool $expires |
||
| 281 | * @param string $cache_mode |
||
| 282 | * @param bool $via_render |
||
| 283 | * @return bool|string |
||
| 284 | */ |
||
| 285 | public static function compile( $filenames, $data = array(), $expires = false, $cache_mode = TimberLoader::CACHE_USE_DEFAULT, $via_render = false ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Compile string. |
||
| 311 | * |
||
| 312 | * @param string $string a string with twig variables. |
||
| 313 | * @param array $data an array with data in it. |
||
| 314 | * @return bool|string |
||
| 315 | */ |
||
| 316 | public static function compile_string( $string, $data = array() ) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Fetch function. |
||
| 328 | * |
||
| 329 | * @param array $filenames |
||
| 330 | * @param array $data |
||
| 331 | * @param bool $expires |
||
| 332 | * @param string $cache_mode |
||
| 333 | * @return bool|string |
||
| 334 | */ |
||
| 335 | public static function fetch( $filenames, $data = array(), $expires = false, $cache_mode = TimberLoader::CACHE_USE_DEFAULT ) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Render function. |
||
| 349 | * |
||
| 350 | * @param array $filenames |
||
| 351 | * @param array $data |
||
| 352 | * @param bool $expires |
||
| 353 | * @param string $cache_mode |
||
| 354 | * @return bool|string |
||
| 355 | */ |
||
| 356 | public static function render( $filenames, $data = array(), $expires = false, $cache_mode = TimberLoader::CACHE_USE_DEFAULT ) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Render string. |
||
| 364 | * |
||
| 365 | * @param string $string a string with twig variables. |
||
| 366 | * @param array $data an array with data in it. |
||
| 367 | * @return bool|string |
||
| 368 | */ |
||
| 369 | public static function render_string( $string, $data = array() ) { |
||
| 374 | |||
| 375 | |||
| 376 | /* Sidebar |
||
| 377 | ================================ */ |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Get sidebar. |
||
| 381 | * |
||
| 382 | * @param string $sidebar |
||
| 383 | * @param array $data |
||
| 384 | * @return bool|string |
||
| 385 | */ |
||
| 386 | public static function get_sidebar( $sidebar = '', $data = array() ) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Get sidebar from PHP |
||
| 398 | * |
||
| 399 | * @param string $sidebar |
||
| 400 | * @param array $data |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | public static function get_sidebar_from_php( $sidebar = '', $data ) { |
||
| 423 | |||
| 424 | /* Widgets |
||
| 425 | ================================ */ |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Get widgets. |
||
| 429 | * |
||
| 430 | * @param int $widget_id |
||
| 431 | * @return TimberFunctionWrapper |
||
| 432 | */ |
||
| 433 | public static function get_widgets( $widget_id ) { |
||
| 436 | |||
| 437 | |||
| 438 | /* Routes |
||
| 439 | ================================ */ |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Add route. |
||
| 443 | * |
||
| 444 | * @param string $route |
||
| 445 | * @param callable $callback |
||
| 446 | * @param array $args |
||
| 447 | * @deprecated since 0.20.0 |
||
| 448 | */ |
||
| 449 | public static function add_route( $route, $callback, $args = array() ) { |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @deprecated since 0.22.2 |
||
| 455 | */ |
||
| 456 | public function cancel_query() { |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @deprecated since 0.22.2 |
||
| 462 | */ |
||
| 463 | function cancel_query_posts_request() { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Load template. |
||
| 471 | * |
||
| 472 | * @deprecated since 0.20.0 |
||
| 473 | */ |
||
| 474 | public static function load_template( $template, $query = false, $status_code = 200, $tparams = false ) { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Load view. |
||
| 480 | * |
||
| 481 | * @deprecated since 0.20.2 |
||
| 482 | */ |
||
| 483 | public static function load_view( $template, $query = false, $status_code = 200, $tparams = false ) { |
||
| 486 | |||
| 487 | |||
| 488 | /* Pagination |
||
| 489 | ================================ */ |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Get pagination. |
||
| 493 | * |
||
| 494 | * @param array $prefs |
||
| 495 | * @return array mixed |
||
| 496 | */ |
||
| 497 | public static function get_pagination( $prefs = array() ) { |
||
| 540 | |||
| 541 | /* Utility |
||
| 542 | ================================ */ |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Get calling script path. |
||
| 546 | * |
||
| 547 | * @param int $offset |
||
| 548 | * @return string |
||
| 549 | * @deprecated since 0.20.0 |
||
| 550 | */ |
||
| 551 | public static function get_calling_script_path( $offset = 0 ) { |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Get calling script dir. |
||
| 558 | * |
||
| 559 | * @return string |
||
| 560 | */ |
||
| 561 | public static function get_calling_script_dir( $offset = 0 ) { |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Get calling script file. |
||
| 572 | * |
||
| 573 | * @param int $offset |
||
| 574 | * @return string|null |
||
| 575 | * @deprecated since 0.20.0 |
||
| 576 | */ |
||
| 577 | public static function get_calling_script_file( $offset = 0 ) { |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Is post class or class map. |
||
| 596 | * |
||
| 597 | * @param string|array $args |
||
| 598 | * @return bool |
||
| 599 | * @deprecated since 0.20.0 |
||
| 600 | */ |
||
| 601 | public static function is_post_class_or_class_map( $args ) { |
||
| 604 | |||
| 605 | } |
||
| 606 |