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 | 129 | public function __construct(View $view, Translator $translator, array $config = []) |
|
103 | |||
104 | /** |
||
105 | * @param string $key |
||
106 | * @param string $default |
||
107 | * @param mixed $customConfig |
||
108 | * @return mixed |
||
109 | */ |
||
110 | 129 | public function getConfig($key = null, $default = null, $customConfig = null) |
|
127 | |||
128 | /** |
||
129 | * @return View |
||
130 | */ |
||
131 | 38 | public function getView() |
|
135 | |||
136 | /** |
||
137 | * Merge options array. |
||
138 | * |
||
139 | * @param array $first |
||
140 | * @param array $second |
||
141 | * @return array |
||
142 | */ |
||
143 | 129 | public function mergeOptions(array $first, array $second) |
|
147 | |||
148 | /** |
||
149 | * Get proper class for field type. |
||
150 | * |
||
151 | * @param $type |
||
152 | * @return string |
||
153 | */ |
||
154 | 86 | public function getFieldType($type) |
|
180 | |||
181 | /** |
||
182 | * Convert array of attributes to html attributes. |
||
183 | * |
||
184 | * @param $options |
||
185 | * @return string |
||
186 | */ |
||
187 | 102 | public function prepareAttributes($options) |
|
204 | |||
205 | /** |
||
206 | * Add custom field. |
||
207 | * |
||
208 | * @param $name |
||
209 | * @param $class |
||
210 | */ |
||
211 | 3 | public function addCustomField($name, $class) |
|
219 | |||
220 | /** |
||
221 | * Load custom field types from config file. |
||
222 | */ |
||
223 | 129 | private function loadCustomTypes() |
|
233 | |||
234 | /** |
||
235 | * Check if custom field with provided name exists |
||
236 | * @param string $name |
||
237 | * @return boolean |
||
238 | */ |
||
239 | 86 | public function hasCustomField($name) |
|
243 | |||
244 | /** |
||
245 | * @param object $model |
||
246 | * @return object|null |
||
247 | */ |
||
248 | 2 | public function convertModelToArray($model) |
|
264 | |||
265 | /** |
||
266 | * Format the label to the proper format. |
||
267 | * |
||
268 | * @param $name |
||
269 | * @return string |
||
270 | */ |
||
271 | 100 | public function formatLabel($name) |
|
287 | |||
288 | /** |
||
289 | * @param FormField $field |
||
290 | * @return RulesParser |
||
291 | */ |
||
292 | 101 | public function createRulesParser(FormField $field) |
|
296 | |||
297 | /** |
||
298 | * @param FormField[] $fields |
||
299 | * @return array |
||
300 | */ |
||
301 | 9 | public function mergeFieldsRules($fields) |
|
321 | |||
322 | /** |
||
323 | * @param array $fields |
||
324 | * @return array |
||
325 | */ |
||
326 | 3 | public function mergeAttributes(array $fields) |
|
335 | |||
336 | /** |
||
337 | * Alter a form's values recursively according to its fields. |
||
338 | * |
||
339 | * @param Form $form |
||
340 | * @param array $values |
||
341 | * @return void |
||
342 | */ |
||
343 | 3 | public function alterFieldValues(Form $form, array &$values) |
|
359 | |||
360 | /** |
||
361 | * Alter a form's validity recursively, and add messages with nested form prefix. |
||
362 | * |
||
363 | * @return void |
||
364 | */ |
||
365 | 9 | public function alterValid(Form $form, Form $mainForm, &$isValid) |
|
383 | |||
384 | /** |
||
385 | * Add unprefixed messages with prefix to a MessageBag. |
||
386 | * |
||
387 | * @return void |
||
388 | */ |
||
389 | 1 | public function appendMessagesWithPrefix(MessageBag $messageBag, $prefix, array $keyedMessages) |
|
401 | |||
402 | /** |
||
403 | * @param string $string |
||
404 | * @return string |
||
405 | */ |
||
406 | 101 | public function transformToDotSyntax($string) |
|
410 | |||
411 | /** |
||
412 | * @param string $string |
||
413 | * @return string |
||
414 | */ |
||
415 | 6 | public function transformToBracketSyntax($string) |
|
425 | |||
426 | /** |
||
427 | * @return TranslatorInterface |
||
428 | */ |
||
429 | 3 | public function getTranslator() |
|
433 | |||
434 | /** |
||
435 | * Check if field name is valid and not reserved. |
||
436 | * |
||
437 | * @throws \InvalidArgumentException |
||
438 | * @param string $name |
||
439 | * @param string $className |
||
440 | */ |
||
441 | 69 | public function checkFieldName($name, $className) |
|
458 | } |
||
459 |
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..