Complex classes like Template 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 Template, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Template |
||
| 14 | { |
||
| 15 | |||
| 16 | const SECTION_MODE_REWRITE = 1; |
||
| 17 | const SECTION_MODE_PREPEND = 2; |
||
| 18 | const SECTION_MODE_APPEND = 3; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Instance of the template engine. |
||
| 22 | * @var Engine |
||
| 23 | */ |
||
| 24 | protected $engine; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The name of the template. |
||
| 28 | * @var Name |
||
| 29 | */ |
||
| 30 | protected $name; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The data assigned to the template. |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $data = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * An array of section content. |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $sections = array(); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The name of the section currently being rendered. |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $sectionName; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The name of the template layout. |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $layoutName; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The data assigned to the template layout. |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $layoutData; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Set section content mode: rewrite/append/prepend |
||
| 64 | * @var int |
||
| 65 | */ |
||
| 66 | protected $sectionMode = self::SECTION_MODE_REWRITE; |
||
| 67 | |||
| 68 | 62 | /** |
|
| 69 | * Create new Template instance. |
||
| 70 | 62 | * @param Engine $engine |
|
| 71 | 62 | * @param string $name |
|
| 72 | */ |
||
| 73 | 62 | public function __construct(Engine $engine, $name) |
|
| 80 | |||
| 81 | /** |
||
| 82 | 4 | * Magic method used to call extension functions. |
|
| 83 | * @param string $name |
||
| 84 | 4 | * @param array $arguments |
|
| 85 | * @return mixed |
||
| 86 | */ |
||
| 87 | public function __call($name, $arguments) |
||
| 91 | |||
| 92 | /** |
||
| 93 | 2 | * Alias for render() method. |
|
| 94 | * @throws \Throwable |
||
| 95 | 2 | * @throws \Exception |
|
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | public function __toString() |
||
| 102 | |||
| 103 | 62 | /** |
|
| 104 | * Assign or get template data. |
||
| 105 | 62 | * @param array $data |
|
| 106 | 2 | * @return mixed |
|
| 107 | */ |
||
| 108 | public function data(array $data = null) |
||
| 116 | 54 | ||
| 117 | /** |
||
| 118 | 54 | * Check if the template exists. |
|
| 119 | * @return boolean |
||
| 120 | */ |
||
| 121 | public function exists() |
||
| 125 | 52 | ||
| 126 | /** |
||
| 127 | 52 | * Get the template path. |
|
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | public function path() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Render the template and layout. |
||
| 137 | 50 | * @param array $data |
|
| 138 | * @throws \Throwable |
||
| 139 | 50 | * @throws \Exception |
|
| 140 | 50 | * @return string |
|
| 141 | 50 | */ |
|
| 142 | public function render(array $data = array()) |
||
| 183 | |||
| 184 | /** |
||
| 185 | 12 | * Set the template's layout. |
|
| 186 | * @param string $name |
||
| 187 | 12 | * @param array $data |
|
| 188 | 12 | * @return null |
|
| 189 | 12 | */ |
|
| 190 | public function layout($name, array $data = array()) |
||
| 195 | |||
| 196 | 12 | /** |
|
| 197 | * Start a new section block. |
||
| 198 | 12 | * @param string $name |
|
| 199 | 2 | * @return null |
|
| 200 | */ |
||
| 201 | 2 | public function start($name) |
|
| 217 | |||
| 218 | 4 | /** |
|
| 219 | * Start a new section block in APPEND mode. |
||
| 220 | 4 | * @param string $name |
|
| 221 | * @return null |
||
| 222 | 4 | */ |
|
| 223 | 4 | public function push($name) |
|
| 228 | |||
| 229 | 10 | /** |
|
| 230 | * Start a new section block in PREPEND mode. |
||
| 231 | 10 | * @param string $name |
|
| 232 | 2 | * @return null |
|
| 233 | */ |
||
| 234 | 2 | public function unshift($name) |
|
| 239 | 8 | ||
| 240 | /** |
||
| 241 | 8 | * Stop the current section block. |
|
| 242 | 8 | * @return null |
|
| 243 | 8 | */ |
|
| 244 | 8 | public function stop() |
|
| 274 | |||
| 275 | /** |
||
| 276 | 2 | * Alias of stop(). |
|
| 277 | * @return null |
||
| 278 | 2 | */ |
|
| 279 | public function end() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Returns the content for a section block. |
||
| 286 | * @param string $name Section name |
||
| 287 | 2 | * @param string $default Default section content |
|
| 288 | * @return string|null |
||
| 289 | 2 | */ |
|
| 290 | 2 | public function section($name, $default = null) |
|
| 298 | 6 | ||
| 299 | /** |
||
| 300 | 6 | * Fetch a rendered template. |
|
| 301 | 6 | * @param string $name |
|
| 302 | 2 | * @param array $data |
|
| 303 | 6 | * @return string |
|
| 304 | 4 | */ |
|
| 305 | 4 | public function fetch($name, array $data = array()) |
|
| 309 | |||
| 310 | 4 | /** |
|
| 311 | * Output a rendered template. |
||
| 312 | 4 | * @param string $name |
|
| 313 | * @param array $data |
||
| 314 | * @return null |
||
| 315 | */ |
||
| 316 | public function insert($name, array $data = array()) |
||
| 320 | |||
| 321 | 6 | /** |
|
| 322 | * Apply multiple functions to variable. |
||
| 323 | 6 | * @param mixed $var |
|
| 324 | * @param string $functions |
||
| 325 | 6 | * @return mixed |
|
| 326 | 2 | */ |
|
| 327 | 2 | public function batch($var, $functions) |
|
| 343 | |||
| 344 | 2 | /** |
|
| 345 | * Escape string. |
||
| 346 | * @param string $string |
||
| 347 | * @param null|string $functions |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | public function escape($string, $functions = null) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Alias to escape function. |
||
| 367 | * @param string $string |
||
| 368 | * @param null|string $functions |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | public function e($string, $functions = null) |
||
| 375 | } |
||
| 376 |