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; |
||
| 11 | class FormHelper |
||
| 12 | { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var View |
||
| 16 | */ |
||
| 17 | protected $view; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var TranslatorInterface |
||
| 21 | */ |
||
| 22 | protected $translator; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $config; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var FormBuilder |
||
| 31 | */ |
||
| 32 | protected $formBuilder; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected static $reservedFieldNames = [ |
||
| 38 | 'save' |
||
| 39 | ]; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * All available field types |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected static $availableFieldTypes = [ |
||
| 47 | 'text' => 'InputType', |
||
| 48 | 'email' => 'InputType', |
||
| 49 | 'url' => 'InputType', |
||
| 50 | 'tel' => 'InputType', |
||
| 51 | 'search' => 'InputType', |
||
| 52 | 'password' => 'InputType', |
||
| 53 | 'hidden' => 'InputType', |
||
| 54 | 'number' => 'InputType', |
||
| 55 | 'date' => 'InputType', |
||
| 56 | 'file' => 'InputType', |
||
| 57 | 'image' => 'InputType', |
||
| 58 | 'color' => 'InputType', |
||
| 59 | 'datetime-local' => 'InputType', |
||
| 60 | 'month' => 'InputType', |
||
| 61 | 'range' => 'InputType', |
||
| 62 | 'time' => 'InputType', |
||
| 63 | 'week' => 'InputType', |
||
| 64 | 'select' => 'SelectType', |
||
| 65 | 'textarea' => 'TextareaType', |
||
| 66 | 'button' => 'ButtonType', |
||
| 67 | 'submit' => 'ButtonType', |
||
| 68 | 'reset' => 'ButtonType', |
||
| 69 | 'radio' => 'CheckableType', |
||
| 70 | 'checkbox' => 'CheckableType', |
||
| 71 | 'choice' => 'ChoiceType', |
||
| 72 | 'form' => 'ChildFormType', |
||
| 73 | 'entity' => 'EntityType', |
||
| 74 | 'collection' => 'CollectionType', |
||
| 75 | 'repeated' => 'RepeatedType', |
||
| 76 | 'static' => 'StaticType' |
||
| 77 | ]; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Custom types |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | private $customTypes = []; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param View $view |
||
| 88 | * @param TranslatorInterface $translator |
||
| 89 | * @param array $config |
||
| 90 | */ |
||
| 91 | 105 | public function __construct(View $view, TranslatorInterface $translator, array $config = []) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @param string $key |
||
| 101 | * @param string $default |
||
| 102 | * @return mixed |
||
| 103 | */ |
||
| 104 | 105 | public function getConfig($key, $default = null) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @return View |
||
| 111 | */ |
||
| 112 | 35 | public function getView() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Merge options array |
||
| 119 | * |
||
| 120 | * @param array $first |
||
| 121 | * @param array $second |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | 105 | public function mergeOptions(array $first, array $second) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Get proper class for field type |
||
| 131 | * |
||
| 132 | * @param $type |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | 68 | public function getFieldType($type) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Convert array of attributes to html attributes |
||
| 164 | * |
||
| 165 | * @param $options |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | 84 | public function prepareAttributes($options) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Add custom field |
||
| 188 | * |
||
| 189 | * @param $name |
||
| 190 | * @param $class |
||
| 191 | */ |
||
| 192 | 3 | public function addCustomField($name, $class) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Load custom field types from config file |
||
| 203 | */ |
||
| 204 | 105 | private function loadCustomTypes() |
|
| 214 | |||
| 215 | 5 | public function convertModelToArray($model) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Format the label to the proper format |
||
| 234 | * |
||
| 235 | * @param $name |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | 82 | public function formatLabel($name) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * @param FormField[] $fields |
||
| 257 | * @return array |
||
| 258 | */ |
||
| 259 | 7 | public function mergeFieldsRules($fields) |
|
| 279 | |||
| 280 | /** |
||
| 281 | * @return array |
||
| 282 | */ |
||
| 283 | 2 | public function mergeAttributes($fields) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Alter a form's values recursively according to its fields |
||
| 295 | * |
||
| 296 | * @return void |
||
| 297 | */ |
||
| 298 | 2 | public function alterFieldValues(Form $form, array &$values) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * @param string $string |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | 83 | public function transformToDotSyntax($string) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * @param string $string |
||
| 326 | * @return string |
||
| 327 | */ |
||
| 328 | 6 | public function transformToBracketSyntax($string) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * @return TranslatorInterface |
||
| 341 | */ |
||
| 342 | 3 | public function getTranslator() |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Check if field name is valid and not reserved |
||
| 349 | * |
||
| 350 | * @throws \InvalidArgumentException |
||
| 351 | * @param string $name |
||
| 352 | * @param string $className |
||
| 353 | */ |
||
| 354 | 54 | public function checkFieldName($name, $className) |
|
| 371 | } |
||
| 372 |
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: