Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 14 | class Stencil_Handler implements Stencil_Handler_Interface, Stencil_Implementation_Interface { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Use ob_start / ob_end_clean to record a variable. |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $recording_for = null; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Revert to recorder after finishing recording. |
||
| 25 | * |
||
| 26 | * @var null|Stencil_Recorder_Interface |
||
| 27 | */ |
||
| 28 | protected $revert_recorder_to; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The instance of the proxy |
||
| 32 | * |
||
| 33 | * The proxy communicates with the engine |
||
| 34 | * |
||
| 35 | * @var Stencil_Implementation $proxy |
||
| 36 | */ |
||
| 37 | protected $proxy; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Recorder instance |
||
| 41 | * |
||
| 42 | * @var Stencil_Recorder_Interface |
||
| 43 | */ |
||
| 44 | protected $recorder; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Control if the wp_head and wp_footer are being loaded |
||
| 48 | * |
||
| 49 | * Default disabled, Stencil will enable this for the core handler |
||
| 50 | * |
||
| 51 | * @var bool |
||
| 52 | */ |
||
| 53 | protected $load_wp_header_and_footer = false; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * StencilHandler constructor. |
||
| 57 | * |
||
| 58 | * @param Stencil_Implementation $implementation Implementation to use. |
||
| 59 | * @param Stencil_Recorder_Interface|null $recorder Recorder to use. |
||
| 60 | */ |
||
| 61 | public function __construct( Stencil_Implementation $implementation, Stencil_Recorder_Interface $recorder = null ) { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Control loading the wp_head and wp_footer into variable |
||
| 68 | * |
||
| 69 | * @param bool|true $yes_or_no Wheter to load them or not. |
||
| 70 | */ |
||
| 71 | public function load_wp_header_and_footer( $yes_or_no = true ) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get the used implementation |
||
| 87 | * |
||
| 88 | * @return Stencil_Implementation |
||
| 89 | */ |
||
| 90 | public function get_implementation() { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Get the default recorder |
||
| 96 | * |
||
| 97 | * @return Stencil_Recorder_Interface |
||
| 98 | */ |
||
| 99 | public function get_recorder() { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Set the new default Recorder |
||
| 105 | * |
||
| 106 | * @param Stencil_Recorder_Interface $recorder The new Recorder to use as default. |
||
| 107 | */ |
||
| 108 | public function set_recorder( Stencil_Recorder_Interface $recorder ) { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Get the engine from the implementation |
||
| 114 | */ |
||
| 115 | public function get_engine() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Find the view that is implemented |
||
| 121 | * |
||
| 122 | * @param array|null $options Options that were collected from the hierarchy. |
||
| 123 | * |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | public function get_usable_view( array $options = null ) { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Sets a variable to the template engine |
||
| 155 | * |
||
| 156 | * @param string $variable Name of the variable. |
||
| 157 | * @param mixed $value Value to assign to the variable. |
||
| 158 | * @param bool $override Optional. Allowed override the variable if already set. |
||
| 159 | * |
||
| 160 | * @return mixed Template known value of the provided variable name |
||
| 161 | */ |
||
| 162 | public function set( $variable, $value, $override = true ) { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Gets a variable from the template engine |
||
| 203 | * |
||
| 204 | * @param string $variable Name of the variable to get. |
||
| 205 | * |
||
| 206 | * @return mixed |
||
| 207 | */ |
||
| 208 | public function get( $variable ) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Displays a chosen template |
||
| 214 | * |
||
| 215 | * Uses fetch to fetch the output |
||
| 216 | * |
||
| 217 | * @param string $template Template file to use. |
||
| 218 | */ |
||
| 219 | public function display( $template ) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Build and display the template |
||
| 225 | * |
||
| 226 | * @param string $template Template file to fetch. |
||
| 227 | * |
||
| 228 | * @return bool|string|void |
||
| 229 | */ |
||
| 230 | public function fetch( $template ) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Set the wp_head and wp_footer variables |
||
| 236 | * |
||
| 237 | * This function is being triggered by |
||
| 238 | * stencil.pre_display and stencil.pre_fetch |
||
| 239 | * so it can be disabled if preferred |
||
| 240 | */ |
||
| 241 | public function set_wp_head_footer() { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Recorder for inline HTML cathing |
||
| 259 | * |
||
| 260 | * @param string $variable Variable to record into. |
||
| 261 | * @param Stencil_Recorder_Interface|null $temporary_recorder Optional. Recorder to use for this recording. |
||
| 262 | * |
||
| 263 | * @throws Exception When already recording for other variable. |
||
| 264 | * @throws InvalidArgumentException When the variable name is not a string. |
||
| 265 | */ |
||
| 266 | public function start_recording( $variable, Stencil_Recorder_Interface $temporary_recorder = null ) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Finish recording raw input |
||
| 291 | * |
||
| 292 | * @return mixed |
||
| 293 | * @throws Exception When we are not recording. |
||
| 294 | */ |
||
| 295 | public function finish_recording() { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Unified function for fetching and displaying a template |
||
| 323 | * |
||
| 324 | * @param string $template Template file to load. |
||
| 325 | * @param string $from Source of this request. |
||
| 326 | * |
||
| 327 | * @return mixed|WP_Error |
||
| 328 | * |
||
| 329 | * @throws LogicException When we are still recording a variable. |
||
| 330 | */ |
||
| 331 | protected function internal_fetch( $template, $from ) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Attach hooks to load the wp_head and wp_footer variables |
||
| 375 | */ |
||
| 376 | View Code Duplication | private function hook_wp_header_and_footer() { |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Detach hooks to load the wp_head and wp_footer variables |
||
| 386 | */ |
||
| 387 | View Code Duplication | private function unhook_wp_header_and_footer() { |
|
| 394 | } |
||
| 395 |