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 |
||
23 | class Html |
||
24 | { |
||
25 | /** @var \Illuminate\Http\Request */ |
||
26 | protected $request; |
||
27 | |||
28 | /** @var \ArrayAccess|array */ |
||
29 | protected $model; |
||
30 | |||
31 | public function __construct(Request $request) |
||
35 | |||
36 | /** |
||
37 | * @param string $href |
||
38 | * @param string $text |
||
39 | * |
||
40 | * @return \Spatie\Html\Elements\A |
||
41 | */ |
||
42 | public function a(?string $href = '', ?string $contents = '') |
||
48 | |||
49 | /** |
||
50 | * @param string $type |
||
51 | * @param string $text |
||
52 | * |
||
53 | * @return \Spatie\Html\Elements\Button |
||
54 | */ |
||
55 | public function button(?string $contents = '', ?string $type = '') |
||
61 | |||
62 | /** |
||
63 | * @param string|array|\Illuminate\Support\Collection $classes |
||
64 | * |
||
65 | * @return \Illuminate\Contracts\Support\Htmlable |
||
66 | */ |
||
67 | public function class($classes): Htmlable |
||
80 | |||
81 | /** |
||
82 | * @param string $value |
||
83 | * |
||
84 | * @return \Spatie\Html\Elements\Input |
||
85 | */ |
||
86 | public function checkbox(string $name = '', ?bool $checked = false, ?string $value = '1') |
||
91 | |||
92 | /** |
||
93 | * @param \Spatie\Html\HtmlElement|string $contents |
||
94 | * |
||
95 | * @return \Spatie\Html\Elements\Div |
||
96 | */ |
||
97 | public function div($contents = null) |
||
101 | |||
102 | /** |
||
103 | * @param string $value |
||
104 | * |
||
105 | * @return \Spatie\Html\Elements\Input |
||
106 | */ |
||
107 | public function email(string $name = '', ?string $value = '') |
||
111 | |||
112 | /** |
||
113 | * @param string $tag |
||
114 | * |
||
115 | * @return \Spatie\Html\Elements\Element |
||
116 | */ |
||
117 | public function element(string $tag) |
||
121 | |||
122 | /** |
||
123 | * @param string $type |
||
124 | * @param string $name |
||
125 | * @param string $value |
||
126 | * |
||
127 | * @return \Spatie\Html\Elements\Input |
||
128 | */ |
||
129 | public function input(?string $type = '', string $name = '', ?string $value = '') |
||
137 | |||
138 | /** |
||
139 | * @param \Spatie\Html\HtmlElement|string $legend |
||
140 | * |
||
141 | * @return \Spatie\Html\Elements\Fieldset |
||
142 | */ |
||
143 | public function fieldset($legend = null) |
||
149 | |||
150 | /** |
||
151 | * @param string $method |
||
152 | * @param string $action |
||
153 | * @param array $parameters |
||
154 | * |
||
155 | * @return \Spatie\Html\Elements\Form |
||
156 | */ |
||
157 | public function form(string $method = 'POST', string $action = '') |
||
177 | |||
178 | /** |
||
179 | * @param string $name |
||
180 | * @param string $value |
||
181 | * |
||
182 | * @return \Spatie\Html\Elements\Input |
||
183 | */ |
||
184 | public function hidden(string $name = '', ?string $value = '') |
||
188 | |||
189 | /** |
||
190 | * @param \Spatie\Html\HtmlElement|iterable|string|null $contents |
||
191 | * @param string $for |
||
192 | * |
||
193 | * @return \Spatie\Html\Elements\Label |
||
194 | */ |
||
195 | public function label($contents = null, string $for = '') |
||
201 | |||
202 | /** |
||
203 | * @param \Spatie\Html\HtmlElement|string $contents |
||
204 | * |
||
205 | * @return \Spatie\Html\Elements\Legend |
||
206 | */ |
||
207 | public function legend($contents = null) |
||
211 | |||
212 | /** |
||
213 | * @param string $email |
||
214 | * @param string $text |
||
215 | * |
||
216 | * @return \Spatie\Html\Elements\A |
||
217 | */ |
||
218 | public function mailto(string $email, string $text = '') |
||
222 | |||
223 | /** |
||
224 | * @param string $text |
||
225 | * @param string $value |
||
226 | * @param bool $selected |
||
227 | * |
||
228 | * @return \Spatie\Html\Elements\Option |
||
229 | */ |
||
230 | public function option(?string $text = '', ?string $value = '', $selected = false) |
||
237 | |||
238 | /** |
||
239 | * @param string $value |
||
240 | * |
||
241 | * @return \Spatie\Html\Elements\Input |
||
242 | */ |
||
243 | public function password(string $name = '') |
||
247 | |||
248 | /** |
||
249 | * @param string $value |
||
250 | * |
||
251 | * @return \Spatie\Html\Elements\Input |
||
252 | */ |
||
253 | public function radio(string $name = '', ?bool $checked = false, ?string $value = '') |
||
258 | |||
259 | /** |
||
260 | * @param string $name |
||
261 | * @param iterable $options |
||
262 | * @param string $value |
||
263 | * |
||
264 | * @return \Spatie\Html\Elements\Select |
||
265 | */ |
||
266 | public function select(string $name = '', iterable $options = [], ?string $value = '') |
||
274 | |||
275 | /** |
||
276 | * @param string $name |
||
277 | * @param iterable $options |
||
278 | * @param iterable $values |
||
279 | * @return \Spatie\Html\Elements\Select |
||
280 | */ |
||
281 | public function multiselect(string $name = '', iterable $options = [], iterable $values = []) |
||
296 | |||
297 | /** |
||
298 | * @param \Spatie\Html\HtmlElement|string $contents |
||
299 | * |
||
300 | * @return \Spatie\Html\Elements\Span |
||
301 | */ |
||
302 | public function span($contents = null) |
||
306 | |||
307 | /** |
||
308 | * @param string $value |
||
309 | * |
||
310 | * @return \Spatie\Html\Elements\Button |
||
311 | */ |
||
312 | public function submit(?string $text = '') |
||
316 | |||
317 | /** |
||
318 | * @param string $number |
||
319 | * @param string $text |
||
320 | * |
||
321 | * @return \Spatie\Html\Elements\A |
||
322 | */ |
||
323 | public function tel(?string $number, ?string $text = '') |
||
327 | |||
328 | /** |
||
329 | * @param string $name |
||
330 | * @param string $value |
||
331 | * |
||
332 | * @return \Spatie\Html\Elements\Input |
||
333 | */ |
||
334 | public function text(string $name = '', ?string $value = '') |
||
338 | |||
339 | /** |
||
340 | * @param string $name |
||
341 | * @param string $value |
||
342 | * |
||
343 | * @return \Spatie\Html\Elements\Textarea |
||
344 | */ |
||
345 | public function textarea(string $name = '', ?string $value = '') |
||
352 | |||
353 | /** |
||
354 | * @return \Spatie\Html\Elements\Input |
||
355 | */ |
||
356 | public function token() |
||
360 | |||
361 | /** |
||
362 | * @param \ArrayAccess|array $model |
||
363 | * |
||
364 | * @return $this |
||
365 | */ |
||
366 | public function model($model) |
||
372 | |||
373 | /** |
||
374 | * @param \ArrayAccess|array $model |
||
375 | * @param string $method |
||
376 | * @param string $action |
||
377 | * |
||
378 | * @return \Spatie\Html\Elements\Form |
||
379 | */ |
||
380 | public function modelForm($model, string $method = 'POST', string $action = ''): Form |
||
386 | |||
387 | /** |
||
388 | * @return $this |
||
389 | */ |
||
390 | public function endModel() |
||
396 | |||
397 | /** |
||
398 | * @return \Illuminate\Contracts\Support\Htmlable |
||
399 | */ |
||
400 | public function closeModelForm(): Htmlable |
||
406 | |||
407 | /** |
||
408 | * @param string $name |
||
409 | * |
||
410 | * @return mixed |
||
411 | */ |
||
412 | protected function old(string $name, $value = '') |
||
426 | |||
427 | protected function fieldName(string $name): string |
||
431 | |||
432 | protected function ensureModelIsAvailable() |
||
438 | } |
||
439 |