Complex classes like Link 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 Link, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Arcanesoft\Core\Helpers\UI; |
||
| 15 | class Link implements Htmlable |
||
| 16 | { |
||
| 17 | /* ----------------------------------------------------------------- |
||
| 18 | | Properties |
||
| 19 | | ----------------------------------------------------------------- |
||
| 20 | */ |
||
| 21 | /** @var string */ |
||
| 22 | protected $action; |
||
| 23 | |||
| 24 | /** @var string */ |
||
| 25 | protected $url; |
||
| 26 | |||
| 27 | /** @var \Illuminate\Support\Collection */ |
||
| 28 | protected $attributes; |
||
| 29 | |||
| 30 | /** @var bool */ |
||
| 31 | protected $disabled; |
||
| 32 | |||
| 33 | /** @var string */ |
||
| 34 | protected $size = 'md'; |
||
| 35 | |||
| 36 | /** @var bool */ |
||
| 37 | public $withTitle = true; |
||
| 38 | |||
| 39 | /** @var bool */ |
||
| 40 | protected $withIcon = true; |
||
| 41 | |||
| 42 | /** @var bool */ |
||
| 43 | protected $withTooltip = true; |
||
| 44 | |||
| 45 | /* ----------------------------------------------------------------- |
||
| 46 | | Constructor |
||
| 47 | | ----------------------------------------------------------------- |
||
| 48 | */ |
||
| 49 | /** |
||
| 50 | * LinkElement constructor. |
||
| 51 | * |
||
| 52 | * @param string $action |
||
| 53 | * @param string $url |
||
| 54 | * @param array $attributes |
||
| 55 | * @param bool $disabled |
||
| 56 | */ |
||
| 57 | 75 | public function __construct($action, $url, array $attributes = [], $disabled = false) |
|
| 65 | |||
| 66 | /* ----------------------------------------------------------------- |
||
| 67 | | Getters & Setters |
||
| 68 | | ----------------------------------------------------------------- |
||
| 69 | */ |
||
| 70 | /** |
||
| 71 | * Get the action title. |
||
| 72 | * |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | 63 | protected function getActionTitle() |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Set the attributes. |
||
| 82 | * |
||
| 83 | * @param array $attributes |
||
| 84 | * |
||
| 85 | * @return self |
||
| 86 | */ |
||
| 87 | 75 | public function setAttributes(array $attributes) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Set an attribute. |
||
| 99 | * |
||
| 100 | * @param string $key |
||
| 101 | * @param mixed $value |
||
| 102 | * |
||
| 103 | * @return self |
||
| 104 | */ |
||
| 105 | 6 | public function setAttribute($key, $value) |
|
| 111 | |||
| 112 | protected function cleanAttributes() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Set the size. |
||
| 123 | * |
||
| 124 | * @param string $size |
||
| 125 | * |
||
| 126 | * @return self |
||
| 127 | */ |
||
| 128 | 75 | public function size($size) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * @param bool $withTitle |
||
| 137 | * |
||
| 138 | * @return self |
||
| 139 | */ |
||
| 140 | 72 | public function withTitle($withTitle) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Show/Hide the icon. |
||
| 149 | * |
||
| 150 | * @var bool $icon |
||
| 151 | * |
||
| 152 | * @return self |
||
| 153 | */ |
||
| 154 | 18 | public function icon($withIcon) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Enable the tooltip. |
||
| 163 | * |
||
| 164 | * @param bool $withTooltip |
||
| 165 | * |
||
| 166 | * @return self |
||
| 167 | */ |
||
| 168 | 24 | public function tooltip($withTooltip) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Show only the icon and the title as tooltip. |
||
| 177 | * |
||
| 178 | * @return self |
||
| 179 | */ |
||
| 180 | public function onlyIcon() |
||
| 184 | |||
| 185 | /* ----------------------------------------------------------------- |
||
| 186 | | Main Methods |
||
| 187 | | ----------------------------------------------------------------- |
||
| 188 | */ |
||
| 189 | /** |
||
| 190 | * @param string $action |
||
| 191 | * @param string $url |
||
| 192 | * @param array $attributes |
||
| 193 | * @param bool $disabled |
||
| 194 | * |
||
| 195 | * @return self |
||
| 196 | */ |
||
| 197 | 75 | public static function make($action, $url, array $attributes = [], $disabled = false) |
|
| 201 | |||
| 202 | /* ----------------------------------------------------------------- |
||
| 203 | | Other Functions |
||
| 204 | | ----------------------------------------------------------------- |
||
| 205 | */ |
||
| 206 | /** |
||
| 207 | * Get content as a string of HTML. |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | 75 | public function toHtml() |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Render the link value. |
||
| 218 | * |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | 75 | protected function renderValue() |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Render the icon. |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | 75 | protected function renderIcon() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Render the attributes. |
||
| 252 | * |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | 75 | protected function renderAttributes() |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Get the link class. |
||
| 277 | * |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | 75 | protected function getLinkClass() |
|
| 284 | |||
| 285 | 75 | protected function getLinkColor() |
|
| 302 | |||
| 303 | 75 | protected function getLinkSize() |
|
| 312 | |||
| 313 | /* ----------------------------------------------------------------- |
||
| 314 | | Helpers Methods |
||
| 315 | | ----------------------------------------------------------------- |
||
| 316 | */ |
||
| 317 | /** |
||
| 318 | * Generate activate icon link. |
||
| 319 | * |
||
| 320 | * @param bool $active |
||
| 321 | * @param string $url |
||
| 322 | * @param array $attributes |
||
| 323 | * @param bool $disabled |
||
| 324 | * |
||
| 325 | * @return self |
||
| 326 | */ |
||
| 327 | 18 | public static function activateIcon($active, $url, array $attributes = [], $disabled = false) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Generate activate icon link for modals (reverse button based on active state). |
||
| 337 | * |
||
| 338 | * @param bool $active |
||
| 339 | * @param string $url |
||
| 340 | * @param array $attributes |
||
| 341 | * @param bool $disabled |
||
| 342 | * |
||
| 343 | * @return self |
||
| 344 | */ |
||
| 345 | 3 | public static function activateModalIcon($active, $url, array $attributes = [], $disabled = false) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Generate activate link with icon for modals (reverse button based on active state). |
||
| 359 | * |
||
| 360 | * @param bool $active |
||
| 361 | * @param string $url |
||
| 362 | * @param array $attributes |
||
| 363 | * @param bool $disabled |
||
| 364 | * |
||
| 365 | * @return self |
||
| 366 | */ |
||
| 367 | 3 | public static function activateModalWithIcon($active, $url, array $attributes = [], $disabled = false) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Generate add icon link. |
||
| 384 | * |
||
| 385 | * @param string $url |
||
| 386 | * @param array $attributes |
||
| 387 | * @param bool $disabled |
||
| 388 | * |
||
| 389 | * @return self |
||
| 390 | */ |
||
| 391 | 9 | public static function addIcon($url, array $attributes = [], $disabled = false) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Generate delete icon link for modals. |
||
| 400 | * |
||
| 401 | * @param string $url |
||
| 402 | * @param array $attributes |
||
| 403 | * @param bool $disabled |
||
| 404 | * |
||
| 405 | * @return self |
||
| 406 | */ |
||
| 407 | 9 | public static function deleteModalIcon($url, array $attributes = [], $disabled = false) |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Generate delete link with icon for modals. |
||
| 416 | * |
||
| 417 | * @param string $url |
||
| 418 | * @param array $attributes |
||
| 419 | * @param bool $disabled |
||
| 420 | * |
||
| 421 | * @return self |
||
| 422 | */ |
||
| 423 | 3 | public static function deleteModalWithIcon($url, array $attributes = [], $disabled = false) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Generate detach icon link for modals. |
||
| 433 | * |
||
| 434 | * @param string $url |
||
| 435 | * @param array $attributes |
||
| 436 | * @param bool $disabled |
||
| 437 | * |
||
| 438 | * @return self |
||
| 439 | */ |
||
| 440 | 9 | public static function detachModalIcon($url, array $attributes = [], $disabled = false) |
|
| 446 | |||
| 447 | /** |
||
| 448 | * Generate edit icon link. |
||
| 449 | * |
||
| 450 | * @param string $url |
||
| 451 | * @param array $attributes |
||
| 452 | * @param bool $disabled |
||
| 453 | * |
||
| 454 | * @return self |
||
| 455 | */ |
||
| 456 | 9 | public static function editIcon($url, array $attributes = [], $disabled = false) |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Generate edit link with icon. |
||
| 465 | * |
||
| 466 | * @param string $url |
||
| 467 | * @param array $attributes |
||
| 468 | * @param bool $disabled |
||
| 469 | * |
||
| 470 | * @return self |
||
| 471 | */ |
||
| 472 | 3 | public static function editWithIcon($url, array $attributes = [], $disabled = false) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Generate restore icon link for modals. |
||
| 481 | * |
||
| 482 | * @param string $url |
||
| 483 | * @param array $attributes |
||
| 484 | * @param bool $disabled |
||
| 485 | * |
||
| 486 | * @return self |
||
| 487 | */ |
||
| 488 | 3 | public static function restoreModalIcon($url, array $attributes = [], $disabled = false) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Generate restore link with icon for modals. |
||
| 497 | * |
||
| 498 | * @param string $url |
||
| 499 | * @param array $attributes |
||
| 500 | * @param bool $disabled |
||
| 501 | * |
||
| 502 | * @return self |
||
| 503 | */ |
||
| 504 | 3 | public static function restoreModalWithIcon($url, array $attributes = [], $disabled = false) |
|
| 511 | |||
| 512 | /** |
||
| 513 | * Generate show icon link. |
||
| 514 | * |
||
| 515 | * @param string $url |
||
| 516 | * @param array $attributes |
||
| 517 | * @param bool $disabled |
||
| 518 | * |
||
| 519 | * @return self |
||
| 520 | */ |
||
| 521 | 9 | public static function showIcon($url, array $attributes = [], $disabled = false) |
|
| 527 | } |
||
| 528 |