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 |
||
| 15 | class FormHelper |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var View |
||
| 20 | */ |
||
| 21 | protected $view; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var TranslatorInterface |
||
| 25 | */ |
||
| 26 | protected $translator; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $config; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var FormBuilder |
||
| 35 | */ |
||
| 36 | protected $formBuilder; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected static $reservedFieldNames = [ |
||
| 42 | 'save' |
||
| 43 | ]; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * All available field types |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected static $availableFieldTypes = [ |
||
| 51 | 'text' => 'InputType', |
||
| 52 | 'email' => 'InputType', |
||
| 53 | 'url' => 'InputType', |
||
| 54 | 'tel' => 'InputType', |
||
| 55 | 'search' => 'InputType', |
||
| 56 | 'password' => 'InputType', |
||
| 57 | 'hidden' => 'InputType', |
||
| 58 | 'number' => 'InputType', |
||
| 59 | 'date' => 'InputType', |
||
| 60 | 'file' => 'InputType', |
||
| 61 | 'image' => 'InputType', |
||
| 62 | 'color' => 'InputType', |
||
| 63 | 'datetime-local' => 'InputType', |
||
| 64 | 'month' => 'InputType', |
||
| 65 | 'range' => 'InputType', |
||
| 66 | 'time' => 'InputType', |
||
| 67 | 'week' => 'InputType', |
||
| 68 | 'select' => 'SelectType', |
||
| 69 | 'textarea' => 'TextareaType', |
||
| 70 | 'button' => 'ButtonType', |
||
| 71 | 'buttongroup' => 'ButtonGroupType', |
||
| 72 | 'submit' => 'ButtonType', |
||
| 73 | 'reset' => 'ButtonType', |
||
| 74 | 'radio' => 'CheckableType', |
||
| 75 | 'checkbox' => 'CheckableType', |
||
| 76 | 'choice' => 'ChoiceType', |
||
| 77 | 'form' => 'ChildFormType', |
||
| 78 | 'entity' => 'EntityType', |
||
| 79 | 'collection' => 'CollectionType', |
||
| 80 | 'repeated' => 'RepeatedType', |
||
| 81 | 'static' => 'StaticType' |
||
| 82 | ]; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Custom types |
||
| 86 | * |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | private $customTypes = []; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param View $view |
||
| 93 | * @param Translator $translator |
||
| 94 | * @param array $config |
||
| 95 | */ |
||
| 96 | 107 | public function __construct(View $view, Translator $translator, array $config = []) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @param string $key |
||
| 106 | * @param string $default |
||
| 107 | * @return mixed |
||
| 108 | */ |
||
| 109 | 107 | public function getConfig($key, $default = null) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @return View |
||
| 116 | */ |
||
| 117 | 36 | public function getView() |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Merge options array. |
||
| 124 | * |
||
| 125 | * @param array $first |
||
| 126 | * @param array $second |
||
| 127 | * @return array |
||
| 128 | */ |
||
| 129 | 107 | public function mergeOptions(array $first, array $second) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Get proper class for field type. |
||
| 136 | * |
||
| 137 | * @param $type |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | 70 | public function getFieldType($type) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Convert array of attributes to html attributes. |
||
| 169 | * |
||
| 170 | * @param $options |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | 86 | public function prepareAttributes($options) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Add custom field. |
||
| 193 | * |
||
| 194 | * @param $name |
||
| 195 | * @param $class |
||
| 196 | */ |
||
| 197 | 3 | public function addCustomField($name, $class) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Load custom field types from config file. |
||
| 208 | */ |
||
| 209 | 107 | private function loadCustomTypes() |
|
| 219 | |||
| 220 | /** |
||
| 221 | * @param object $model |
||
| 222 | * @return object|null |
||
| 223 | */ |
||
| 224 | 2 | public function convertModelToArray($model) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Format the label to the proper format. |
||
| 243 | * |
||
| 244 | * @param $name |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | 84 | public function formatLabel($name) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @param FormField $field |
||
| 266 | * @return RulesParser |
||
| 267 | */ |
||
| 268 | 85 | public function createRulesParser(FormField $field) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * @param FormField[] $fields |
||
| 275 | * @return array |
||
| 276 | */ |
||
| 277 | 8 | public function mergeFieldsRules($fields) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @param array $fields |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | 3 | public function mergeAttributes(array $fields) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Alter a form's values recursively according to its fields. |
||
| 314 | * |
||
| 315 | * @param Form $form |
||
| 316 | * @param array $values |
||
| 317 | * @return void |
||
| 318 | */ |
||
| 319 | 3 | public function alterFieldValues(Form $form, array &$values) |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Alter a form's validity recursively, and add messages with nested form prefix. |
||
| 338 | * |
||
| 339 | * @return void |
||
| 340 | */ |
||
| 341 | 8 | public function alterValid(Form $form, Form $mainForm, &$isValid) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Add unprefixed messages with prefix to a MessageBag. |
||
| 362 | * |
||
| 363 | * @return void |
||
| 364 | */ |
||
| 365 | 1 | public function appendMessagesWithPrefix(MessageBag $messageBag, $prefix, array $keyedMessages) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * @param string $string |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | 85 | public function transformToDotSyntax($string) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * @param string $string |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | 6 | public function transformToBracketSyntax($string) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * @return TranslatorInterface |
||
| 404 | */ |
||
| 405 | 3 | public function getTranslator() |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Check if field name is valid and not reserved. |
||
| 412 | * |
||
| 413 | * @throws \InvalidArgumentException |
||
| 414 | * @param string $name |
||
| 415 | * @param string $className |
||
| 416 | */ |
||
| 417 | 55 | public function checkFieldName($name, $className) |
|
| 434 | } |
||
| 435 |
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..