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 | * WP_Query has posts. |
||
120 | * |
||
121 | * @return bool |
||
122 | * @deprecated since 0.20.0 |
||
123 | */ |
||
124 | static function wp_query_has_posts() { |
||
127 | |||
128 | /* Term Retrieval |
||
129 | ================================ */ |
||
130 | |||
131 | /** |
||
132 | * Get terms. |
||
133 | * |
||
134 | * @param string|array $args |
||
135 | * @param array $maybe_args |
||
136 | * @param string $TermClass |
||
137 | * @return mixed |
||
138 | */ |
||
139 | public static function get_terms( $args = null, $maybe_args = array(), $TermClass = 'TimberTerm' ) { |
||
142 | |||
143 | /* Site Retrieval |
||
144 | ================================ */ |
||
145 | |||
146 | /** |
||
147 | * Get sites. |
||
148 | * |
||
149 | * @param array|bool $blog_ids |
||
150 | * @return array |
||
151 | */ |
||
152 | public static function get_sites( $blog_ids = false ) { |
||
163 | |||
164 | |||
165 | /* Template Setup and Display |
||
166 | ================================ */ |
||
167 | |||
168 | /** |
||
169 | * Get context. |
||
170 | * |
||
171 | * @return array |
||
172 | */ |
||
173 | public static function get_context() { |
||
190 | |||
191 | /** |
||
192 | * Compile function. |
||
193 | * |
||
194 | * @param array $filenames |
||
195 | * @param array $data |
||
196 | * @param bool $expires |
||
197 | * @param string $cache_mode |
||
198 | * @param bool $via_render |
||
199 | * @return bool|string |
||
200 | */ |
||
201 | public static function compile( $filenames, $data = array(), $expires = false, $cache_mode = TimberLoader::CACHE_USE_DEFAULT, $via_render = false ) { |
||
224 | |||
225 | /** |
||
226 | * Compile string. |
||
227 | * |
||
228 | * @param string $string a string with twig variables. |
||
229 | * @param array $data an array with data in it. |
||
230 | * @return bool|string |
||
231 | */ |
||
232 | public static function compile_string( $string, $data = array() ) { |
||
241 | |||
242 | /** |
||
243 | * Fetch function. |
||
244 | * |
||
245 | * @param array $filenames |
||
246 | * @param array $data |
||
247 | * @param bool $expires |
||
248 | * @param string $cache_mode |
||
249 | * @return bool|string |
||
250 | */ |
||
251 | public static function fetch( $filenames, $data = array(), $expires = false, $cache_mode = TimberLoader::CACHE_USE_DEFAULT ) { |
||
262 | |||
263 | /** |
||
264 | * Render function. |
||
265 | * |
||
266 | * @param array $filenames |
||
267 | * @param array $data |
||
268 | * @param bool $expires |
||
269 | * @param string $cache_mode |
||
270 | * @return bool|string |
||
271 | */ |
||
272 | public static function render( $filenames, $data = array(), $expires = false, $cache_mode = TimberLoader::CACHE_USE_DEFAULT ) { |
||
277 | |||
278 | /** |
||
279 | * Render string. |
||
280 | * |
||
281 | * @param string $string a string with twig variables. |
||
282 | * @param array $data an array with data in it. |
||
283 | * @return bool|string |
||
284 | */ |
||
285 | public static function render_string( $string, $data = array() ) { |
||
290 | |||
291 | |||
292 | /* Sidebar |
||
293 | ================================ */ |
||
294 | |||
295 | /** |
||
296 | * Get sidebar. |
||
297 | * |
||
298 | * @param string $sidebar |
||
299 | * @param array $data |
||
300 | * @return bool|string |
||
301 | */ |
||
302 | public static function get_sidebar( $sidebar = '', $data = array() ) { |
||
311 | |||
312 | /** |
||
313 | * Get sidebar from PHP |
||
314 | * |
||
315 | * @param string $sidebar |
||
316 | * @param array $data |
||
317 | * @return string |
||
318 | */ |
||
319 | public static function get_sidebar_from_php( $sidebar = '', $data ) { |
||
339 | |||
340 | /* Widgets |
||
341 | ================================ */ |
||
342 | |||
343 | /** |
||
344 | * Get widgets. |
||
345 | * |
||
346 | * @param int $widget_id |
||
347 | * @return TimberFunctionWrapper |
||
348 | */ |
||
349 | public static function get_widgets( $widget_id ) { |
||
352 | |||
353 | |||
354 | /* Routes |
||
355 | ================================ */ |
||
356 | |||
357 | /** |
||
358 | * Add route. |
||
359 | * |
||
360 | * @param string $route |
||
361 | * @param callable $callback |
||
362 | * @param array $args |
||
363 | * @deprecated since 0.20.0 |
||
364 | */ |
||
365 | public static function add_route( $route, $callback, $args = array() ) { |
||
368 | |||
369 | /** |
||
370 | * Load template. |
||
371 | * |
||
372 | * @deprecated since 0.20.0 |
||
373 | */ |
||
374 | public static function load_template( $template, $query = false, $status_code = 200, $tparams = false ) { |
||
377 | |||
378 | /** |
||
379 | * Load view. |
||
380 | * |
||
381 | * @deprecated since 0.20.2 |
||
382 | */ |
||
383 | public static function load_view( $template, $query = false, $status_code = 200, $tparams = false ) { |
||
386 | |||
387 | |||
388 | /* Pagination |
||
389 | ================================ */ |
||
390 | |||
391 | /** |
||
392 | * Get pagination. |
||
393 | * |
||
394 | * @param array $prefs |
||
395 | * @return array mixed |
||
396 | */ |
||
397 | public static function get_pagination( $prefs = array() ) { |
||
440 | |||
441 | /* Utility |
||
442 | ================================ */ |
||
443 | |||
444 | /** |
||
445 | * Get calling script dir. |
||
446 | * |
||
447 | * @return string |
||
448 | */ |
||
449 | public static function get_calling_script_dir( $offset = 0 ) { |
||
457 | |||
458 | /** |
||
459 | * Get calling script file. |
||
460 | * |
||
461 | * @param int $offset |
||
462 | * @return string|null |
||
463 | * @deprecated since 0.20.0 |
||
464 | */ |
||
465 | public static function get_calling_script_file( $offset = 0 ) { |
||
481 | |||
482 | |||
483 | } |
||
484 |