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 | * Parse structures |
||
22 | */ |
||
23 | |||
24 | private function parseStructures() { |
||
45 | |||
46 | /** |
||
47 | * Parse elementaries |
||
48 | */ |
||
49 | |||
50 | private function parseElementaries() { |
||
68 | |||
69 | /** |
||
70 | * Build the block contents |
||
71 | */ |
||
72 | |||
73 | protected function buildContents() : string { |
||
128 | |||
129 | /** |
||
130 | * Constructor |
||
131 | */ |
||
132 | |||
133 | public function __construct(string $contents = '') { |
||
139 | |||
140 | /** |
||
141 | * Cloner |
||
142 | */ |
||
143 | |||
144 | public function __clone() { |
||
152 | |||
153 | /** |
||
154 | * Set a block, a loop, or a variable |
||
155 | * |
||
156 | * @return the current block object |
||
157 | */ |
||
158 | |||
159 | public function set(string $name, $value) : Block { |
||
171 | |||
172 | /** |
||
173 | * Set multiple components (blocks, loops, or variables) |
||
174 | * |
||
175 | * @return the current block object |
||
176 | */ |
||
177 | |||
178 | public function setArray(array $components) : Block { |
||
184 | |||
185 | /** |
||
186 | * Set a block |
||
187 | * |
||
188 | * @return the current block object |
||
189 | */ |
||
190 | |||
191 | public function setBlock(string $name, Block $block) : Block { |
||
197 | |||
198 | /** |
||
199 | * Set a loop |
||
200 | * |
||
201 | * @return the current block object |
||
202 | */ |
||
203 | |||
204 | public function setLoop(string $name, array $items) : Block { |
||
210 | |||
211 | /** |
||
212 | * Set a variable |
||
213 | * |
||
214 | * @return the current block object |
||
215 | */ |
||
216 | |||
217 | public function setVar(string $name, string $value) : Block { |
||
223 | |||
224 | /** |
||
225 | * Get a block, a loop, or a variable |
||
226 | * |
||
227 | * @return the component or false if the component with the given name does not exist |
||
228 | */ |
||
229 | |||
230 | public function get(string $name) { |
||
234 | |||
235 | /** |
||
236 | * Get a block. It's recommended to use this method instead of magic getter to avoid an error on getting nonexistent block |
||
237 | * |
||
238 | * @return the block or an empty block if the block with the given name does not exist |
||
239 | */ |
||
240 | |||
241 | public function getBlock(string $name) : Block { |
||
245 | |||
246 | /** |
||
247 | * Get the blocks list |
||
248 | */ |
||
249 | |||
250 | public function getBlocks() : array { |
||
254 | |||
255 | /** |
||
256 | * Get a loop. It's recommended to use this method instead of magic getter to avoid an error on getting nonexistent loop |
||
257 | * |
||
258 | * @return the loop or an empty loop if the loop with the given name does not exist |
||
259 | */ |
||
260 | |||
261 | public function getLoop(string $name) : Loop { |
||
265 | |||
266 | /** |
||
267 | * Get the loops list |
||
268 | */ |
||
269 | |||
270 | public function getLoops() : array { |
||
274 | |||
275 | /** |
||
276 | * Get a variable |
||
277 | * |
||
278 | * @return the value or false if the variable with the given name does not exist |
||
279 | */ |
||
280 | |||
281 | public function getVar(string $name) { |
||
285 | |||
286 | /** |
||
287 | * Get the variable list |
||
288 | */ |
||
289 | |||
290 | public function getVars() : array { |
||
294 | |||
295 | /** |
||
296 | * Get the block contents |
||
297 | */ |
||
298 | |||
299 | public function getContents() : string { |
||
319 | |||
320 | /** |
||
321 | * Disable the block |
||
322 | * |
||
323 | * @return the current block object |
||
324 | */ |
||
325 | |||
326 | public function disable() : Block { |
||
332 | |||
333 | /** |
||
334 | * Enable the block |
||
335 | * |
||
336 | * @return the current block object |
||
337 | */ |
||
338 | |||
339 | public function enable() : Block { |
||
345 | |||
346 | /** |
||
347 | * Check if the block is enabled |
||
348 | */ |
||
349 | |||
350 | public function isEnabled() : bool { |
||
354 | |||
355 | /** |
||
356 | * An alias for the set method |
||
357 | */ |
||
358 | |||
359 | public function __set(string $name, $value) : Block { |
||
363 | |||
364 | /** |
||
365 | * An alias for the get method |
||
366 | */ |
||
367 | |||
368 | public function __get(string $name) { |
||
372 | |||
373 | /** |
||
374 | * Check if a component exists |
||
375 | */ |
||
376 | |||
377 | public function __isset(string $name) : bool { |
||
381 | } |
||
382 | } |
||
383 |