Complex classes like AbstractElement 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 AbstractElement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | abstract class AbstractElement |
||
9 | { |
||
10 | /** |
||
11 | * @var string $id Значение атрибута ID элемента |
||
12 | */ |
||
13 | protected $id; |
||
14 | |||
15 | /** |
||
16 | * @var string $name Значение атрибута name элемента |
||
17 | */ |
||
18 | protected $name; |
||
19 | |||
20 | /** |
||
21 | * @var Collection|iterable $classes Коллекция классом элемента |
||
22 | */ |
||
23 | protected $classes; |
||
24 | |||
25 | /** |
||
26 | * @var Collection|iterable $attributes Коллекция дополнительных атрибутов элемента |
||
27 | */ |
||
28 | protected $attributes; |
||
29 | |||
30 | /** |
||
31 | * @var Collection|iterable $styles Коллекция inline-стилей элемента |
||
32 | */ |
||
33 | protected $styles; |
||
34 | |||
35 | /** |
||
36 | * @var string $value Значение элемента |
||
37 | */ |
||
38 | protected $value; |
||
39 | |||
40 | /** |
||
41 | * @var string $tag Тег элемента |
||
42 | */ |
||
43 | protected $tag; |
||
44 | |||
45 | /** |
||
46 | * @var Collection|iterable $parts Набор атрибутов для генерации html-кода элементов |
||
47 | */ |
||
48 | protected $parts; |
||
49 | |||
50 | /** |
||
51 | * @var bool $needClose Признак, что надо закрывать тег элемента |
||
52 | */ |
||
53 | protected $needClose = false; |
||
54 | |||
55 | /** |
||
56 | * @return void |
||
57 | */ |
||
58 | abstract public function tag(); |
||
59 | |||
60 | public function __construct() |
||
72 | |||
73 | /** |
||
74 | * Сеттер значения элемента |
||
75 | * |
||
76 | * @param string|null $value |
||
77 | * @return AbstractElement |
||
78 | */ |
||
79 | public function value(?string $value): self |
||
85 | |||
86 | /** |
||
87 | * Сеттер атрибута id элемента |
||
88 | * |
||
89 | * @param string|null $value |
||
90 | * @return AbstractElement |
||
91 | */ |
||
92 | public function id(?string $value): self |
||
98 | |||
99 | /** |
||
100 | * Сеттер атрибута name элемента |
||
101 | * |
||
102 | * @param string|null $value |
||
103 | * @return AbstractElement |
||
104 | */ |
||
105 | public function name(?string $value): self |
||
111 | |||
112 | /** |
||
113 | * Добавить элементу один или несколько классов |
||
114 | * |
||
115 | * @param array ...$classes |
||
116 | * @return AbstractElement |
||
117 | */ |
||
118 | public function class(...$classes): self |
||
124 | |||
125 | /** |
||
126 | * Добавить элементу один или несколько классов из массива |
||
127 | * |
||
128 | * @param iterable $classes |
||
129 | * @return AbstractElement |
||
130 | */ |
||
131 | public function classes(iterable $classes): self |
||
141 | |||
142 | /** |
||
143 | * Добавить к элементу один или несколько inline-стилей |
||
144 | * |
||
145 | * @param string $key |
||
146 | * @param string|null $value |
||
147 | * @return AbstractElement |
||
148 | */ |
||
149 | public function style(string $key, string $value = null): self |
||
166 | |||
167 | /** |
||
168 | * Добавить к элементу один или несколько inline-стилей из массива |
||
169 | * |
||
170 | * @param iterable $styles |
||
171 | * @return AbstractElement |
||
172 | */ |
||
173 | public function styles(iterable $styles): self |
||
181 | |||
182 | /** |
||
183 | * Добавить аттрибут к элементу |
||
184 | * |
||
185 | * @param string $key |
||
186 | * @param string|null $value |
||
187 | * @return AbstractElement |
||
188 | */ |
||
189 | public function attr(string $key, string $value = null): self |
||
195 | |||
196 | /** |
||
197 | * Добавить один или несколько атрибутов из массива к элементу |
||
198 | * |
||
199 | * @param iterable $attributes |
||
200 | * @return AbstractElement |
||
201 | */ |
||
202 | public function attrs(iterable $attributes): self |
||
216 | |||
217 | /** |
||
218 | * Очистить классы элемента |
||
219 | * |
||
220 | * @return AbstractElement |
||
221 | */ |
||
222 | public function clearClasses(): self |
||
228 | |||
229 | /** |
||
230 | * Очистить классы стили |
||
231 | * |
||
232 | * @return AbstractElement |
||
233 | */ |
||
234 | public function clearStyles(): self |
||
240 | |||
241 | /** |
||
242 | * Очистить классы атрибуты |
||
243 | * |
||
244 | * @return AbstractElement |
||
245 | */ |
||
246 | public function clearAttributes(): self |
||
252 | |||
253 | /** |
||
254 | * Преобразовать в строку для вывода в HTML |
||
255 | * |
||
256 | * @return string |
||
257 | */ |
||
258 | public function __toString() |
||
305 | |||
306 | protected function addAttr(...$names): self |
||
316 | |||
317 | protected function addAttrAs(...$names): self |
||
327 | } |