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 |
||
44 | class Timber { |
||
45 | |||
46 | public static $locations; |
||
47 | public static $dirname; |
||
48 | public static $twig_cache = false; |
||
49 | public static $cache = false; |
||
50 | public static $auto_meta = true; |
||
51 | public static $autoescape = false; |
||
52 | |||
53 | /** |
||
54 | * @codeCoverageIgnore |
||
55 | */ |
||
56 | public function __construct() { |
||
64 | |||
65 | /** |
||
66 | * Tests whether we can use Timber |
||
67 | * @codeCoverageIgnore |
||
68 | * @return |
||
69 | */ |
||
70 | protected function test_compatibility() { |
||
81 | |||
82 | function init_constants() { |
||
85 | |||
86 | /** |
||
87 | * @codeCoverageIgnore |
||
88 | */ |
||
89 | protected function init() { |
||
96 | |||
97 | /* Post Retrieval Routine |
||
98 | ================================ */ |
||
99 | |||
100 | /** |
||
101 | * Get post. |
||
102 | * |
||
103 | * @param mixed $query |
||
104 | * @param string $PostClass |
||
105 | * @return array|bool|null |
||
106 | */ |
||
107 | public static function get_post( $query = false, $PostClass = 'TimberPost' ) { |
||
110 | |||
111 | /** |
||
112 | * Get posts. |
||
113 | * |
||
114 | * @param mixed $query |
||
115 | * @param string $PostClass |
||
116 | * @return array|bool|null |
||
117 | */ |
||
118 | public static function get_posts( $query = false, $PostClass = 'TimberPost', $return_collection = false ) { |
||
121 | |||
122 | /** |
||
123 | * Query post. |
||
124 | * |
||
125 | * @param mixed $query |
||
126 | * @param string $PostClass |
||
127 | * @return array|bool|null |
||
128 | */ |
||
129 | public static function query_post( $query = false, $PostClass = 'TimberPost' ) { |
||
132 | |||
133 | /** |
||
134 | * Query posts. |
||
135 | * |
||
136 | * @param mixed $query |
||
137 | * @param string $PostClass |
||
138 | * @return array|bool|null |
||
139 | */ |
||
140 | public static function query_posts( $query = false, $PostClass = 'TimberPost' ) { |
||
143 | |||
144 | /** |
||
145 | * Get pids. |
||
146 | * |
||
147 | * @param array|string $query |
||
148 | * @return array |
||
149 | * @deprecated since 0.20.0 |
||
150 | */ |
||
151 | static function get_pids( $query = null ) { |
||
154 | |||
155 | /** |
||
156 | * Get posts from loop. |
||
157 | * |
||
158 | * @param string $PostClass |
||
159 | * @return array |
||
160 | * @deprecated since 0.20.0 |
||
161 | */ |
||
162 | static function get_posts_from_loop( $PostClass ) { |
||
165 | |||
166 | /** |
||
167 | * Get posts from slug. |
||
168 | * |
||
169 | * @param string $slug |
||
170 | * @param string $PostClass |
||
171 | * @return array |
||
172 | * @deprecated since 0.20.0 |
||
173 | */ |
||
174 | static function get_posts_from_slug( $slug, $PostClass = 'TimberPost' ) { |
||
177 | |||
178 | /** |
||
179 | * Get posts from WP_Query. |
||
180 | * |
||
181 | * @param array $query |
||
182 | * @param string $PostClass |
||
183 | * @return array |
||
184 | * @deprecated since 0.20.0 |
||
185 | */ |
||
186 | static function get_posts_from_wp_query( $query = array(), $PostClass = 'TimberPost' ) { |
||
189 | |||
190 | /** |
||
191 | * Get posts from array of ids. |
||
192 | * |
||
193 | * @param array $query |
||
194 | * @param string $PostClass |
||
195 | * @return array|null |
||
196 | * @deprecated since 0.20.0 |
||
197 | */ |
||
198 | static function get_posts_from_array_of_ids( $query = array(), $PostClass = 'TimberPost' ) { |
||
201 | |||
202 | /** |
||
203 | * Get pid. |
||
204 | * |
||
205 | * @param unknown $query |
||
206 | * @return int |
||
207 | * @deprecated since 0.20.0 |
||
208 | */ |
||
209 | static function get_pid( $query ) { |
||
215 | |||
216 | /** |
||
217 | * WP_Query has posts. |
||
218 | * |
||
219 | * @return bool |
||
220 | * @deprecated since 0.20.0 |
||
221 | */ |
||
222 | static function wp_query_has_posts() { |
||
225 | |||
226 | /* Term Retrieval |
||
227 | ================================ */ |
||
228 | |||
229 | /** |
||
230 | * Get terms. |
||
231 | * |
||
232 | * @param string|array $args |
||
233 | * @param array $maybe_args |
||
234 | * @param string $TermClass |
||
235 | * @return mixed |
||
236 | */ |
||
237 | public static function get_terms( $args = null, $maybe_args = array(), $TermClass = 'TimberTerm' ) { |
||
240 | |||
241 | /* Site Retrieval |
||
242 | ================================ */ |
||
243 | |||
244 | /** |
||
245 | * Get sites. |
||
246 | * |
||
247 | * @param array|bool $blog_ids |
||
248 | * @return array |
||
249 | */ |
||
250 | public static function get_sites( $blog_ids = false ) { |
||
261 | |||
262 | |||
263 | /* Template Setup and Display |
||
264 | ================================ */ |
||
265 | |||
266 | /** |
||
267 | * Get context. |
||
268 | * |
||
269 | * @return array |
||
270 | */ |
||
271 | public static function get_context() { |
||
300 | |||
301 | /** |
||
302 | * Compile function. |
||
303 | * |
||
304 | * @param array $filenames |
||
305 | * @param array $data |
||
306 | * @param bool $expires |
||
307 | * @param string $cache_mode |
||
308 | * @param bool $via_render |
||
309 | * @return bool|string |
||
310 | */ |
||
311 | public static function compile( $filenames, $data = array(), $expires = false, $cache_mode = TimberLoader::CACHE_USE_DEFAULT, $via_render = false ) { |
||
334 | |||
335 | /** |
||
336 | * Compile string. |
||
337 | * |
||
338 | * @param string $string a string with twig variables. |
||
339 | * @param array $data an array with data in it. |
||
340 | * @return bool|string |
||
341 | */ |
||
342 | public static function compile_string( $string, $data = array() ) { |
||
351 | |||
352 | /** |
||
353 | * Fetch function. |
||
354 | * |
||
355 | * @param array $filenames |
||
356 | * @param array $data |
||
357 | * @param bool $expires |
||
358 | * @param string $cache_mode |
||
359 | * @return bool|string |
||
360 | */ |
||
361 | public static function fetch( $filenames, $data = array(), $expires = false, $cache_mode = TimberLoader::CACHE_USE_DEFAULT ) { |
||
372 | |||
373 | /** |
||
374 | * Render function. |
||
375 | * |
||
376 | * @param array $filenames |
||
377 | * @param array $data |
||
378 | * @param bool $expires |
||
379 | * @param string $cache_mode |
||
380 | * @return bool|string |
||
381 | */ |
||
382 | public static function render( $filenames, $data = array(), $expires = false, $cache_mode = TimberLoader::CACHE_USE_DEFAULT ) { |
||
387 | |||
388 | /** |
||
389 | * Render string. |
||
390 | * |
||
391 | * @param string $string a string with twig variables. |
||
392 | * @param array $data an array with data in it. |
||
393 | * @return bool|string |
||
394 | */ |
||
395 | public static function render_string( $string, $data = array() ) { |
||
400 | |||
401 | |||
402 | /* Sidebar |
||
403 | ================================ */ |
||
404 | |||
405 | /** |
||
406 | * Get sidebar. |
||
407 | * |
||
408 | * @param string $sidebar |
||
409 | * @param array $data |
||
410 | * @return bool|string |
||
411 | */ |
||
412 | public static function get_sidebar( $sidebar = '', $data = array() ) { |
||
421 | |||
422 | /** |
||
423 | * Get sidebar from PHP |
||
424 | * |
||
425 | * @param string $sidebar |
||
426 | * @param array $data |
||
427 | * @return string |
||
428 | */ |
||
429 | public static function get_sidebar_from_php( $sidebar = '', $data ) { |
||
449 | |||
450 | /* Widgets |
||
451 | ================================ */ |
||
452 | |||
453 | /** |
||
454 | * Get widgets. |
||
455 | * |
||
456 | * @param int $widget_id |
||
457 | * @return TimberFunctionWrapper |
||
458 | */ |
||
459 | public static function get_widgets( $widget_id ) { |
||
462 | |||
463 | |||
464 | /* Routes |
||
465 | ================================ */ |
||
466 | |||
467 | /** |
||
468 | * Add route. |
||
469 | * |
||
470 | * @param string $route |
||
471 | * @param callable $callback |
||
472 | * @param array $args |
||
473 | * @deprecated since 0.20.0 |
||
474 | */ |
||
475 | public static function add_route( $route, $callback, $args = array() ) { |
||
478 | |||
479 | /** |
||
480 | * @deprecated since 0.22.2 |
||
481 | */ |
||
482 | public function cancel_query() { |
||
485 | |||
486 | /** |
||
487 | * @deprecated since 0.22.2 |
||
488 | */ |
||
489 | function cancel_query_posts_request() { |
||
494 | |||
495 | /** |
||
496 | * Load template. |
||
497 | * |
||
498 | * @deprecated since 0.20.0 |
||
499 | */ |
||
500 | public static function load_template( $template, $query = false, $status_code = 200, $tparams = false ) { |
||
503 | |||
504 | /** |
||
505 | * Load view. |
||
506 | * |
||
507 | * @deprecated since 0.20.2 |
||
508 | */ |
||
509 | public static function load_view( $template, $query = false, $status_code = 200, $tparams = false ) { |
||
512 | |||
513 | |||
514 | /* Pagination |
||
515 | ================================ */ |
||
516 | |||
517 | /** |
||
518 | * Get pagination. |
||
519 | * |
||
520 | * @param array $prefs |
||
521 | * @return array mixed |
||
522 | */ |
||
523 | public static function get_pagination( $prefs = array() ) { |
||
566 | |||
567 | /* Utility |
||
568 | ================================ */ |
||
569 | |||
570 | /** |
||
571 | * Get calling script path. |
||
572 | * |
||
573 | * @param int $offset |
||
574 | * @return string |
||
575 | * @deprecated since 0.20.0 |
||
576 | */ |
||
577 | public static function get_calling_script_path( $offset = 0 ) { |
||
581 | |||
582 | /** |
||
583 | * Get calling script dir. |
||
584 | * |
||
585 | * @return string |
||
586 | */ |
||
587 | public static function get_calling_script_dir( $offset = 0 ) { |
||
595 | |||
596 | /** |
||
597 | * Get calling script file. |
||
598 | * |
||
599 | * @param int $offset |
||
600 | * @return string|null |
||
601 | * @deprecated since 0.20.0 |
||
602 | */ |
||
603 | public static function get_calling_script_file( $offset = 0 ) { |
||
604 | $caller = null; |
||
605 | $backtrace = debug_backtrace(); |
||
606 | $i = 0; |
||
607 | foreach ( $backtrace as $trace ) { |
||
608 | if ( array_key_exists('file', $trace) && $trace['file'] != __FILE__ ) { |
||
609 | $caller = $trace['file']; |
||
610 | break; |
||
611 | } |
||
612 | $i++; |
||
613 | } |
||
614 | if ( $offset ) { |
||
615 | $caller = $backtrace[$i + $offset]['file']; |
||
616 | } |
||
617 | return $caller; |
||
618 | } |
||
619 | |||
620 | /** |
||
621 | * Is post class or class map. |
||
622 | * |
||
623 | * @param string|array $args |
||
624 | * @return bool |
||
625 | * @deprecated since 0.20.0 |
||
626 | */ |
||
627 | public static function is_post_class_or_class_map( $args ) { |
||
630 | |||
631 | } |
||
632 |