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 |
||
16 | class FormHelper |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * @var View |
||
21 | */ |
||
22 | protected $view; |
||
23 | |||
24 | /** |
||
25 | * @var TranslatorInterface |
||
26 | */ |
||
27 | protected $translator; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $config; |
||
33 | |||
34 | /** |
||
35 | * @var FormBuilder |
||
36 | */ |
||
37 | protected $formBuilder; |
||
38 | |||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | protected static $reservedFieldNames = [ |
||
43 | 'save' |
||
44 | ]; |
||
45 | |||
46 | /** |
||
47 | * All available field types |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected static $availableFieldTypes = [ |
||
52 | 'text' => 'InputType', |
||
53 | 'email' => 'InputType', |
||
54 | 'url' => 'InputType', |
||
55 | 'tel' => 'InputType', |
||
56 | 'search' => 'InputType', |
||
57 | 'password' => 'InputType', |
||
58 | 'hidden' => 'InputType', |
||
59 | 'number' => 'InputType', |
||
60 | 'date' => 'InputType', |
||
61 | 'file' => 'InputType', |
||
62 | 'image' => 'InputType', |
||
63 | 'color' => 'InputType', |
||
64 | 'datetime-local' => 'InputType', |
||
65 | 'month' => 'InputType', |
||
66 | 'range' => 'InputType', |
||
67 | 'time' => 'InputType', |
||
68 | 'week' => 'InputType', |
||
69 | 'select' => 'SelectType', |
||
70 | 'textarea' => 'TextareaType', |
||
71 | 'button' => 'ButtonType', |
||
72 | 'buttongroup' => 'ButtonGroupType', |
||
73 | 'submit' => 'ButtonType', |
||
74 | 'reset' => 'ButtonType', |
||
75 | 'radio' => 'CheckableType', |
||
76 | 'checkbox' => 'CheckableType', |
||
77 | 'choice' => 'ChoiceType', |
||
78 | 'form' => 'ChildFormType', |
||
79 | 'entity' => 'EntityType', |
||
80 | 'collection' => 'CollectionType', |
||
81 | 'repeated' => 'RepeatedType', |
||
82 | 'static' => 'StaticType' |
||
83 | ]; |
||
84 | |||
85 | /** |
||
86 | * Custom types |
||
87 | * |
||
88 | * @var array |
||
89 | */ |
||
90 | private $customTypes = []; |
||
91 | |||
92 | /** |
||
93 | * @param View $view |
||
94 | * @param Translator $translator |
||
95 | * @param array $config |
||
96 | */ |
||
97 | 130 | public function __construct(View $view, Translator $translator, array $config = []) |
|
104 | |||
105 | /** |
||
106 | * @param string $key |
||
107 | * @param string $default |
||
108 | * @param array $customConfig |
||
109 | * @return mixed |
||
110 | */ |
||
111 | 130 | public function getConfig($key = null, $default = null, $customConfig = []) |
|
121 | |||
122 | /** |
||
123 | * @return View |
||
124 | */ |
||
125 | 38 | public function getView() |
|
129 | |||
130 | /** |
||
131 | * Merge options array. |
||
132 | * |
||
133 | * @param array $first |
||
134 | * @param array $second |
||
135 | * @return array |
||
136 | */ |
||
137 | 130 | public function mergeOptions(array $first, array $second) |
|
141 | |||
142 | /** |
||
143 | * Get proper class for field type. |
||
144 | * |
||
145 | * @param $type |
||
146 | * @return string |
||
147 | */ |
||
148 | 87 | public function getFieldType($type) |
|
174 | |||
175 | /** |
||
176 | * Convert array of attributes to html attributes. |
||
177 | * |
||
178 | * @param $options |
||
179 | * @return string |
||
180 | */ |
||
181 | 103 | public function prepareAttributes($options) |
|
198 | |||
199 | /** |
||
200 | * Add custom field. |
||
201 | * |
||
202 | * @param $name |
||
203 | * @param $class |
||
204 | */ |
||
205 | 3 | public function addCustomField($name, $class) |
|
213 | |||
214 | /** |
||
215 | * Load custom field types from config file. |
||
216 | */ |
||
217 | 130 | private function loadCustomTypes() |
|
227 | |||
228 | /** |
||
229 | * Check if custom field with provided name exists |
||
230 | * @param string $name |
||
231 | * @return boolean |
||
232 | */ |
||
233 | 87 | public function hasCustomField($name) |
|
237 | |||
238 | /** |
||
239 | * @param object $model |
||
240 | * @return object|null |
||
241 | */ |
||
242 | 4 | public function convertModelToArray($model) |
|
258 | |||
259 | /** |
||
260 | * Format the label to the proper format. |
||
261 | * |
||
262 | * @param $name |
||
263 | * @return string |
||
264 | */ |
||
265 | 101 | public function formatLabel($name) |
|
281 | |||
282 | /** |
||
283 | * @param FormField $field |
||
284 | * @return RulesParser |
||
285 | */ |
||
286 | 102 | public function createRulesParser(FormField $field) |
|
290 | |||
291 | /** |
||
292 | * @param FormField $field |
||
293 | * @return array |
||
294 | */ |
||
295 | 9 | public function getFieldValidationRules(FormField $field) |
|
308 | |||
309 | /** |
||
310 | * @param FormField[] $fields |
||
311 | * @return array |
||
312 | */ |
||
313 | 9 | public function mergeFieldsRules($fields) |
|
323 | |||
324 | /** |
||
325 | * @param array $fields |
||
326 | * @return array |
||
327 | */ |
||
328 | 3 | public function mergeAttributes(array $fields) |
|
337 | |||
338 | /** |
||
339 | * Alter a form's values recursively according to its fields. |
||
340 | * |
||
341 | * @param Form $form |
||
342 | * @param array $values |
||
343 | * @return void |
||
344 | */ |
||
345 | 3 | public function alterFieldValues(Form $form, array &$values) |
|
361 | |||
362 | /** |
||
363 | * Alter a form's validity recursively, and add messages with nested form prefix. |
||
364 | * |
||
365 | * @return void |
||
366 | */ |
||
367 | 9 | public function alterValid(Form $form, Form $mainForm, &$isValid) |
|
385 | |||
386 | /** |
||
387 | * Add unprefixed messages with prefix to a MessageBag. |
||
388 | * |
||
389 | * @return void |
||
390 | */ |
||
391 | 1 | public function appendMessagesWithPrefix(MessageBag $messageBag, $prefix, array $keyedMessages) |
|
403 | |||
404 | /** |
||
405 | * @param string $string |
||
406 | * @return string |
||
407 | */ |
||
408 | 102 | public function transformToDotSyntax($string) |
|
412 | |||
413 | /** |
||
414 | * @param string $string |
||
415 | * @return string |
||
416 | */ |
||
417 | 7 | public function transformToBracketSyntax($string) |
|
418 | { |
||
419 | 7 | $name = explode('.', $string); |
|
420 | 7 | if ($name && count($name) == 1) { |
|
421 | 1 | return $name[0]; |
|
422 | } |
||
423 | |||
424 | 6 | $first = array_shift($name); |
|
425 | 6 | return $first . '[' . implode('][', $name) . ']'; |
|
426 | } |
||
427 | |||
428 | /** |
||
429 | * @return TranslatorInterface |
||
430 | */ |
||
431 | 3 | public function getTranslator() |
|
435 | |||
436 | /** |
||
437 | * Check if field name is valid and not reserved. |
||
438 | * |
||
439 | * @throws \InvalidArgumentException |
||
440 | * @param string $name |
||
441 | * @param string $className |
||
442 | */ |
||
443 | 70 | public function checkFieldName($name, $className) |
|
460 | } |
||
461 |
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..