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 | * Whether the section should be appended or not. |
||
52 | * @deprecated stayed for backward compatibility, use $sectionMode instead |
||
53 | * @var boolean |
||
54 | */ |
||
55 | protected $appendSection; |
||
56 | |||
57 | /** |
||
58 | * The name of the template layout. |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $layoutName; |
||
62 | |||
63 | /** |
||
64 | * The data assigned to the template layout. |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $layoutData; |
||
68 | 62 | ||
69 | /** |
||
70 | 62 | * Set section content mode: rewrite/append/prepend |
|
71 | 62 | * @var int |
|
72 | */ |
||
73 | 62 | protected $sectionMode = self::SECTION_MODE_REWRITE; |
|
74 | 62 | ||
75 | /** |
||
76 | * Create new Template instance. |
||
77 | * @param Engine $engine |
||
78 | * @param string $name |
||
79 | */ |
||
80 | public function __construct(Engine $engine, $name) |
||
87 | |||
88 | /** |
||
89 | * Magic method used to call extension functions. |
||
90 | * @param string $name |
||
91 | * @param array $arguments |
||
92 | * @return mixed |
||
93 | 2 | */ |
|
94 | public function __call($name, $arguments) |
||
98 | |||
99 | /** |
||
100 | * Alias for render() method. |
||
101 | * @throws \Throwable |
||
102 | * @throws \Exception |
||
103 | 62 | * @return string |
|
104 | */ |
||
105 | 62 | public function __toString() |
|
109 | 62 | ||
110 | 62 | /** |
|
111 | * Assign or get template data. |
||
112 | * @param array $data |
||
113 | * @return mixed |
||
114 | */ |
||
115 | public function data(array $data = null) |
||
123 | |||
124 | /** |
||
125 | 52 | * Check if the template exists. |
|
126 | * @return boolean |
||
127 | 52 | */ |
|
128 | public function exists() |
||
132 | |||
133 | /** |
||
134 | * Get the template path. |
||
135 | * @return string |
||
136 | */ |
||
137 | 50 | public function path() |
|
141 | 50 | ||
142 | /** |
||
143 | 50 | * Render the template and layout. |
|
144 | 2 | * @param array $data |
|
145 | 2 | * @throws \Throwable |
|
146 | 2 | * @throws \Exception |
|
147 | * @return string |
||
148 | */ |
||
149 | public function render(array $data = array()) |
||
190 | |||
191 | /** |
||
192 | * Set the template's layout. |
||
193 | * @param string $name |
||
194 | * @param array $data |
||
195 | * @return null |
||
196 | 12 | */ |
|
197 | public function layout($name, array $data = array()) |
||
202 | |||
203 | /** |
||
204 | 10 | * Start a new section block. |
|
205 | 2 | * @param string $name |
|
206 | * @return null |
||
207 | */ |
||
208 | 10 | public function start($name) |
|
224 | |||
225 | /** |
||
226 | * Start a new section block in APPEND mode. |
||
227 | * @param string $name |
||
228 | * @return null |
||
229 | 10 | */ |
|
230 | public function push($name) |
||
236 | |||
237 | 8 | /** |
|
238 | 8 | * Start a new section block in PREPEND mode. |
|
239 | 8 | * @param string $name |
|
240 | * @return null |
||
241 | 8 | */ |
|
242 | 8 | public function unshift($name) |
|
248 | |||
249 | /** |
||
250 | 4 | * Stop the current section block. |
|
251 | * @return null |
||
252 | 4 | */ |
|
253 | 4 | public function stop() |
|
284 | |||
285 | /** |
||
286 | * Alias of stop(). |
||
287 | 2 | * @return null |
|
288 | */ |
||
289 | 2 | public function end() |
|
293 | |||
294 | /** |
||
295 | * Returns the content for a section block. |
||
296 | * @param string $name Section name |
||
297 | * @param string $default Default section content |
||
298 | 6 | * @return string|null |
|
299 | */ |
||
300 | 6 | public function section($name, $default = null) |
|
308 | 2 | ||
309 | /** |
||
310 | 4 | * Fetch a rendered template. |
|
311 | * @param string $name |
||
312 | 4 | * @param array $data |
|
313 | * @return string |
||
314 | */ |
||
315 | public function fetch($name, array $data = array()) |
||
319 | |||
320 | /** |
||
321 | 6 | * Output a rendered template. |
|
322 | * @param string $name |
||
323 | 6 | * @param array $data |
|
324 | * @return null |
||
325 | 6 | */ |
|
326 | 2 | public function insert($name, array $data = array()) |
|
330 | 2 | ||
331 | 2 | /** |
|
332 | * Apply multiple functions to variable. |
||
333 | 6 | * @param mixed $var |
|
334 | * @param string $functions |
||
335 | * @return mixed |
||
336 | */ |
||
337 | public function batch($var, $functions) |
||
353 | |||
354 | /** |
||
355 | * Escape string. |
||
356 | * @param string $string |
||
357 | * @param null|string $functions |
||
358 | * @return string |
||
359 | */ |
||
360 | public function escape($string, $functions = null) |
||
374 | |||
375 | /** |
||
376 | * Alias to escape function. |
||
377 | * @param string $string |
||
378 | * @param null|string $functions |
||
379 | * @return string |
||
380 | */ |
||
381 | public function e($string, $functions = null) |
||
385 | } |
||
386 |