Complex classes like View_Core 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 View_Core, and based on these observations, apply Extract Interface, too.
| 1 | <?php defined('SYSPATH') or die('No direct access allowed.'); |
||
| 13 | class View_Core |
||
| 14 | { |
||
| 15 | |||
| 16 | // The view file name and type |
||
| 17 | protected $kohana_filename = false; |
||
| 18 | protected $kohana_filetype = false; |
||
| 19 | |||
| 20 | // View variable storage |
||
| 21 | protected $kohana_local_data = array(); |
||
| 22 | protected static $kohana_global_data = array(); |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Creates a new View using the given parameters. |
||
| 26 | * |
||
| 27 | * @param string view name |
||
| 28 | * @param array pre-load data |
||
| 29 | * @param string type of file: html, css, js, etc. |
||
| 30 | * @param string $name |
||
|
|
|||
| 31 | * @return View |
||
| 32 | */ |
||
| 33 | public static function factory($name = null, $data = null, $type = null) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Attempts to load a view and pre-load view data. |
||
| 40 | * |
||
| 41 | * @throws Kohana_Exception if the requested view cannot be found |
||
| 42 | * @param string view name |
||
| 43 | * @param array pre-load data |
||
| 44 | * @param string type of file: html, css, js, etc. |
||
| 45 | * @return void |
||
| 46 | */ |
||
| 47 | public function __construct($name = null, $data = null, $type = null) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Magic method access to test for view property |
||
| 62 | * |
||
| 63 | * @param string View property to test for |
||
| 64 | * @return boolean |
||
| 65 | */ |
||
| 66 | public function __isset($key = null) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Sets the view filename. |
||
| 73 | * |
||
| 74 | * @chainable |
||
| 75 | * @param string view filename |
||
| 76 | * @param string view file type |
||
| 77 | * @param string $name |
||
| 78 | * @return View_Core |
||
| 79 | */ |
||
| 80 | public function set_filename($name, $type = null) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Sets a view variable. |
||
| 107 | * |
||
| 108 | * @param string|array name of variable or an array of variables |
||
| 109 | * @param mixed value when using a named variable |
||
| 110 | * @return View_Core |
||
| 111 | */ |
||
| 112 | public function set($name, $value = null) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Checks for a property existence in the view locally or globally. Unlike the built in __isset(), |
||
| 127 | * this method can take an array of properties to test simultaneously. |
||
| 128 | * |
||
| 129 | * @param string $key property name to test for |
||
| 130 | * @param array $key array of property names to test for |
||
| 131 | * @return boolean property test result |
||
| 132 | * @return array associative array of keys and boolean test result |
||
| 133 | */ |
||
| 134 | public function is_set($key = false) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Sets a bound variable by reference. |
||
| 160 | * |
||
| 161 | * @param string name of variable |
||
| 162 | * @param mixed variable to assign by reference |
||
| 163 | * @param string $name |
||
| 164 | * @return View_Core |
||
| 165 | */ |
||
| 166 | public function bind($name, & $var) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Sets a view global variable. |
||
| 175 | * |
||
| 176 | * @param string|array name of variable or an array of variables |
||
| 177 | * @param mixed value when using a named variable |
||
| 178 | * @return void |
||
| 179 | */ |
||
| 180 | public static function set_global($name, $value = null) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Magically sets a view variable. |
||
| 193 | * |
||
| 194 | * @param string variable key |
||
| 195 | * @param string variable value |
||
| 196 | * @return void |
||
| 197 | */ |
||
| 198 | public function __set($key, $value) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Magically gets a view variable. |
||
| 205 | * |
||
| 206 | * @param string variable key |
||
| 207 | * @return mixed variable value if the key is found |
||
| 208 | * @return void if the key is not found |
||
| 209 | */ |
||
| 210 | public function &__get($key) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Magically converts view object to string. |
||
| 227 | * |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | public function __toString() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Renders a view. |
||
| 242 | * |
||
| 243 | * @param boolean set to TRUE to echo the output instead of returning it |
||
| 244 | * @param callback special renderer to pass the output through |
||
| 245 | * @return string if print is FALSE |
||
| 246 | * @return void if print is TRUE |
||
| 247 | */ |
||
| 248 | public function render($print = false, $renderer = false) |
||
| 290 | } // End View |
||
| 291 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.