Complex classes like ViewThemer 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 ViewThemer, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Myth\Themers; |
||
40 | class ViewThemer implements ThemerInterface |
||
41 | { |
||
42 | |||
43 | protected $theme = ''; |
||
44 | |||
45 | protected $default_theme = null; |
||
46 | |||
47 | protected $active_theme = null; |
||
48 | |||
49 | protected $layout = 'index'; |
||
50 | |||
51 | protected $view = ''; |
||
52 | |||
53 | protected $vars = []; |
||
54 | |||
55 | protected $folders = []; |
||
56 | |||
57 | protected $variants = []; |
||
58 | |||
59 | protected $current_variant = ''; |
||
60 | |||
61 | protected $parse_views = false; |
||
62 | |||
63 | protected $ci; |
||
64 | |||
65 | //-------------------------------------------------------------------- |
||
66 | |||
67 | public function __construct($ci) |
||
73 | |||
74 | //-------------------------------------------------------------------- |
||
75 | |||
76 | /** |
||
77 | * The main entryway into rendering a view. This is called from the |
||
78 | * controller and is generally the last method called. |
||
79 | * |
||
80 | * @param string $layout If provided, will override the default layout. |
||
81 | * @param int $cache_time The number of seconds to cache the output for. |
||
82 | * @return mixed |
||
83 | */ |
||
84 | public function render($layout = null) |
||
105 | |||
106 | //-------------------------------------------------------------------- |
||
107 | |||
108 | /** |
||
109 | * Used within the template layout file to render the current content. |
||
110 | * This content is typically used to display the current view. |
||
111 | * |
||
112 | * @param int $cache_time |
||
113 | * |
||
114 | * @return mixed |
||
115 | */ |
||
116 | public function content() |
||
140 | |||
141 | //-------------------------------------------------------------------- |
||
142 | |||
143 | /** |
||
144 | * Loads a view file. Useful to control caching. Intended for use |
||
145 | * from within view files. |
||
146 | * |
||
147 | * You can specify that a view should belong to a theme by prefixing |
||
148 | * the name of the theme and a colon to the view name. For example, |
||
149 | * "admin:header" would try to display the "header.php" file within |
||
150 | * the "admin" theme. |
||
151 | * |
||
152 | * If a variant has been specified, it will be added to the end |
||
153 | * of the view name before looking for the file. |
||
154 | * |
||
155 | * @param string $view |
||
156 | * @param array $data |
||
157 | * @param int $cache_time Number of minutes to cache the page for |
||
158 | * @param string $cache_name A custom name for the cached file. |
||
159 | * @return mixed |
||
160 | */ |
||
161 | public function display($view, $data = array(), $cache_time = 0, $cache_name=null) |
||
229 | |||
230 | //-------------------------------------------------------------------- |
||
231 | |||
232 | /** |
||
233 | * Runs a callback method and returns the contents to the view. |
||
234 | * |
||
235 | * @param $command |
||
236 | * @param int $cache_time |
||
237 | * @param string $cache_name // Number of MINUTES to cache output |
||
238 | * @return mixed|void |
||
239 | */ |
||
240 | public function call($command, $cache_time=0, $cache_name=null) |
||
293 | |||
294 | //-------------------------------------------------------------------- |
||
295 | |||
296 | /** |
||
297 | * Sets the active theme to use. This theme should be |
||
298 | * relative to one of the 'theme_paths' folders. |
||
299 | * |
||
300 | * @param $theme |
||
301 | */ |
||
302 | public function setTheme($theme) |
||
306 | |||
307 | //-------------------------------------------------------------------- |
||
308 | |||
309 | /** |
||
310 | * Returns the current theme. |
||
311 | * |
||
312 | * @return mixed|string |
||
313 | */ |
||
314 | public function theme() |
||
318 | |||
319 | //-------------------------------------------------------------------- |
||
320 | |||
321 | /** |
||
322 | * Sets the default theme to use if another isn't specified. |
||
323 | * |
||
324 | * @param $theme |
||
325 | * @return mixed|void |
||
326 | */ |
||
327 | public function setDefaultTheme($theme) |
||
331 | |||
332 | //-------------------------------------------------------------------- |
||
333 | |||
334 | |||
335 | /** |
||
336 | * Sets the current view file to render. |
||
337 | * |
||
338 | * @param $file |
||
339 | * @return mixed |
||
340 | */ |
||
341 | public function setView($file) |
||
345 | |||
346 | //-------------------------------------------------------------------- |
||
347 | |||
348 | /** |
||
349 | * Returns the current view. |
||
350 | * |
||
351 | * @return mixed|string |
||
352 | */ |
||
353 | public function view() |
||
357 | |||
358 | //-------------------------------------------------------------------- |
||
359 | |||
360 | /** |
||
361 | * Sets the current layout to be used. MUST be the name of one of |
||
362 | * the layout files within the current theme. Case-sensitive. |
||
363 | * |
||
364 | * @param $file |
||
365 | * @return mixed |
||
366 | */ |
||
367 | public function setLayout($file) |
||
373 | |||
374 | //-------------------------------------------------------------------- |
||
375 | |||
376 | /** |
||
377 | * Returns the current layout. |
||
378 | * |
||
379 | * @return mixed|string |
||
380 | */ |
||
381 | public function layout() |
||
385 | |||
386 | //-------------------------------------------------------------------- |
||
387 | |||
388 | /** |
||
389 | * Stores one or more pieces of data to be passed to the views when |
||
390 | * they are rendered out. |
||
391 | * |
||
392 | * If both $key and $value are ! empty, then it will treat it as a |
||
393 | * key/value pair. If $key is an array of key/value pairs, then $value |
||
394 | * is ignored and each element of the array are made available to the |
||
395 | * view as if it was a single $key/$value pair. |
||
396 | * |
||
397 | * @param string|array $key |
||
398 | * @param mixed $value |
||
399 | * @return $this |
||
400 | */ |
||
401 | public function set($key, $value = null) |
||
412 | |||
413 | //-------------------------------------------------------------------- |
||
414 | |||
415 | /** |
||
416 | * Returns a value that has been previously set(). |
||
417 | * |
||
418 | * @param $key |
||
419 | * @return mixed |
||
420 | */ |
||
421 | public function get($key) |
||
425 | |||
426 | //-------------------------------------------------------------------- |
||
427 | |||
428 | /** |
||
429 | * Determines whether or not the view should be parsed with the |
||
430 | * CodeIgniter's parser. |
||
431 | * |
||
432 | * @param bool $parse |
||
433 | * @return mixed |
||
434 | */ |
||
435 | public function parseViews($parse = false) |
||
441 | |||
442 | //-------------------------------------------------------------------- |
||
443 | |||
444 | /** |
||
445 | * Theme paths allow you to have multiple locations for themes to be |
||
446 | * stored. This might be used for separating themes for different sub- |
||
447 | * applications, or a core theme and user-submitted themes. |
||
448 | * |
||
449 | * @param $alias The name the theme can be referenced by |
||
450 | * @param $path A new path where themes can be found. |
||
451 | * |
||
452 | * @return mixed |
||
453 | */ |
||
454 | public function addThemePath($alias, $path) |
||
460 | |||
461 | //-------------------------------------------------------------------- |
||
462 | |||
463 | /** |
||
464 | * Removes a single theme path. |
||
465 | * |
||
466 | * @param $alias |
||
467 | * @return $this |
||
468 | */ |
||
469 | public function removeThemePath($alias) |
||
475 | |||
476 | //-------------------------------------------------------------------- |
||
477 | |||
478 | /** |
||
479 | * Returns the path to the active/default theme's folder. |
||
480 | * |
||
481 | * @return string|null |
||
482 | */ |
||
483 | public function getThemePath() |
||
494 | |||
495 | //-------------------------------------------------------------------- |
||
496 | |||
497 | |||
498 | |||
499 | //-------------------------------------------------------------------- |
||
500 | // Variants |
||
501 | //-------------------------------------------------------------------- |
||
502 | |||
503 | /** |
||
504 | * Sets the variant used when creating view names. These variants can |
||
505 | * be anything, but by default are used to render specific templates |
||
506 | * for desktop, tablet, and phone. The name of the variant is added |
||
507 | * to the view name, joined by a "+" symbol. |
||
508 | * |
||
509 | * Example: |
||
510 | * $this->setVariant('phone'); |
||
511 | * $this->display('header'); |
||
512 | * |
||
513 | * Tries to display "views/header+phone.php" |
||
514 | * |
||
515 | * @param $variant |
||
516 | * @return $this |
||
517 | */ |
||
518 | public function setVariant($variant) |
||
526 | //-------------------------------------------------------------------- |
||
527 | |||
528 | /** |
||
529 | * Adds a new variant to the system. |
||
530 | * |
||
531 | * @param $name |
||
532 | * @param $postfix |
||
533 | * @return $this|mixed |
||
534 | */ |
||
535 | public function addVariant($name, $postfix) |
||
541 | |||
542 | //-------------------------------------------------------------------- |
||
543 | |||
544 | /** |
||
545 | * Removes a variant from the system. |
||
546 | * |
||
547 | * @param $name |
||
548 | * @return $this|mixed |
||
549 | */ |
||
550 | public function removeVariant($name) |
||
558 | |||
559 | //-------------------------------------------------------------------- |
||
560 | // Private Methods |
||
561 | //-------------------------------------------------------------------- |
||
562 | |||
563 | /** |
||
564 | * Handles the actual loading of a view file, and checks for any |
||
565 | * overrides in themes, etc. |
||
566 | * |
||
567 | * @param $view |
||
568 | * @param $data |
||
569 | * |
||
570 | * @return string |
||
571 | */ |
||
572 | private function loadView($view, $data) |
||
599 | |||
600 | //-------------------------------------------------------------------- |
||
601 | |||
602 | } |
||
603 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.