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 | const SECTION_MODE_REWRITE = 1; |
||
16 | const SECTION_MODE_PREPEND = 2; |
||
17 | const SECTION_MODE_APPEND = 3; |
||
18 | |||
19 | /** |
||
20 | * Set section content mode: rewrite/append/prepend |
||
21 | * @var int |
||
22 | */ |
||
23 | protected $sectionMode = self::SECTION_MODE_REWRITE; |
||
24 | |||
25 | /** |
||
26 | * Instance of the template engine. |
||
27 | * @var Engine |
||
28 | */ |
||
29 | protected $engine; |
||
30 | |||
31 | /** |
||
32 | * The name of the template. |
||
33 | * @var Name |
||
34 | */ |
||
35 | protected $name; |
||
36 | |||
37 | /** |
||
38 | * The data assigned to the template. |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $data = array(); |
||
42 | |||
43 | /** |
||
44 | * An array of section content. |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $sections = array(); |
||
48 | |||
49 | /** |
||
50 | * The name of the section currently being rendered. |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $sectionName; |
||
54 | |||
55 | /** |
||
56 | * Whether the section should be appended or not. |
||
57 | * @deprecated stayed for backward compatibility, use $sectionMode instead |
||
58 | * @var boolean |
||
59 | */ |
||
60 | protected $appendSection; |
||
61 | |||
62 | /** |
||
63 | * The name of the template layout. |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $layoutName; |
||
67 | |||
68 | 62 | /** |
|
69 | * The data assigned to the template layout. |
||
70 | 62 | * @var array |
|
71 | 62 | */ |
|
72 | protected $layoutData; |
||
73 | 62 | ||
74 | 62 | /** |
|
75 | * Create new Template instance. |
||
76 | * @param Engine $engine |
||
77 | * @param string $name |
||
78 | */ |
||
79 | public function __construct(Engine $engine, $name) |
||
86 | |||
87 | /** |
||
88 | * Magic method used to call extension functions. |
||
89 | * @param string $name |
||
90 | * @param array $arguments |
||
91 | * @return mixed |
||
92 | */ |
||
93 | 2 | public function __call($name, $arguments) |
|
97 | |||
98 | /** |
||
99 | * Alias for render() method. |
||
100 | * @throws \Throwable |
||
101 | * @throws \Exception |
||
102 | * @return string |
||
103 | 62 | */ |
|
104 | public function __toString() |
||
108 | |||
109 | 62 | /** |
|
110 | 62 | * Assign or get template data. |
|
111 | * @param array $data |
||
112 | * @return mixed |
||
113 | */ |
||
114 | public function data(array $data = null) |
||
122 | |||
123 | /** |
||
124 | * Check if the template exists. |
||
125 | 52 | * @return boolean |
|
126 | */ |
||
127 | 52 | public function exists() |
|
131 | |||
132 | /** |
||
133 | * Get the template path. |
||
134 | * @return string |
||
135 | */ |
||
136 | public function path() |
||
140 | 50 | ||
141 | 50 | /** |
|
142 | * Render the template and layout. |
||
143 | 50 | * @param array $data |
|
144 | 2 | * @throws \Throwable |
|
145 | 2 | * @throws \Exception |
|
146 | 2 | * @return string |
|
147 | */ |
||
148 | public function render(array $data = array()) |
||
189 | 12 | ||
190 | /** |
||
191 | * Set the template's layout. |
||
192 | * @param string $name |
||
193 | * @param array $data |
||
194 | * @return null |
||
195 | */ |
||
196 | 12 | public function layout($name, array $data = array()) |
|
201 | 2 | ||
202 | /** |
||
203 | * Start a new section block. |
||
204 | 10 | * @param string $name |
|
205 | 2 | * @return null |
|
206 | */ |
||
207 | public function start($name) |
||
223 | 4 | ||
224 | /** |
||
225 | * Start a new section block in APPEND mode. |
||
226 | * @param string $name |
||
227 | * @return null |
||
228 | */ |
||
229 | 10 | public function push($name) |
|
235 | |||
236 | /** |
||
237 | 8 | * Start a new section block in PREPEND mode. |
|
238 | 8 | * @param string $name |
|
239 | 8 | * @return null |
|
240 | */ |
||
241 | 8 | public function unshift($name) |
|
247 | |||
248 | /** |
||
249 | * Stop the current section block. |
||
250 | 4 | * @return null |
|
251 | */ |
||
252 | 4 | public function stop() |
|
283 | |||
284 | /** |
||
285 | * Alias of stop(). |
||
286 | * @return null |
||
287 | 2 | */ |
|
288 | public function end() |
||
292 | |||
293 | /** |
||
294 | * Returns the content for a section block. |
||
295 | * @param string $name Section name |
||
296 | * @param string $default Default section content |
||
297 | * @return string|null |
||
298 | 6 | */ |
|
299 | public function section($name, $default = null) |
||
307 | 2 | ||
308 | 2 | /** |
|
309 | * Fetch a rendered template. |
||
310 | 4 | * @param string $name |
|
311 | * @param array $data |
||
312 | 4 | * @return string |
|
313 | */ |
||
314 | public function fetch($name, array $data = array()) |
||
318 | |||
319 | /** |
||
320 | * Output a rendered template. |
||
321 | 6 | * @param string $name |
|
322 | * @param array $data |
||
323 | 6 | * @return null |
|
324 | */ |
||
325 | 6 | public function insert($name, array $data = array()) |
|
329 | 6 | ||
330 | 2 | /** |
|
331 | 2 | * Apply multiple functions to variable. |
|
332 | * @param mixed $var |
||
333 | 6 | * @param string $functions |
|
334 | * @return mixed |
||
335 | */ |
||
336 | public function batch($var, $functions) |
||
352 | |||
353 | /** |
||
354 | * Escape string. |
||
355 | * @param string $string |
||
356 | * @param null|string $functions |
||
357 | * @return string |
||
358 | */ |
||
359 | public function escape($string, $functions = null) |
||
373 | |||
374 | /** |
||
375 | * Alias to escape function. |
||
376 | * @param string $string |
||
377 | * @param null|string $functions |
||
378 | * @return string |
||
379 | */ |
||
380 | public function e($string, $functions = null) |
||
384 | } |
||
385 |