Complex classes like Html 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 Html, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Html |
||
22 | { |
||
23 | /** @var \Illuminate\Http\Request */ |
||
24 | protected $request; |
||
25 | |||
26 | /** @var \ArrayAccess|array */ |
||
27 | protected $model; |
||
28 | |||
29 | public function __construct(Request $request) |
||
33 | |||
34 | /** |
||
35 | * @param string $href |
||
36 | * @param string $text |
||
37 | * |
||
38 | * @return \Spatie\Html\Elements\A |
||
39 | */ |
||
40 | public function a(?string $href = '', ?string $text = '') |
||
46 | |||
47 | /** |
||
48 | * @param string $type |
||
49 | * @param string $text |
||
50 | * |
||
51 | * @return \Spatie\Html\Elements\Button |
||
52 | */ |
||
53 | public function button(?string $text = '', ?string $type = 'button') |
||
59 | |||
60 | /** |
||
61 | * @param string $value |
||
62 | * |
||
63 | * @return \Spatie\Html\Elements\Input |
||
64 | */ |
||
65 | public function checkbox(string $name = '', ?bool $checked = false, ?string $value = '1') |
||
70 | |||
71 | /** |
||
72 | * @param \Spatie\Html\HtmlElement|string $contents |
||
73 | * |
||
74 | * @return \Spatie\Html\Elements\Div |
||
75 | */ |
||
76 | public function div($contents = null) |
||
80 | |||
81 | /** |
||
82 | * @param string $value |
||
83 | * |
||
84 | * @return \Spatie\Html\Elements\Input |
||
85 | */ |
||
86 | public function email(string $name = '', ?string $value = '') |
||
90 | |||
91 | /** |
||
92 | * @param string $tag |
||
93 | * |
||
94 | * @return \Spatie\Html\Elements\Element |
||
95 | */ |
||
96 | public function element(string $tag) |
||
100 | |||
101 | /** |
||
102 | * @param string $type |
||
103 | * @param string $name |
||
104 | * @param string $value |
||
105 | * |
||
106 | * @return \Spatie\Html\Elements\Input |
||
107 | */ |
||
108 | public function input(?string $type = '', string $name = '', ?string $value = '') |
||
116 | |||
117 | /** |
||
118 | * @param \Spatie\Html\HtmlElement|string $legend |
||
119 | * |
||
120 | * @return \Spatie\Html\Elements\Fieldset |
||
121 | */ |
||
122 | public function fieldset($legend = null) |
||
128 | |||
129 | /** |
||
130 | * @param string $method |
||
131 | * @param string $action |
||
132 | * @param array $parameters |
||
133 | * |
||
134 | * @return \Spatie\Html\Elements\Form |
||
135 | */ |
||
136 | public function form(string $method = 'POST', string $action = '') |
||
156 | |||
157 | /** |
||
158 | * @param string $name |
||
159 | * @param string $value |
||
160 | * |
||
161 | * @return \Spatie\Html\Elements\Input |
||
162 | */ |
||
163 | public function hidden(string $name = '', ?string $value = '') |
||
167 | |||
168 | /** |
||
169 | * @param \Spatie\Html\HtmlElement|iterable|string|null $contents |
||
170 | * @param string $for |
||
171 | * |
||
172 | * @return \Spatie\Html\Elements\Label |
||
173 | */ |
||
174 | public function label($contents = null, string $for = '') |
||
180 | |||
181 | /** |
||
182 | * @param \Spatie\Html\HtmlElement|string $contents |
||
183 | * |
||
184 | * @return \Spatie\Html\Elements\Legend |
||
185 | */ |
||
186 | public function legend($contents = null) |
||
190 | |||
191 | /** |
||
192 | * @param string $email |
||
193 | * @param string $text |
||
194 | * |
||
195 | * @return \Spatie\Html\Elements\A |
||
196 | */ |
||
197 | public function mailto(string $email, string $text = '') |
||
201 | |||
202 | /** |
||
203 | * @param string $text |
||
204 | * @param string $value |
||
205 | * @param bool $selected |
||
206 | * |
||
207 | * @return \Spatie\Html\Elements\Option |
||
208 | */ |
||
209 | public function option(?string $text = '', ?string $value = '', $selected = false) |
||
216 | |||
217 | /** |
||
218 | * @param string $value |
||
219 | * |
||
220 | * @return \Spatie\Html\Elements\Input |
||
221 | */ |
||
222 | public function password(string $name = '') |
||
226 | |||
227 | /** |
||
228 | * @param string $value |
||
229 | * |
||
230 | * @return \Spatie\Html\Elements\Input |
||
231 | */ |
||
232 | public function radio(string $name = '', ?bool $checked = false, ?string $value = '') |
||
237 | |||
238 | /** |
||
239 | * @param string $name |
||
240 | * @param iterable $options |
||
241 | * @param string $value |
||
242 | * |
||
243 | * @return \Spatie\Html\Elements\Select |
||
244 | */ |
||
245 | public function select(string $name = '', iterable $options = [], ?string $value = '') |
||
253 | |||
254 | /** |
||
255 | * @param \Spatie\Html\HtmlElement|string $contents |
||
256 | * |
||
257 | * @return \Spatie\Html\Elements\Span |
||
258 | */ |
||
259 | public function span($contents = null) |
||
263 | |||
264 | /** |
||
265 | * @param string $value |
||
266 | * |
||
267 | * @return \Spatie\Html\Elements\Button |
||
268 | */ |
||
269 | public function submit(?string $text = '') |
||
273 | |||
274 | /** |
||
275 | * @param string $number |
||
276 | * @param string $text |
||
277 | * |
||
278 | * @return \Spatie\Html\Elements\A |
||
279 | */ |
||
280 | public function tel(?string $number, ?string $text = '') |
||
284 | |||
285 | /** |
||
286 | * @param string $name |
||
287 | * @param string $value |
||
288 | * |
||
289 | * @return \Spatie\Html\Elements\Input |
||
290 | */ |
||
291 | public function text(string $name = '', ?string $value = '') |
||
295 | |||
296 | /** |
||
297 | * @param string $name |
||
298 | * @param string $value |
||
299 | * |
||
300 | * @return \Spatie\Html\Elements\Textarea |
||
301 | */ |
||
302 | public function textarea(string $name = '', ?string $value = '') |
||
309 | |||
310 | /** |
||
311 | * @return \Spatie\Html\Elements\Input |
||
312 | */ |
||
313 | public function token() |
||
317 | |||
318 | /** |
||
319 | * @param \ArrayAccess|array $model |
||
320 | * |
||
321 | * @return $this |
||
322 | */ |
||
323 | public function model($model) |
||
329 | |||
330 | /** |
||
331 | * @param \ArrayAccess|array $model |
||
332 | * @param string $method |
||
333 | * @param string $action |
||
334 | * |
||
335 | * @return \Spatie\Html\Elements\Form |
||
336 | */ |
||
337 | public function modelForm($model, string $method = 'POST', string $action = ''): Form |
||
343 | |||
344 | /** |
||
345 | * @return $this |
||
346 | */ |
||
347 | public function endModel() |
||
353 | |||
354 | /** |
||
355 | * @return \Illuminate\Contracts\Support\Htmlable |
||
356 | */ |
||
357 | public function closeModelForm(): Htmlable |
||
363 | |||
364 | /** |
||
365 | * @param string $name |
||
366 | * |
||
367 | * @return mixed |
||
368 | */ |
||
369 | protected function old(string $name, ?string $value = '') |
||
383 | |||
384 | protected function fieldName(string $name): string |
||
388 | |||
389 | protected function ensureModelIsAvailable() |
||
395 | } |
||
396 |