Complex classes like Stencil 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 Stencil, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | final class Stencil implements Stencil_Interface, Stencil_Handlers_Interface, Stencil_Handler_Interface { |
||
| 18 | /** |
||
| 19 | * Instance for singleton |
||
| 20 | * |
||
| 21 | * @var Stencil |
||
| 22 | */ |
||
| 23 | private static $instance; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Handler instance |
||
| 27 | * |
||
| 28 | * @var Stencil_Handler |
||
| 29 | */ |
||
| 30 | private static $handler; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Flow instance |
||
| 34 | * |
||
| 35 | * @var Stencil_Flow_Interface |
||
| 36 | */ |
||
| 37 | private static $flow; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The default implementation class |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private static $default_implementation_class; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Boot up the main Stencil flow |
||
| 48 | * |
||
| 49 | * Triggered by the {FILTER_PREFIX}engine_ready hook |
||
| 50 | * |
||
| 51 | * @param Stencil_Implementation $engine Boot Stencil with the supplied engine. |
||
| 52 | */ |
||
| 53 | public static function boot( Stencil_Implementation $engine ) { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Construct the class and initialize engine |
||
| 68 | * |
||
| 69 | * @param Stencil_Implementation $engine Initilize Stencil with the supplied engine. |
||
| 70 | */ |
||
| 71 | private function __construct( Stencil_Implementation $engine ) { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get the template engine instance |
||
| 103 | * |
||
| 104 | * Named controller for usability |
||
| 105 | * |
||
| 106 | * @return Stencil |
||
| 107 | * @throws Exception If Stencil was not initialized properly. |
||
| 108 | */ |
||
| 109 | public static function controller() { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get a new Handler |
||
| 119 | * |
||
| 120 | * @param Stencil_Implementation|null $implementation Optional. Set Implementation on new Handler. |
||
| 121 | * @param Stencil_Recorder_Interface|null $recorder Optional. Supply a custom Recorder. |
||
| 122 | * |
||
| 123 | * @return Stencil_Handler |
||
| 124 | */ |
||
| 125 | public static function get_handler( Stencil_Implementation $implementation = null, Stencil_Recorder_Interface $recorder = null ) { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Set the flow controller |
||
| 136 | * |
||
| 137 | * Handler proxy functions |
||
| 138 | * |
||
| 139 | * @param Stencil_Flow_Interface $flow The new Flow to set to Stencil. |
||
| 140 | */ |
||
| 141 | public function set_flow( Stencil_Flow_Interface $flow ) { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Get the used flow |
||
| 147 | * |
||
| 148 | * Handler proxy functions |
||
| 149 | * |
||
| 150 | * @return Stencil_Flow_Interface |
||
| 151 | */ |
||
| 152 | public function get_flow() { |
||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * Get a variable value from the template |
||
| 159 | * |
||
| 160 | * Handler proxy function |
||
| 161 | * |
||
| 162 | * @param string $variable Variable name to retrieve. |
||
| 163 | * |
||
| 164 | * @return mixed |
||
| 165 | */ |
||
| 166 | public function get( $variable ) { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Set a variable to the template |
||
| 172 | * |
||
| 173 | * Handler proxy function |
||
| 174 | * |
||
| 175 | * @param string $variable Name of the variable to set. |
||
| 176 | * @param mixed $value Value of the variable to set. |
||
| 177 | * |
||
| 178 | * @return mixed The value of the variable after it being set. |
||
| 179 | */ |
||
| 180 | public function set( $variable, $value ) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Get the implementation |
||
| 186 | * |
||
| 187 | * Handler proxy functions |
||
| 188 | * |
||
| 189 | * @return Stencil_Implementation |
||
| 190 | */ |
||
| 191 | public function get_implementation() { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get the reference to the engine of the loaded Proxy |
||
| 197 | * |
||
| 198 | * This way theme developers can apply services and other |
||
| 199 | * engine specific functionality easily |
||
| 200 | * |
||
| 201 | * @return mixed |
||
| 202 | */ |
||
| 203 | public function get_engine() { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Set the recorder |
||
| 209 | * |
||
| 210 | * Handler proxy functions |
||
| 211 | * |
||
| 212 | * @param Stencil_Recorder_Interface $recorder New Recorder to use. |
||
| 213 | */ |
||
| 214 | public function set_recorder( Stencil_Recorder_Interface $recorder ) { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Handler proxy functions |
||
| 220 | * |
||
| 221 | * Get the used recorder |
||
| 222 | * |
||
| 223 | * @return Stencil_Recorder_Interface |
||
| 224 | */ |
||
| 225 | public function get_recorder() { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Start recording for a variable |
||
| 231 | * |
||
| 232 | * Handler proxy functions |
||
| 233 | * |
||
| 234 | * @param string $variable Variable to Record into. |
||
| 235 | * @param Stencil_Recorder_Interface|null $recorder Optional. Custom recorder to use for this action. |
||
| 236 | */ |
||
| 237 | public function start_recording( $variable, Stencil_Recorder_Interface $recorder = null ) { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Finish a recording |
||
| 243 | * |
||
| 244 | * Handler proxy functions |
||
| 245 | * |
||
| 246 | * @returns mixed Value of Recording. |
||
| 247 | */ |
||
| 248 | public function finish_recording() { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Set the hierarchy handler for a page type |
||
| 254 | * |
||
| 255 | * HandlerFactory proxy functions |
||
| 256 | * |
||
| 257 | * @param string $page Page Type to set for. |
||
| 258 | * @param array|Traversable|null $handler Optional. Handler that will provide hierarchy for specified page. |
||
| 259 | */ |
||
| 260 | public function set_hierarchy( $page, $handler ) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Remove all view options for a page |
||
| 266 | * |
||
| 267 | * This will make the page be displayed with the 'index' view |
||
| 268 | * |
||
| 269 | * @param string $page Page to remove hierarchy of |
||
| 270 | */ |
||
| 271 | public function remove_hierarchy( $page ) { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Set the page type handler for a page |
||
| 277 | * |
||
| 278 | * HandlerFactory proxy functions |
||
| 279 | * |
||
| 280 | * @param string $page Page to set for. |
||
| 281 | * @param callable $handler Handler that will be executed for specified page. |
||
| 282 | */ |
||
| 283 | public function set_page_type_handler( $page, $handler ) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Set the page type hooker for a page |
||
| 289 | * |
||
| 290 | * HandlerFactory proxy functions |
||
| 291 | * |
||
| 292 | * @param string $page Page to set for. |
||
| 293 | * @param callable $handler Hooker that will be executed for specified page. |
||
| 294 | */ |
||
| 295 | public function set_page_type_hooker( $page, $handler ) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Remove the page type handler for a page |
||
| 301 | * |
||
| 302 | * HandlerFactory proxy functions |
||
| 303 | * |
||
| 304 | * @param string $page Page Type to remove handler of. |
||
| 305 | */ |
||
| 306 | public function remove_page_type_handler( $page ) { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Remove the page hooker for a page |
||
| 312 | * |
||
| 313 | * HandlerFactory proxy functions |
||
| 314 | * |
||
| 315 | * @param string $page Page Type to remove hooker of. |
||
| 316 | */ |
||
| 317 | public function remove_page_type_hooker( $page ) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Run the main process |
||
| 323 | * |
||
| 324 | * @param string|null $page Optional. Fake a certain page. |
||
| 325 | */ |
||
| 326 | public function run( $page = null ) { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Append assets directory to the template directory uri |
||
| 363 | * |
||
| 364 | * @param string $base Base Path to append the assets directory to. |
||
| 365 | * |
||
| 366 | * @return string |
||
| 367 | */ |
||
| 368 | public static function append_assets_directory( $base ) { |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Rewrite all scripts to index.php of the theme |
||
| 399 | * |
||
| 400 | * @param string $template Template that is being loaded. |
||
| 401 | * |
||
| 402 | * @return mixed |
||
| 403 | */ |
||
| 404 | public static function template_include_override( $template ) { |
||
| 422 | } |
||
| 423 |