Complex classes like TwitterBootstrap4 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 TwitterBootstrap4, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class TwitterBootstrap4 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 | 'has-warning', |
||
| 69 | 'has-error', |
||
| 70 | 'has-success', |
||
| 71 | ); |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The default HTML tag used for icons |
||
| 75 | * |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $iconTag = 'i'; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The default set for icon fonts |
||
| 82 | * By default Bootstrap 4 offers no fonts, but we'll add Font Awesome |
||
| 83 | * |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected $iconSet = 'fa'; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The default prefix icon names |
||
| 90 | * Using Font Awesome 5, this can be 'fa' or 'fas' for solid, 'far' for regular |
||
| 91 | * |
||
| 92 | * @var string |
||
| 93 | */ |
||
| 94 | protected $iconPrefix = 'fa'; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Create a new TwitterBootstrap instance |
||
| 98 | * |
||
| 99 | * @param \Illuminate\Container\Container $app |
||
| 100 | */ |
||
| 101 | public function __construct(Container $app) |
||
| 106 | |||
| 107 | //////////////////////////////////////////////////////////////////// |
||
| 108 | /////////////////////////// FILTER ARRAYS ////////////////////////// |
||
| 109 | //////////////////////////////////////////////////////////////////// |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Filter buttons classes |
||
| 113 | * |
||
| 114 | * @param array $classes An array of classes |
||
| 115 | * |
||
| 116 | * @return string[] A filtered array |
||
| 117 | */ |
||
| 118 | public function filterButtonClasses($classes) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Filter field classes |
||
| 132 | * |
||
| 133 | * @param array $classes An array of classes |
||
| 134 | * |
||
| 135 | * @return array A filtered array |
||
| 136 | */ |
||
| 137 | public function filterFieldClasses($classes) |
||
| 149 | |||
| 150 | //////////////////////////////////////////////////////////////////// |
||
| 151 | ///////////////////// EXPOSE FRAMEWORK SPECIFICS /////////////////// |
||
| 152 | //////////////////////////////////////////////////////////////////// |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Framework error state |
||
| 156 | * |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | public function errorState() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Returns corresponding inline class of a field |
||
| 166 | * |
||
| 167 | * @param Field $field |
||
| 168 | * |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function getInlineLabelClass($field) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Set the fields width from a label width |
||
| 183 | * |
||
| 184 | * @param array $labelWidths |
||
| 185 | */ |
||
| 186 | protected function setFieldWidths($labelWidths) |
||
| 203 | |||
| 204 | //////////////////////////////////////////////////////////////////// |
||
| 205 | ///////////////////////////// ADD CLASSES ////////////////////////// |
||
| 206 | //////////////////////////////////////////////////////////////////// |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Add classes to a field |
||
| 210 | * |
||
| 211 | * @param Field $field |
||
| 212 | * @param array $classes The possible classes to add |
||
| 213 | * |
||
| 214 | * @return Field |
||
| 215 | */ |
||
| 216 | public function getFieldClasses(Field $field, $classes) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Add group classes |
||
| 249 | * |
||
| 250 | * @return string A list of group classes |
||
| 251 | */ |
||
| 252 | public function getGroupClasses() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Add label classes |
||
| 263 | * |
||
| 264 | * @return string[] An array of attributes with the label class |
||
| 265 | */ |
||
| 266 | public function getLabelClasses() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Add uneditable field classes |
||
| 279 | * |
||
| 280 | * @return string An array of attributes with the uneditable class |
||
| 281 | */ |
||
| 282 | public function getUneditableClasses() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Add plain text field classes |
||
| 289 | * |
||
| 290 | * @return string An array of attributes with the plain text class |
||
| 291 | */ |
||
| 292 | public function getPlainTextClasses() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Add form class |
||
| 299 | * |
||
| 300 | * @param string $type The type of form to add |
||
| 301 | * |
||
| 302 | * @return string|null |
||
| 303 | */ |
||
| 304 | public function getFormClasses($type) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Add actions block class |
||
| 311 | * |
||
| 312 | * @return string|null |
||
| 313 | */ |
||
| 314 | public function getActionClasses() |
||
| 322 | |||
| 323 | //////////////////////////////////////////////////////////////////// |
||
| 324 | //////////////////////////// RENDER BLOCKS ///////////////////////// |
||
| 325 | //////////////////////////////////////////////////////////////////// |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Render an help text |
||
| 329 | * |
||
| 330 | * @param string $text |
||
| 331 | * @param array $attributes |
||
| 332 | * |
||
| 333 | * @return Element |
||
| 334 | */ |
||
| 335 | public function createHelp($text, $attributes = array()) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Render an help text |
||
| 342 | * |
||
| 343 | * @param string $text |
||
| 344 | * @param array $attributes |
||
| 345 | * |
||
| 346 | * @return Element |
||
| 347 | */ |
||
| 348 | public function createBlockHelp($text, $attributes = array()) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Render a disabled field |
||
| 355 | * |
||
| 356 | * @param Field $field |
||
| 357 | * |
||
| 358 | * @return Element |
||
| 359 | */ |
||
| 360 | public function createDisabledField(Field $field) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Render a plain text field |
||
| 367 | * |
||
| 368 | * @param Field $field |
||
| 369 | * |
||
| 370 | * @return Element |
||
| 371 | */ |
||
| 372 | public function createPlainTextField(Field $field) |
||
| 381 | |||
| 382 | //////////////////////////////////////////////////////////////////// |
||
| 383 | //////////////////////////// WRAP BLOCKS /////////////////////////// |
||
| 384 | //////////////////////////////////////////////////////////////////// |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Wrap an item to be prepended or appended to the current field |
||
| 388 | * |
||
| 389 | * @return Element A wrapped item |
||
| 390 | */ |
||
| 391 | public function placeAround($item) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Wrap a field with prepended and appended items |
||
| 406 | * |
||
| 407 | * @param Field $field |
||
| 408 | * @param array $prepend |
||
| 409 | * @param array $append |
||
| 410 | * |
||
| 411 | * @return string A field concatented with prepended and/or appended items |
||
| 412 | */ |
||
| 413 | public function prependAppend($field, $prepend, $append) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Wrap a field with potential additional tags |
||
| 426 | * |
||
| 427 | * @param Field $field |
||
| 428 | * |
||
| 429 | * @return Element A wrapped field |
||
| 430 | */ |
||
| 431 | public function wrapField($field) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Wrap actions block with potential additional tags |
||
| 442 | * |
||
| 443 | * @param Actions $actions |
||
| 444 | * |
||
| 445 | * @return string A wrapped actions block |
||
| 446 | */ |
||
| 447 | public function wrapActions($actions) |
||
| 456 | } |
||
| 457 |
This check marks private properties in classes that are never used. Those properties can be removed.