Complex classes like TwitterBootstrap5 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 TwitterBootstrap5, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class TwitterBootstrap5 extends Framework implements FrameworkInterface |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Form types that trigger special styling for this Framework |
||
| 18 | * |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | protected $availableTypes = array('horizontal', 'vertical', 'inline'); |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The button types available |
||
| 25 | * |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | private $buttons = array( |
||
| 29 | 'lg', |
||
| 30 | 'sm', |
||
| 31 | 'xs', |
||
| 32 | 'block', |
||
| 33 | 'link', |
||
| 34 | 'primary', |
||
| 35 | 'secondary', |
||
| 36 | 'warning', |
||
| 37 | 'danger', |
||
| 38 | 'success', |
||
| 39 | 'info', |
||
| 40 | 'light', |
||
| 41 | 'dark', |
||
| 42 | ); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The field sizes available |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | private $fields = array( |
||
| 50 | 'lg', |
||
| 51 | 'sm', |
||
| 52 | // 'col-xs-1', 'col-xs-2', 'col-xs-3', 'col-xs-4', 'col-xs-5', 'col-xs-6', |
||
| 53 | // 'col-xs-7', 'col-xs-8', 'col-xs-9', 'col-xs-10', 'col-xs-11', 'col-xs-12', |
||
| 54 | // 'col-sm-1', 'col-sm-2', 'col-sm-3', 'col-sm-4', 'col-sm-5', 'col-sm-6', |
||
| 55 | // 'col-sm-7', 'col-sm-8', 'col-sm-9', 'col-sm-10', 'col-sm-11', 'col-sm-12', |
||
| 56 | // 'col-md-1', 'col-md-2', 'col-md-3', 'col-md-4', 'col-md-5', 'col-md-6', |
||
| 57 | // 'col-md-7', 'col-md-8', 'col-md-9', 'col-md-10', 'col-md-11', 'col-md-12', |
||
| 58 | // 'col-lg-1', 'col-lg-2', 'col-lg-3', 'col-lg-4', 'col-lg-5', 'col-lg-6', |
||
| 59 | // 'col-lg-7', 'col-lg-8', 'col-lg-9', 'col-lg-10', 'col-lg-11', 'col-lg-12', |
||
| 60 | ); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The field states available |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | protected $states = array( |
||
| 68 | 'is-invalid', |
||
| 69 | ); |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The default HTML tag used for icons |
||
| 73 | * |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | protected $iconTag = 'i'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * The default set for icon fonts |
||
| 80 | * By default Bootstrap 4 offers no fonts, but we'll add Font Awesome |
||
| 81 | * |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $iconSet = 'fa'; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The default prefix icon names |
||
| 88 | * Using Font Awesome 5, this can be 'fa' or 'fas' for solid, 'far' for regular |
||
| 89 | * |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | protected $iconPrefix = 'fa'; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Create a new TwitterBootstrap instance |
||
| 96 | * |
||
| 97 | * @param \Illuminate\Container\Container $app |
||
| 98 | */ |
||
| 99 | public function __construct(Container $app) |
||
| 104 | |||
| 105 | //////////////////////////////////////////////////////////////////// |
||
| 106 | /////////////////////////// FILTER ARRAYS ////////////////////////// |
||
| 107 | //////////////////////////////////////////////////////////////////// |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Filter buttons classes |
||
| 111 | * |
||
| 112 | * @param array $classes An array of classes |
||
| 113 | * |
||
| 114 | * @return string[] A filtered array |
||
| 115 | */ |
||
| 116 | public function filterButtonClasses($classes) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Filter field classes |
||
| 130 | * |
||
| 131 | * @param array $classes An array of classes |
||
| 132 | * |
||
| 133 | * @return array A filtered array |
||
| 134 | */ |
||
| 135 | public function filterFieldClasses($classes) |
||
| 147 | |||
| 148 | //////////////////////////////////////////////////////////////////// |
||
| 149 | ///////////////////// EXPOSE FRAMEWORK SPECIFICS /////////////////// |
||
| 150 | //////////////////////////////////////////////////////////////////// |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Framework error state |
||
| 154 | * |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | public function errorState() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Returns corresponding inline class of a field |
||
| 164 | * |
||
| 165 | * @param Field $field |
||
| 166 | * |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | public function getInlineLabelClass($field) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Set the fields width from a label width |
||
| 181 | * |
||
| 182 | * @param array $labelWidths |
||
| 183 | */ |
||
| 184 | protected function setFieldWidths($labelWidths) |
||
| 201 | |||
| 202 | //////////////////////////////////////////////////////////////////// |
||
| 203 | ///////////////////////////// ADD CLASSES ////////////////////////// |
||
| 204 | //////////////////////////////////////////////////////////////////// |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Add classes to a field |
||
| 208 | * |
||
| 209 | * @param Field $field |
||
| 210 | * @param array $classes The possible classes to add |
||
| 211 | * |
||
| 212 | * @return Field |
||
| 213 | */ |
||
| 214 | public function getFieldClasses(Field $field, $classes) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Add group classes |
||
| 258 | * |
||
| 259 | * @return string A list of group classes |
||
| 260 | */ |
||
| 261 | public function getGroupClasses() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Add label classes |
||
| 272 | * |
||
| 273 | * @return string[] An array of attributes with the label class |
||
| 274 | */ |
||
| 275 | public function getLabelClasses() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Add uneditable field classes |
||
| 288 | * |
||
| 289 | * @return string An array of attributes with the uneditable class |
||
| 290 | */ |
||
| 291 | public function getUneditableClasses() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Add plain text field classes |
||
| 298 | * |
||
| 299 | * @return string An array of attributes with the plain text class |
||
| 300 | */ |
||
| 301 | public function getPlainTextClasses() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Add form class |
||
| 308 | * |
||
| 309 | * @param string $type The type of form to add |
||
| 310 | * |
||
| 311 | * @return string|null |
||
| 312 | */ |
||
| 313 | public function getFormClasses($type) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Add actions block class |
||
| 320 | * |
||
| 321 | * @return string|null |
||
| 322 | */ |
||
| 323 | public function getActionClasses() |
||
| 331 | |||
| 332 | //////////////////////////////////////////////////////////////////// |
||
| 333 | //////////////////////////// RENDER BLOCKS ///////////////////////// |
||
| 334 | //////////////////////////////////////////////////////////////////// |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Render an help text |
||
| 338 | * |
||
| 339 | * @param string $text |
||
| 340 | * @param array $attributes |
||
| 341 | * |
||
| 342 | * @return Element |
||
| 343 | */ |
||
| 344 | public function createHelp($text, $attributes = array()) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Render an validation error text |
||
| 351 | * |
||
| 352 | * @param string $text |
||
| 353 | * @param array $attributes |
||
| 354 | * |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | public function createValidationError($text, $attributes = array()) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Render an help text |
||
| 364 | * |
||
| 365 | * @param string $text |
||
| 366 | * @param array $attributes |
||
| 367 | * |
||
| 368 | * @return Element |
||
| 369 | */ |
||
| 370 | public function createBlockHelp($text, $attributes = array()) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Render a disabled field |
||
| 377 | * |
||
| 378 | * @param Field $field |
||
| 379 | * |
||
| 380 | * @return Element |
||
| 381 | */ |
||
| 382 | public function createDisabledField(Field $field) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Render a plain text field |
||
| 389 | * |
||
| 390 | * @param Field $field |
||
| 391 | * |
||
| 392 | * @return Element |
||
| 393 | */ |
||
| 394 | public function createPlainTextField(Field $field) |
||
| 403 | |||
| 404 | //////////////////////////////////////////////////////////////////// |
||
| 405 | //////////////////////////// WRAP BLOCKS /////////////////////////// |
||
| 406 | //////////////////////////////////////////////////////////////////// |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Wrap an item to be prepended or appended to the current field |
||
| 410 | * |
||
| 411 | * @return Element A wrapped item |
||
| 412 | */ |
||
| 413 | public function placeAround($item, $place = null) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Wrap a field with prepended and appended items |
||
| 436 | * |
||
| 437 | * @param Field $field |
||
| 438 | * @param array $prepend |
||
| 439 | * @param array $append |
||
| 440 | * |
||
| 441 | * @return string A field concatented with prepended and/or appended items |
||
| 442 | */ |
||
| 443 | public function prependAppend($field, $prepend, $append) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Wrap a field with potential additional tags |
||
| 456 | * |
||
| 457 | * @param Field $field |
||
| 458 | * |
||
| 459 | * @return Element A wrapped field |
||
| 460 | */ |
||
| 461 | public function wrapField($field) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Wrap actions block with potential additional tags |
||
| 472 | * |
||
| 473 | * @param Actions $actions |
||
| 474 | * |
||
| 475 | * @return string A wrapped actions block |
||
| 476 | */ |
||
| 477 | public function wrapActions($actions) |
||
| 486 | } |
||
| 487 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..