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() { |
||
191 | |||
192 | /** |
||
193 | * Compile function. |
||
194 | * |
||
195 | * @param array $filenames |
||
196 | * @param array $data |
||
197 | * @param bool $expires |
||
198 | * @param string $cache_mode |
||
199 | * @param bool $via_render |
||
200 | * @return bool|string |
||
201 | */ |
||
202 | public static function compile( $filenames, $data = array(), $expires = false, $cache_mode = TimberLoader::CACHE_USE_DEFAULT, $via_render = false ) { |
||
225 | |||
226 | /** |
||
227 | * Compile string. |
||
228 | * |
||
229 | * @param string $string a string with twig variables. |
||
230 | * @param array $data an array with data in it. |
||
231 | * @return bool|string |
||
232 | */ |
||
233 | public static function compile_string( $string, $data = array() ) { |
||
242 | |||
243 | /** |
||
244 | * Fetch function. |
||
245 | * |
||
246 | * @param array $filenames |
||
247 | * @param array $data |
||
248 | * @param bool $expires |
||
249 | * @param string $cache_mode |
||
250 | * @return bool|string |
||
251 | */ |
||
252 | public static function fetch( $filenames, $data = array(), $expires = false, $cache_mode = TimberLoader::CACHE_USE_DEFAULT ) { |
||
263 | |||
264 | /** |
||
265 | * Render function. |
||
266 | * |
||
267 | * @param array $filenames |
||
268 | * @param array $data |
||
269 | * @param bool $expires |
||
270 | * @param string $cache_mode |
||
271 | * @return bool|string |
||
272 | */ |
||
273 | public static function render( $filenames, $data = array(), $expires = false, $cache_mode = TimberLoader::CACHE_USE_DEFAULT ) { |
||
278 | |||
279 | /** |
||
280 | * Render string. |
||
281 | * |
||
282 | * @param string $string a string with twig variables. |
||
283 | * @param array $data an array with data in it. |
||
284 | * @return bool|string |
||
285 | */ |
||
286 | public static function render_string( $string, $data = array() ) { |
||
291 | |||
292 | |||
293 | /* Sidebar |
||
294 | ================================ */ |
||
295 | |||
296 | /** |
||
297 | * Get sidebar. |
||
298 | * |
||
299 | * @param string $sidebar |
||
300 | * @param array $data |
||
301 | * @return bool|string |
||
302 | */ |
||
303 | public static function get_sidebar( $sidebar = '', $data = array() ) { |
||
312 | |||
313 | /** |
||
314 | * Get sidebar from PHP |
||
315 | * |
||
316 | * @param string $sidebar |
||
317 | * @param array $data |
||
318 | * @return string |
||
319 | */ |
||
320 | public static function get_sidebar_from_php( $sidebar = '', $data ) { |
||
340 | |||
341 | /* Widgets |
||
342 | ================================ */ |
||
343 | |||
344 | /** |
||
345 | * Get widgets. |
||
346 | * |
||
347 | * @param int $widget_id |
||
348 | * @return TimberFunctionWrapper |
||
349 | */ |
||
350 | public static function get_widgets( $widget_id ) { |
||
353 | |||
354 | |||
355 | /* Routes |
||
356 | ================================ */ |
||
357 | |||
358 | /** |
||
359 | * Add route. |
||
360 | * |
||
361 | * @param string $route |
||
362 | * @param callable $callback |
||
363 | * @param array $args |
||
364 | * @deprecated since 0.20.0 |
||
365 | */ |
||
366 | public static function add_route( $route, $callback, $args = array() ) { |
||
369 | |||
370 | /** |
||
371 | * Load template. |
||
372 | * |
||
373 | * @deprecated since 0.20.0 |
||
374 | */ |
||
375 | public static function load_template( $template, $query = false, $status_code = 200, $tparams = false ) { |
||
378 | |||
379 | /** |
||
380 | * Load view. |
||
381 | * |
||
382 | * @deprecated since 0.20.2 |
||
383 | */ |
||
384 | public static function load_view( $template, $query = false, $status_code = 200, $tparams = false ) { |
||
387 | |||
388 | |||
389 | /* Pagination |
||
390 | ================================ */ |
||
391 | |||
392 | /** |
||
393 | * Get pagination. |
||
394 | * |
||
395 | * @param array $prefs |
||
396 | * @return array mixed |
||
397 | */ |
||
398 | public static function get_pagination( $prefs = array() ) { |
||
444 | |||
445 | /* Utility |
||
446 | ================================ */ |
||
447 | |||
448 | /** |
||
449 | * Get calling script dir. |
||
450 | * |
||
451 | * @return string |
||
452 | */ |
||
453 | public static function get_calling_script_dir( $offset = 0 ) { |
||
461 | |||
462 | /** |
||
463 | * Get calling script file. |
||
464 | * |
||
465 | * @param int $offset |
||
466 | * @return string|null |
||
467 | * @deprecated since 0.20.0 |
||
468 | */ |
||
469 | public static function get_calling_script_file( $offset = 0 ) { |
||
485 | |||
486 | |||
487 | } |
||
488 | |||
490 | Timber::$dirname = 'views'; |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.