Complex classes like Tag 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 Tag, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Tag |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * Items (strings) to be added before the element |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $before = array(); |
||
21 | |||
22 | /** |
||
23 | * Items (strings) to be added after the element |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $after = array(); |
||
28 | |||
29 | /** |
||
30 | * The HTML tag |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $tag = 'div'; |
||
35 | |||
36 | /** |
||
37 | * Is the element self enclosing |
||
38 | * |
||
39 | * @var bool |
||
40 | */ |
||
41 | protected $isSelfClosing = false; |
||
42 | |||
43 | /** |
||
44 | * Properties collection |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $props = array(); |
||
49 | |||
50 | /** |
||
51 | * Content of the element. |
||
52 | * Can be a string, array, object that has __toString() |
||
53 | * |
||
54 | * @var mixed |
||
55 | */ |
||
56 | protected $content = array(); |
||
57 | |||
58 | /** |
||
59 | * Parent element |
||
60 | * |
||
61 | * @var Tag |
||
62 | */ |
||
63 | protected $parent; |
||
64 | |||
65 | /** |
||
66 | * The tag builder. |
||
67 | * This is so we can attach children without having to construct them |
||
68 | * |
||
69 | * @var Builder |
||
70 | */ |
||
71 | protected $builder; |
||
72 | |||
73 | /** |
||
74 | * Factory method. |
||
75 | * If $tag ends in '/' the tag will be considered 'self-closing' |
||
76 | * |
||
77 | * @example ExtendedTag::factory('hr/', null, ['class' => 'separator']); |
||
78 | * ExtendedTag::factory('div', 'This is my content', ['class' => 'container']); |
||
79 | * |
||
80 | * @param string $tag |
||
81 | * @param array $props |
||
82 | * @param mixed $content |
||
83 | * @param Builder $builder |
||
84 | * |
||
85 | * @return Tag |
||
86 | */ |
||
87 | 5 | static public function create($tag, $props = null, $content = null, Builder $builder = null) |
|
101 | |||
102 | /** |
||
103 | * |
||
104 | * @param array $props |
||
105 | * Additional data for the HTML element (attributes, private data) |
||
106 | * @param mixed $content |
||
107 | * Content of the HTML element (a string, an array) |
||
108 | * @param Builder $builder |
||
109 | 36 | */ |
|
110 | public function __construct($props = null, $content = null, Builder $builder = null) |
||
120 | |||
121 | /** |
||
122 | * Set multipe properties to the HTML element |
||
123 | * |
||
124 | * @param array $props |
||
125 | * |
||
126 | * @return self |
||
127 | 36 | */ |
|
128 | public function setProps($props) |
||
139 | |||
140 | /** |
||
141 | * Set a single property to the HTML element |
||
142 | * |
||
143 | * @param string $name |
||
144 | * @param mixed $value |
||
145 | * |
||
146 | * @return Tag |
||
147 | 29 | */ |
|
148 | public function set($name, $value = null) |
||
161 | |||
162 | /** |
||
163 | * @param $name |
||
164 | * |
||
165 | * @return mixed |
||
166 | 29 | */ |
|
167 | protected function cleanAttributeName($name) |
||
176 | |||
177 | /** |
||
178 | * Returns some or all of the HTML element's properties |
||
179 | * |
||
180 | * @param array|null $list |
||
181 | * |
||
182 | * @return array |
||
183 | 27 | */ |
|
184 | public function getProps($list = null) |
||
197 | |||
198 | /** |
||
199 | * Returns one of HTML element's properties |
||
200 | * |
||
201 | * @param string $name |
||
202 | * |
||
203 | * @return mixed |
||
204 | 18 | */ |
|
205 | public function get($name) |
||
211 | |||
212 | /** |
||
213 | * Add a class to the element's class list |
||
214 | * |
||
215 | * @param string $class |
||
216 | * |
||
217 | * @return self |
||
218 | 4 | */ |
|
219 | public function addClass($class) |
||
227 | |||
228 | /** |
||
229 | * Remove a class from the element's class list |
||
230 | * |
||
231 | * @param string $class |
||
232 | * |
||
233 | * @return self |
||
234 | 2 | */ |
|
235 | public function removeClass($class) |
||
245 | |||
246 | /** |
||
247 | * Toggles a class on the element |
||
248 | * |
||
249 | * @param string $class |
||
250 | * |
||
251 | * @return self |
||
252 | 1 | */ |
|
253 | public function toggleClass($class) |
||
261 | |||
262 | /** |
||
263 | * Checks if the element has a specific class |
||
264 | * |
||
265 | * @param string $class |
||
266 | * |
||
267 | * @return boolean |
||
268 | 7 | */ |
|
269 | public function hasClass($class) |
||
275 | |||
276 | /** |
||
277 | * Set the content |
||
278 | * |
||
279 | * @param mixed $content |
||
280 | * |
||
281 | * @return $this |
||
282 | 24 | */ |
|
283 | public function setContent($content) |
||
298 | |||
299 | /** |
||
300 | * Get the content/children |
||
301 | * |
||
302 | * @return mixed |
||
303 | */ |
||
304 | public function getContent() |
||
308 | |||
309 | /** |
||
310 | * Empties the content of the tag |
||
311 | * |
||
312 | * @return \Sirius\Html\Tag |
||
313 | 23 | */ |
|
314 | public function clearContent() |
||
320 | |||
321 | /** |
||
322 | * @param $tagTextOrArray |
||
323 | * |
||
324 | * @return self |
||
325 | 23 | */ |
|
326 | protected function addChild($tagTextOrArray) |
||
345 | |||
346 | /** |
||
347 | * Return the attributes as a string for HTML output |
||
348 | * example: title="Click here to delete" class="remove" |
||
349 | * |
||
350 | * @return string |
||
351 | 23 | */ |
|
352 | protected function getAttributesString() |
||
371 | |||
372 | /** |
||
373 | * @param $attr |
||
374 | * |
||
375 | * @return string |
||
376 | */ |
||
377 | 23 | protected function escapeAttr($attr) |
|
392 | |||
393 | /** |
||
394 | * Render the element |
||
395 | 19 | * |
|
396 | * @return string |
||
397 | 19 | */ |
|
398 | public function render() |
||
410 | |||
411 | /** |
||
412 | * Return the innerHTML content of the tag |
||
413 | * |
||
414 | * @return string |
||
415 | */ |
||
416 | 23 | public function getInnerHtml() |
|
420 | 11 | ||
421 | 11 | public function __toString() |
|
425 | 23 | ||
426 | 23 | /** |
|
427 | * Add a string or a stringifiable object immediately before the element |
||
428 | 23 | * |
|
429 | * @param string|object $stringOrObject |
||
430 | * |
||
431 | * @return Tag |
||
432 | */ |
||
433 | public function before($stringOrObject) |
||
439 | |||
440 | /** |
||
441 | 18 | * Add a string or a stringifiable object immediately after the element |
|
442 | * |
||
443 | 18 | * @param string|object $stringOrObject |
|
444 | * |
||
445 | * @return Tag |
||
446 | */ |
||
447 | public function after($stringOrObject) |
||
453 | 1 | ||
454 | /** |
||
455 | 1 | * Add a string or a stringifiable object immediately as the first child of the element |
|
456 | * |
||
457 | 1 | * @param string|object $stringOrObject |
|
458 | * |
||
459 | * @return Tag |
||
460 | */ |
||
461 | public function prepend($stringOrObject) |
||
467 | 1 | ||
468 | /** |
||
469 | 1 | * Add a string or a stringifiable object as the last child the element |
|
470 | * |
||
471 | 1 | * @param string|object $stringOrObject |
|
472 | * |
||
473 | * @return Tag |
||
474 | */ |
||
475 | public function append($stringOrObject) |
||
481 | 1 | ||
482 | /** |
||
483 | 1 | * Add something before and after the element. |
|
484 | * Proxy for calling before() and after() simoultaneously |
||
485 | 1 | * |
|
486 | * @param string|object $before |
||
487 | * @param string|object $after |
||
488 | * |
||
489 | * @return Tag |
||
490 | */ |
||
491 | public function wrap($before, $after) |
||
495 | 1 | } |
|
496 | |||
497 |