Complex classes like FormHelper 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 FormHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Kris\LaravelFormBuilder; |
||
| 12 | class FormHelper |
||
| 13 | { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var View |
||
| 17 | */ |
||
| 18 | protected $view; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var TranslatorInterface |
||
| 22 | */ |
||
| 23 | protected $translator; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $config; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var FormBuilder |
||
| 32 | */ |
||
| 33 | protected $formBuilder; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected static $reservedFieldNames = [ |
||
| 39 | 'save' |
||
| 40 | ]; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * All available field types |
||
| 44 | * |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected static $availableFieldTypes = [ |
||
| 48 | 'text' => 'InputType', |
||
| 49 | 'email' => 'InputType', |
||
| 50 | 'url' => 'InputType', |
||
| 51 | 'tel' => 'InputType', |
||
| 52 | 'search' => 'InputType', |
||
| 53 | 'password' => 'InputType', |
||
| 54 | 'hidden' => 'InputType', |
||
| 55 | 'number' => 'InputType', |
||
| 56 | 'date' => 'InputType', |
||
| 57 | 'file' => 'InputType', |
||
| 58 | 'image' => 'InputType', |
||
| 59 | 'color' => 'InputType', |
||
| 60 | 'datetime-local' => 'InputType', |
||
| 61 | 'month' => 'InputType', |
||
| 62 | 'range' => 'InputType', |
||
| 63 | 'time' => 'InputType', |
||
| 64 | 'week' => 'InputType', |
||
| 65 | 'select' => 'SelectType', |
||
| 66 | 'textarea' => 'TextareaType', |
||
| 67 | 'button' => 'ButtonType', |
||
| 68 | 'submit' => 'ButtonType', |
||
| 69 | 'reset' => 'ButtonType', |
||
| 70 | 'radio' => 'CheckableType', |
||
| 71 | 'checkbox' => 'CheckableType', |
||
| 72 | 'choice' => 'ChoiceType', |
||
| 73 | 'form' => 'ChildFormType', |
||
| 74 | 'entity' => 'EntityType', |
||
| 75 | 'collection' => 'CollectionType', |
||
| 76 | 'repeated' => 'RepeatedType', |
||
| 77 | 'static' => 'StaticType' |
||
| 78 | ]; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Custom types |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | private $customTypes = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param View $view |
||
| 89 | * @param TranslatorInterface $translator |
||
| 90 | * @param array $config |
||
| 91 | */ |
||
| 92 | 105 | public function __construct(View $view, TranslatorInterface $translator, array $config = []) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @param string $key |
||
| 102 | * @param string $default |
||
| 103 | * @return mixed |
||
| 104 | */ |
||
| 105 | 105 | public function getConfig($key, $default = null) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * @return View |
||
| 112 | */ |
||
| 113 | 35 | public function getView() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Merge options array |
||
| 120 | * |
||
| 121 | * @param array $first |
||
| 122 | * @param array $second |
||
| 123 | * @return array |
||
| 124 | */ |
||
| 125 | 105 | public function mergeOptions(array $first, array $second) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Get proper class for field type |
||
| 132 | * |
||
| 133 | * @param $type |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | 68 | public function getFieldType($type) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Convert array of attributes to html attributes |
||
| 165 | * |
||
| 166 | * @param $options |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | 84 | public function prepareAttributes($options) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Add custom field |
||
| 189 | * |
||
| 190 | * @param $name |
||
| 191 | * @param $class |
||
| 192 | */ |
||
| 193 | 3 | public function addCustomField($name, $class) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Load custom field types from config file |
||
| 204 | */ |
||
| 205 | 105 | private function loadCustomTypes() |
|
| 215 | |||
| 216 | 5 | public function convertModelToArray($model) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Format the label to the proper format |
||
| 235 | * |
||
| 236 | * @param $name |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | 82 | public function formatLabel($name) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @param FormField[] $fields |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | 7 | public function mergeFieldsRules($fields) |
|
| 280 | |||
| 281 | /** |
||
| 282 | * @return array |
||
| 283 | */ |
||
| 284 | 2 | public function mergeAttributes($fields) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Alter a form's values recursively according to its fields |
||
| 296 | * |
||
| 297 | * @return void |
||
| 298 | */ |
||
| 299 | 2 | public function alterFieldValues(Form $form, array &$values) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Alter a form's validity recursively, and add messages with nested form prefix |
||
| 318 | * |
||
| 319 | * @return void |
||
| 320 | */ |
||
| 321 | 7 | public function alterValid(Form $form, Form $mainForm, &$isValid) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Add unprefixed messages with prefix to a MessageBag |
||
| 342 | */ |
||
| 343 | public function appendMessagesWithPrefix(MessageBag $messageBag, $prefix, array $keyedMessages) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param string $string |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | 83 | public function transformToDotSyntax($string) |
|
| 364 | |||
| 365 | /** |
||
| 366 | * @param string $string |
||
| 367 | * @return string |
||
| 368 | */ |
||
| 369 | 6 | public function transformToBracketSyntax($string) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * @return TranslatorInterface |
||
| 382 | */ |
||
| 383 | 3 | public function getTranslator() |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Check if field name is valid and not reserved |
||
| 390 | * |
||
| 391 | * @throws \InvalidArgumentException |
||
| 392 | * @param string $name |
||
| 393 | * @param string $className |
||
| 394 | */ |
||
| 395 | 54 | public function checkFieldName($name, $className) |
|
| 412 | } |
||
| 413 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: