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 ) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get the template engine instance |
||
| 102 | * |
||
| 103 | * Named controller for usability |
||
| 104 | * |
||
| 105 | * @return Stencil |
||
| 106 | * @throws Exception If Stencil was not initialized properly. |
||
| 107 | */ |
||
| 108 | public static function controller() { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get a new Handler |
||
| 118 | * |
||
| 119 | * @param Stencil_Implementation|null $implementation Optional. Set Implementation on new Handler. |
||
| 120 | * @param Stencil_Recorder_Interface|null $recorder Optional. Supply a custom Recorder. |
||
| 121 | * |
||
| 122 | * @return Stencil_Handler |
||
| 123 | */ |
||
| 124 | public static function get_handler( Stencil_Implementation $implementation = null, Stencil_Recorder_Interface $recorder = null ) { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Set the flow controller |
||
| 135 | * |
||
| 136 | * Handler proxy functions |
||
| 137 | * |
||
| 138 | * @param Stencil_Flow_Interface $flow The new Flow to set to Stencil. |
||
| 139 | */ |
||
| 140 | public function set_flow( Stencil_Flow_Interface $flow ) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get the used flow |
||
| 146 | * |
||
| 147 | * Handler proxy functions |
||
| 148 | * |
||
| 149 | * @return Stencil_Flow_Interface |
||
| 150 | */ |
||
| 151 | public function get_flow() { |
||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * Get a variable value from the template |
||
| 158 | * |
||
| 159 | * Handler proxy function |
||
| 160 | * |
||
| 161 | * @param string $variable Variable name to retrieve. |
||
| 162 | * |
||
| 163 | * @return mixed |
||
| 164 | */ |
||
| 165 | public function get( $variable ) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Set a variable to the template |
||
| 171 | * |
||
| 172 | * Handler proxy function |
||
| 173 | * |
||
| 174 | * @param string $variable Name of the variable to set. |
||
| 175 | * @param mixed $value Value of the variable to set. |
||
| 176 | * |
||
| 177 | * @return mixed The value of the variable after it being set. |
||
| 178 | */ |
||
| 179 | public function set( $variable, $value ) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get the implementation |
||
| 185 | * |
||
| 186 | * Handler proxy functions |
||
| 187 | * |
||
| 188 | * @return Stencil_Implementation |
||
| 189 | */ |
||
| 190 | public function get_implementation() { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Get the reference to the engine of the loaded Proxy |
||
| 196 | * |
||
| 197 | * This way theme developers can apply services and other |
||
| 198 | * engine specific functionality easily |
||
| 199 | * |
||
| 200 | * @return mixed |
||
| 201 | */ |
||
| 202 | public function get_engine() { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Set the recorder |
||
| 208 | * |
||
| 209 | * Handler proxy functions |
||
| 210 | * |
||
| 211 | * @param Stencil_Recorder_Interface $recorder New Recorder to use. |
||
| 212 | */ |
||
| 213 | public function set_recorder( Stencil_Recorder_Interface $recorder ) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Handler proxy functions |
||
| 219 | * |
||
| 220 | * Get the used recorder |
||
| 221 | * |
||
| 222 | * @return Stencil_Recorder_Interface |
||
| 223 | */ |
||
| 224 | public function get_recorder() { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Start recording for a variable |
||
| 230 | * |
||
| 231 | * Handler proxy functions |
||
| 232 | * |
||
| 233 | * @param string $variable Variable to Record into. |
||
| 234 | * @param Stencil_Recorder_Interface|null $recorder Optional. Custom recorder to use for this action. |
||
| 235 | */ |
||
| 236 | public function start_recording( $variable, Stencil_Recorder_Interface $recorder = null ) { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Finish a recording |
||
| 242 | * |
||
| 243 | * Handler proxy functions |
||
| 244 | * |
||
| 245 | * @returns mixed Value of Recording. |
||
| 246 | */ |
||
| 247 | public function finish_recording() { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Set the hierarchy handler for a page type |
||
| 253 | * |
||
| 254 | * HandlerFactory proxy functions |
||
| 255 | * |
||
| 256 | * @param string $type Page Type to set for. |
||
| 257 | * @param array|Traversable $handler Handler that will provide hierarchy for specified page. |
||
| 258 | */ |
||
| 259 | public function set_hierarchy_handler( $type, $handler ) { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Set the page type handler for a page |
||
| 265 | * |
||
| 266 | * HandlerFactory proxy functions |
||
| 267 | * |
||
| 268 | * @param string $type Page Type to set for. |
||
| 269 | * @param callable $handler Handler that will be executed for specified page. |
||
| 270 | */ |
||
| 271 | public function set_page_type_handler( $type, $handler ) { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Set the page type hooker for a page |
||
| 277 | * |
||
| 278 | * HandlerFactory proxy functions |
||
| 279 | * |
||
| 280 | * @param string $type Page Type to set for. |
||
| 281 | * @param callable $handler Hooker that will be executed for specified page. |
||
| 282 | */ |
||
| 283 | public function set_page_type_hooker( $type, $handler ) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Remove the page type handler for a page |
||
| 289 | * |
||
| 290 | * HandlerFactory proxy functions |
||
| 291 | * |
||
| 292 | * @param string $type Page Type to remove handler of. |
||
| 293 | */ |
||
| 294 | public function remove_page_type_handler( $type ) { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Remove the page hooker for a page |
||
| 300 | * |
||
| 301 | * HandlerFactory proxy functions |
||
| 302 | * |
||
| 303 | * @param string $type Page Type to remove hooker of. |
||
| 304 | */ |
||
| 305 | public function remove_page_type_hooker( $type ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Run the main process |
||
| 311 | * |
||
| 312 | * @param string|null $page Optional. Fake a certain page. |
||
| 313 | */ |
||
| 314 | public function run( $page = null ) { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Append assets directory to the template directory uri |
||
| 351 | * |
||
| 352 | * @param string $base Base Path to append the assets directory to. |
||
| 353 | * |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | public static function append_assets_directory( $base ) { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Rewrite all scripts to index.php of the theme |
||
| 387 | * |
||
| 388 | * @param string $template Template that is being loaded. |
||
| 389 | * |
||
| 390 | * @return mixed |
||
| 391 | */ |
||
| 392 | public static function template_include_override( $template ) { |
||
| 410 | } |
||
| 411 |