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 = []) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param \Spatie\Html\HtmlElement|string $contents |
||
| 297 | * |
||
| 298 | * @return \Spatie\Html\Elements\Span |
||
| 299 | */ |
||
| 300 | public function span($contents = null) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param string $value |
||
| 307 | * |
||
| 308 | * @return \Spatie\Html\Elements\Button |
||
| 309 | */ |
||
| 310 | public function submit(?string $text = '') |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param string $number |
||
| 317 | * @param string $text |
||
| 318 | * |
||
| 319 | * @return \Spatie\Html\Elements\A |
||
| 320 | */ |
||
| 321 | public function tel(?string $number, ?string $text = '') |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param string $name |
||
| 328 | * @param string $value |
||
| 329 | * |
||
| 330 | * @return \Spatie\Html\Elements\Input |
||
| 331 | */ |
||
| 332 | public function text(string $name = '', ?string $value = '') |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param string $name |
||
| 339 | * @param string $value |
||
| 340 | * |
||
| 341 | * @return \Spatie\Html\Elements\Textarea |
||
| 342 | */ |
||
| 343 | public function textarea(string $name = '', ?string $value = '') |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @return \Spatie\Html\Elements\Input |
||
| 353 | */ |
||
| 354 | public function token() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @param \ArrayAccess|array $model |
||
| 361 | * |
||
| 362 | * @return $this |
||
| 363 | */ |
||
| 364 | public function model($model) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @param \ArrayAccess|array $model |
||
| 373 | * @param string $method |
||
| 374 | * @param string $action |
||
| 375 | * |
||
| 376 | * @return \Spatie\Html\Elements\Form |
||
| 377 | */ |
||
| 378 | public function modelForm($model, string $method = 'POST', string $action = ''): Form |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @return $this |
||
| 387 | */ |
||
| 388 | public function endModel() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @return \Illuminate\Contracts\Support\Htmlable |
||
| 397 | */ |
||
| 398 | public function closeModelForm(): Htmlable |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @param string $name |
||
| 407 | * |
||
| 408 | * @return mixed |
||
| 409 | */ |
||
| 410 | protected function old(string $name, ?string $value = '') |
||
| 424 | |||
| 425 | protected function fieldName(string $name): string |
||
| 429 | |||
| 430 | protected function ensureModelIsAvailable() |
||
| 436 | } |
||
| 437 |