Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 27 | class Html |
||
| 28 | { |
||
| 29 | use Macroable; |
||
| 30 | |||
| 31 | /** @var \Illuminate\Http\Request */ |
||
| 32 | protected $request; |
||
| 33 | |||
| 34 | /** @var \ArrayAccess|array */ |
||
| 35 | protected $model; |
||
| 36 | |||
| 37 | public function __construct(Request $request) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param string|null $href |
||
| 44 | * @param string|null $text |
||
| 45 | * |
||
| 46 | * @return \Spatie\Html\Elements\A |
||
| 47 | */ |
||
| 48 | public function a($href = null, $contents = null) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param string|null $href |
||
| 57 | * @param string|null $text |
||
| 58 | * |
||
| 59 | * @return \Spatie\Html\Elements\I |
||
| 60 | */ |
||
| 61 | public function i($contents = null) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param string|null $type |
||
| 69 | * @param string|null $text |
||
| 70 | * |
||
| 71 | * @return \Spatie\Html\Elements\Button |
||
| 72 | */ |
||
| 73 | public function button($contents = null, $type = null, $name = '') |
||
| 74 | { |
||
| 75 | return Button::create() |
||
| 76 | ->attributeIf($type, 'type', $type) |
||
| 77 | ->attributeIf($name, 'name', $this->fieldName($name)) |
||
| 78 | ->html($contents); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param \Illuminate\Support\Collection|iterable|string $classes |
||
| 83 | * |
||
| 84 | * @return \Illuminate\Contracts\Support\Htmlable |
||
| 85 | */ |
||
| 86 | public function class($classes): Htmlable |
||
| 87 | { |
||
| 88 | if ($classes instanceof Collection) { |
||
| 89 | $classes = $classes->toArray(); |
||
| 90 | } |
||
| 91 | |||
| 92 | $attributes = new Attributes(); |
||
| 93 | $attributes->addClass($classes); |
||
| 94 | |||
| 95 | return new HtmlString( |
||
| 96 | $attributes->render() |
||
| 97 | ); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param string|null $name |
||
| 102 | * @param bool $checked |
||
| 103 | * @param string|null $value |
||
| 104 | * |
||
| 105 | * @return \Spatie\Html\Elements\Input |
||
| 106 | */ |
||
| 107 | public function checkbox($name = null, $checked = false, $value = '1') |
||
| 108 | { |
||
| 109 | return $this->input('checkbox', $name, $value) |
||
| 110 | ->attributeIf(! is_null($value), 'value', $value) |
||
| 111 | ->attributeIf((bool) $this->old($name, $checked), 'checked'); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param \Spatie\Html\HtmlElement|string|null $contents |
||
| 116 | * |
||
| 117 | * @return \Spatie\Html\Elements\Div |
||
| 118 | */ |
||
| 119 | public function div($contents = null) |
||
| 120 | { |
||
| 121 | return Div::create()->children($contents); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param string|null $name |
||
| 126 | * @param string|null $value |
||
| 127 | * |
||
| 128 | * @return \Spatie\Html\Elements\Input |
||
| 129 | */ |
||
| 130 | public function email($name = '', $value = '') |
||
| 131 | { |
||
| 132 | return $this->input('email', $name, $value); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param string|null $name |
||
| 137 | * @param string|null $value |
||
| 138 | * |
||
| 139 | * @return \Spatie\Html\Elements\Input |
||
| 140 | */ |
||
| 141 | public function date($name = '', $value = '') |
||
| 142 | { |
||
| 143 | return $this->input('date', $name, $value); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param string|null $name |
||
| 148 | * @param string|null $value |
||
| 149 | * |
||
| 150 | * @return \Spatie\Html\Elements\Input |
||
| 151 | */ |
||
| 152 | public function time($name = '', $value = '') |
||
| 153 | { |
||
| 154 | return $this->input('time', $name, $value); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param string $tag |
||
| 159 | * |
||
| 160 | * @return \Spatie\Html\Elements\Element |
||
| 161 | */ |
||
| 162 | public function element($tag) |
||
| 163 | { |
||
| 164 | return Element::withTag($tag); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param string|null $type |
||
| 169 | * @param string|null $name |
||
| 170 | * @param string|null $value |
||
| 171 | * |
||
| 172 | * @return \Spatie\Html\Elements\Input |
||
| 173 | */ |
||
| 174 | public function input($type = null, $name = null, $value = null) |
||
| 175 | { |
||
| 176 | $hasValue = $name && ($type !== 'password' && ! is_null($this->old($name, $value)) || ! is_null($value)); |
||
| 177 | |||
| 178 | return Input::create() |
||
| 179 | ->attributeIf($type, 'type', $type) |
||
| 180 | ->attributeIf($name, 'name', $this->fieldName($name)) |
||
| 181 | ->attributeIf($name, 'id', $this->fieldName($name)) |
||
| 182 | ->attributeIf($hasValue, 'value', $this->old($name, $value)); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param \Spatie\Html\HtmlElement|string|null $legend |
||
| 187 | * |
||
| 188 | * @return \Spatie\Html\Elements\Fieldset |
||
| 189 | */ |
||
| 190 | public function fieldset($legend = null) |
||
| 191 | { |
||
| 192 | return $legend ? |
||
| 193 | Fieldset::create()->legend($legend) : |
||
| 194 | Fieldset::create(); |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param string $method |
||
| 199 | * @param string|null $action |
||
| 200 | * |
||
| 201 | * @return \Spatie\Html\Elements\Form |
||
| 202 | */ |
||
| 203 | public function form($method = 'POST', $action = null) |
||
| 204 | { |
||
| 205 | $method = strtoupper($method); |
||
| 206 | $form = Form::create(); |
||
| 207 | |||
| 208 | // If Laravel needs to spoof the form's method, we'll append a hidden |
||
| 209 | // field containing the actual method |
||
| 210 | if (in_array($method, ['DELETE', 'PATCH', 'PUT'])) { |
||
| 211 | $form = $form->addChild($this->hidden('_method')->value($method)); |
||
| 212 | } |
||
| 213 | |||
| 214 | // On any other method that get, the form needs a CSRF token |
||
| 215 | if ($method !== 'GET') { |
||
| 216 | $form = $form->addChild($this->token()); |
||
| 217 | } |
||
| 218 | |||
| 219 | return $form |
||
| 220 | ->method($method === 'GET' ? 'GET' : 'POST') |
||
| 221 | ->attributeIf($action, 'action', $action); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param string|null $name |
||
| 226 | * @param string|null $value |
||
| 227 | * |
||
| 228 | * @return \Spatie\Html\Elements\Input |
||
| 229 | */ |
||
| 230 | public function hidden($name = null, $value = null) |
||
| 231 | { |
||
| 232 | return $this->input('hidden', $name, $value); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param string|null $src |
||
| 237 | * @param string|null $alt |
||
| 238 | * |
||
| 239 | * @return \Spatie\Html\Elements\Img |
||
| 240 | */ |
||
| 241 | public function img($src = null, $alt = null) |
||
| 242 | { |
||
| 243 | return Img::create() |
||
| 244 | ->attributeIf($src, 'src', $src) |
||
| 245 | ->attributeIf($alt, 'alt', $alt); |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param \Spatie\Html\HtmlElement|iterable|string|null $contents |
||
| 250 | * @param string|null $for |
||
| 251 | * |
||
| 252 | * @return \Spatie\Html\Elements\Label |
||
| 253 | */ |
||
| 254 | public function label($contents = null, $for = null) |
||
| 255 | { |
||
| 256 | return Label::create() |
||
| 257 | ->attributeIf($for, 'for', $this->fieldName($for)) |
||
| 258 | ->children($contents); |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param \Spatie\Html\HtmlElement|string|null $contents |
||
| 263 | * |
||
| 264 | * @return \Spatie\Html\Elements\Legend |
||
| 265 | */ |
||
| 266 | public function legend($contents = null) |
||
| 267 | { |
||
| 268 | return Legend::create()->html($contents); |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param string $email |
||
| 273 | * @param string|null $text |
||
| 274 | * |
||
| 275 | * @return \Spatie\Html\Elements\A |
||
| 276 | */ |
||
| 277 | public function mailto($email, $text = null) |
||
| 278 | { |
||
| 279 | return $this->a('mailto:'.$email, $text ?: $email); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param string|null $name |
||
| 284 | * @param iterable $options |
||
| 285 | * @param string|iterable|null $value |
||
| 286 | * |
||
| 287 | * @return \Spatie\Html\Elements\Select |
||
| 288 | */ |
||
| 289 | View Code Duplication | public function multiselect($name = null, $options = [], $value = null) |
|
| 290 | { |
||
| 291 | return Select::create() |
||
| 292 | ->attributeIf($name, 'name', $this->fieldName($name)) |
||
| 293 | ->attributeIf($name, 'id', $this->fieldName($name)) |
||
| 294 | ->options($options) |
||
| 295 | ->value($name ? $this->old($name, $value) : $value) |
||
| 296 | ->multiple(); |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param string|null $text |
||
| 301 | * @param string|null $value |
||
| 302 | * @param bool $selected |
||
| 303 | * |
||
| 304 | * @return \Spatie\Html\Elements\Option |
||
| 305 | */ |
||
| 306 | public function option($text = null, $value = null, $selected = false) |
||
| 307 | { |
||
| 308 | return Option::create() |
||
| 309 | ->text($text) |
||
| 310 | ->value($value) |
||
| 311 | ->selectedIf($selected); |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param string|null $value |
||
| 316 | * |
||
| 317 | * @return \Spatie\Html\Elements\Input |
||
| 318 | */ |
||
| 319 | public function password($name = null) |
||
| 320 | { |
||
| 321 | return $this->input('password', $name); |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param string|null $name |
||
| 326 | * @param bool $checked |
||
| 327 | * @param string|null $value |
||
| 328 | * |
||
| 329 | * @return \Spatie\Html\Elements\Input |
||
| 330 | */ |
||
| 331 | public function radio($name = null, $checked = false, $value = null) |
||
| 332 | { |
||
| 333 | return $this->input('radio', $name, $value) |
||
| 334 | ->attributeIf($name, 'id', $value === null ? $name : ($name.'_'.str_slug($value))) |
||
| 335 | ->attributeIf(! is_null($value), 'value', $value) |
||
| 336 | ->attributeIf((! is_null($value) && $this->old($name) == $value) || $checked, 'checked'); |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param string|null $name |
||
| 341 | * @param iterable $options |
||
| 342 | * @param string|iterable|null $value |
||
| 343 | * |
||
| 344 | * @return \Spatie\Html\Elements\Select |
||
| 345 | */ |
||
| 346 | View Code Duplication | public function select($name = null, $options = [], $value = null) |
|
| 347 | { |
||
| 348 | return Select::create() |
||
| 349 | ->attributeIf($name, 'name', $this->fieldName($name)) |
||
| 350 | ->attributeIf($name, 'id', $this->fieldName($name)) |
||
| 351 | ->options($options) |
||
| 352 | ->value($name ? $this->old($name, $value) : $value); |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param \Spatie\Html\HtmlElement|string|null $contents |
||
| 357 | * |
||
| 358 | * @return \Spatie\Html\Elements\Span |
||
| 359 | */ |
||
| 360 | public function span($contents = null) |
||
| 361 | { |
||
| 362 | return Span::create()->children($contents); |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @param string|null $text |
||
| 367 | * |
||
| 368 | * @return \Spatie\Html\Elements\Button |
||
| 369 | */ |
||
| 370 | public function submit($text = null) |
||
| 371 | { |
||
| 372 | return $this->button($text, 'submit'); |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param string|null $text |
||
| 377 | * |
||
| 378 | * @return \Spatie\Html\Elements\Button |
||
| 379 | */ |
||
| 380 | public function reset($text = null) |
||
| 381 | { |
||
| 382 | return $this->button($text, 'reset'); |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @param string $number |
||
| 387 | * @param string|null $text |
||
| 388 | * |
||
| 389 | * @return \Spatie\Html\Elements\A |
||
| 390 | */ |
||
| 391 | public function tel($number, $text = null) |
||
| 392 | { |
||
| 393 | return $this->a('tel:'.$number, $text ?: $number); |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @param string|null $name |
||
| 398 | * @param string|null $value |
||
| 399 | * |
||
| 400 | * @return \Spatie\Html\Elements\Input |
||
| 401 | */ |
||
| 402 | public function text($name = null, $value = null) |
||
| 403 | { |
||
| 404 | return $this->input('text', $name, $value); |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @param string|null $name |
||
| 409 | * |
||
| 410 | * @return \Spatie\Html\Elements\File |
||
| 411 | */ |
||
| 412 | public function file($name = null) |
||
| 413 | { |
||
| 414 | return File::create() |
||
| 415 | ->attributeIf($name, 'name', $this->fieldName($name)) |
||
| 416 | ->attributeIf($name, 'id', $this->fieldName($name)); |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param string|null $name |
||
| 421 | * @param string|null $value |
||
| 422 | * |
||
| 423 | * @return \Spatie\Html\Elements\Textarea |
||
| 424 | */ |
||
| 425 | public function textarea($name = null, $value = null) |
||
| 426 | { |
||
| 427 | return Textarea::create() |
||
| 428 | ->attributeIf($name, 'name', $this->fieldName($name)) |
||
| 429 | ->attributeIf($name, 'id', $this->fieldName($name)) |
||
| 430 | ->value($this->old($name, $value)); |
||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @return \Spatie\Html\Elements\Input |
||
| 435 | */ |
||
| 436 | public function token() |
||
| 437 | { |
||
| 438 | return $this |
||
| 439 | ->hidden() |
||
| 440 | ->name('_token') |
||
| 441 | ->value($this->request->session()->token()); |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param \ArrayAccess|array $model |
||
| 446 | * |
||
| 447 | * @return $this |
||
| 448 | */ |
||
| 449 | public function model($model) |
||
| 450 | { |
||
| 451 | $this->model = $model; |
||
| 452 | |||
| 453 | return $this; |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @param \ArrayAccess|array $model |
||
| 458 | * @param string|null $method |
||
| 459 | * @param string|null $action |
||
| 460 | * |
||
| 461 | * @return \Spatie\Html\Elements\Form |
||
| 462 | */ |
||
| 463 | public function modelForm($model, $method = 'POST', $action = null): Form |
||
| 464 | { |
||
| 465 | $this->model($model); |
||
| 466 | |||
| 467 | return $this->form($method, $action); |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @return $this |
||
| 472 | */ |
||
| 473 | public function endModel() |
||
| 474 | { |
||
| 475 | $this->model = null; |
||
| 476 | |||
| 477 | return $this; |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @return \Illuminate\Contracts\Support\Htmlable |
||
| 482 | */ |
||
| 483 | public function closeModelForm(): Htmlable |
||
| 484 | { |
||
| 485 | $this->endModel(); |
||
| 486 | |||
| 487 | return $this->form()->close(); |
||
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * @param string $name |
||
| 492 | * @param mixed $value |
||
| 493 | * |
||
| 494 | * @return mixed |
||
| 495 | */ |
||
| 496 | protected function old($name, $value = null) |
||
| 497 | { |
||
| 498 | if (empty($name)) { |
||
| 499 | return; |
||
| 500 | } |
||
| 501 | |||
| 502 | // Convert array format (sth[1]) to dot notation (sth.1) |
||
| 503 | $name = preg_replace('/\[(.+)\]/U', '.$1', $name); |
||
| 504 | |||
| 505 | // If there's no default value provided, the html builder currently |
||
| 506 | // has a model assigned and there aren't old input items, |
||
| 507 | // try to retrieve a value from the model. |
||
| 508 | if (empty($value) && $this->model && empty($this->request->old())) { |
||
| 509 | $value = data_get($this->model, $name) ?? ''; |
||
| 510 | } |
||
| 511 | |||
| 512 | return $this->request->old($name, $value); |
||
| 513 | } |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Retrieve the value from the current session or assigned model. This is |
||
| 517 | * a public alias for `old`. |
||
| 518 | * |
||
| 519 | * @param string $name |
||
| 520 | * @param mixed $value |
||
| 521 | * |
||
| 522 | * @return mixed |
||
| 523 | */ |
||
| 524 | public function value($name, $default = null) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @param string $name |
||
| 531 | * |
||
| 532 | * @return string |
||
| 533 | */ |
||
| 534 | protected function fieldName($name) |
||
| 538 | |||
| 539 | protected function ensureModelIsAvailable() |
||
| 545 | } |
||
| 546 |