Complex classes like Block 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 Block, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Block extends Group { |
||
15 | |||
16 | private $contents = '', $enabled = true; |
||
17 | |||
18 | private $blocks = [], $loops = [], $widgets = [], $variables = [], $phrases = []; |
||
19 | |||
20 | /** |
||
21 | * Set an empty template widget if was not set before |
||
22 | */ |
||
23 | |||
24 | private function registerWidget(string $name) { |
||
28 | |||
29 | /** |
||
30 | * Parse structures |
||
31 | */ |
||
32 | |||
33 | private function parseStructures() { |
||
54 | |||
55 | /** |
||
56 | * Parse elementaries |
||
57 | */ |
||
58 | |||
59 | private function parseElementaries() { |
||
77 | |||
78 | /** |
||
79 | * Build the block contents |
||
80 | */ |
||
81 | |||
82 | protected function buildContents() : string { |
||
137 | |||
138 | /** |
||
139 | * Constructor |
||
140 | */ |
||
141 | |||
142 | public function __construct(string $contents = '') { |
||
148 | |||
149 | /** |
||
150 | * Cloner |
||
151 | */ |
||
152 | |||
153 | public function __clone() { |
||
161 | |||
162 | /** |
||
163 | * Set a block, a loop, or a variable |
||
164 | * |
||
165 | * @return Template\Block : the current block object |
||
166 | */ |
||
167 | |||
168 | public function set(string $name, $component) : Block { |
||
180 | |||
181 | /** |
||
182 | * Set multiple components (blocks, loops, or variables) |
||
183 | * |
||
184 | * @return Template\Block : the current block object |
||
185 | */ |
||
186 | |||
187 | public function setArray(array $components) : Block { |
||
193 | |||
194 | /** |
||
195 | * Set a block |
||
196 | * |
||
197 | * @return Template\Block : the current block object |
||
198 | */ |
||
199 | |||
200 | public function setBlock(string $name, Block $block) : Block { |
||
206 | |||
207 | /** |
||
208 | * Set a loop |
||
209 | * |
||
210 | * @return Template\Block : the current block object |
||
211 | */ |
||
212 | |||
213 | public function setLoop(string $name, array $items) : Block { |
||
219 | |||
220 | /** |
||
221 | * Set a variable |
||
222 | * |
||
223 | * @return Template\Block : the current block object |
||
224 | */ |
||
225 | |||
226 | public function setVar(string $name, string $value) : Block { |
||
232 | |||
233 | /** |
||
234 | * Get a block, a loop, or a variable |
||
235 | * |
||
236 | * @return Template\Block|Template\Loop|string|false : the component or false if the component does not exist |
||
237 | */ |
||
238 | |||
239 | public function get(string $name) { |
||
243 | |||
244 | /** |
||
245 | * Get a block. It's recommended to use this method instead of magic getter to avoid an error on getting nonexistent block |
||
246 | * |
||
247 | * @return Template\Block : the block or an empty block if the block does not exist |
||
248 | */ |
||
249 | |||
250 | public function getBlock(string $name) : Block { |
||
254 | |||
255 | /** |
||
256 | * Get the blocks list |
||
257 | */ |
||
258 | |||
259 | public function getBlocks() : array { |
||
263 | |||
264 | /** |
||
265 | * Get a loop. It's recommended to use this method instead of magic getter to avoid an error on getting nonexistent loop |
||
266 | * |
||
267 | * @return Template\Loop : the loop or an empty loop if the loop does not exist |
||
268 | */ |
||
269 | |||
270 | public function getLoop(string $name) : Loop { |
||
274 | |||
275 | /** |
||
276 | * Get the loops list |
||
277 | */ |
||
278 | |||
279 | public function getLoops() : array { |
||
283 | |||
284 | /** |
||
285 | * Get a variable |
||
286 | * |
||
287 | * @return string|false : the value or false if the variable does not exist |
||
288 | */ |
||
289 | |||
290 | public function getVar(string $name) { |
||
294 | |||
295 | /** |
||
296 | * Get the variable list |
||
297 | */ |
||
298 | |||
299 | public function getVars() : array { |
||
303 | |||
304 | /** |
||
305 | * Get the block contents |
||
306 | */ |
||
307 | |||
308 | public function getContents() : string { |
||
328 | |||
329 | /** |
||
330 | * Disable the block |
||
331 | * |
||
332 | * @return Template\Block : the current block object |
||
333 | */ |
||
334 | |||
335 | public function disable() : Block { |
||
341 | |||
342 | /** |
||
343 | * Enable the block |
||
344 | * |
||
345 | * @return Template\Block : the current block object |
||
346 | */ |
||
347 | |||
348 | public function enable() : Block { |
||
354 | |||
355 | /** |
||
356 | * Check if the block is enabled |
||
357 | */ |
||
358 | |||
359 | public function isEnabled() : bool { |
||
363 | |||
364 | /** |
||
365 | * An alias for the set method |
||
366 | */ |
||
367 | |||
368 | public function __set(string $name, $component) : Block { |
||
372 | |||
373 | /** |
||
374 | * An alias for the get method |
||
375 | */ |
||
376 | |||
377 | public function __get(string $name) { |
||
381 | |||
382 | /** |
||
383 | * Check if a component exists |
||
384 | */ |
||
385 | |||
386 | public function __isset(string $name) : bool { |
||
390 | } |
||
391 | } |
||
392 |