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) |
|
149 | { |
||
150 | 87 | $types = array_keys(static::$availableFieldTypes); |
|
151 | |||
152 | 87 | if (!$type || trim($type) == '') { |
|
153 | 1 | throw new \InvalidArgumentException('Field type must be provided.'); |
|
154 | } |
||
155 | |||
156 | 86 | if ($this->hasCustomField($type)) { |
|
157 | 2 | return $this->customTypes[$type]; |
|
158 | } |
||
159 | |||
160 | 84 | if (!in_array($type, $types)) { |
|
161 | 2 | throw new \InvalidArgumentException( |
|
162 | 2 | sprintf( |
|
163 | 2 | 'Unsupported field type [%s]. Available types are: %s', |
|
164 | 2 | $type, |
|
165 | 2 | join(', ', array_merge($types, array_keys($this->customTypes))) |
|
166 | 2 | ) |
|
167 | 2 | ); |
|
168 | } |
||
169 | |||
170 | 82 | $namespace = __NAMESPACE__.'\\Fields\\'; |
|
171 | |||
172 | 82 | return $namespace . static::$availableFieldTypes[$type]; |
|
173 | } |
||
174 | |||
175 | /** |
||
176 | * Convert array of attributes to html attributes. |
||
177 | * |
||
178 | * @param $options |
||
179 | * @return string |
||
180 | */ |
||
181 | 103 | public function prepareAttributes($options) |
|
182 | { |
||
183 | 103 | if (!$options) { |
|
184 | 13 | return null; |
|
185 | } |
||
186 | |||
187 | 103 | $attributes = []; |
|
188 | |||
189 | 103 | foreach ($options as $name => $option) { |
|
190 | 103 | if ($option !== null) { |
|
191 | 103 | $name = is_numeric($name) ? $option : $name; |
|
192 | 103 | $attributes[] = $name.'="'.$option.'" '; |
|
193 | 103 | } |
|
194 | 103 | } |
|
195 | |||
196 | 103 | return join('', $attributes); |
|
197 | } |
||
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() |
|
218 | { |
||
219 | 130 | $customFields = (array) $this->getConfig('custom_fields'); |
|
220 | |||
221 | 130 | if (!empty($customFields)) { |
|
222 | 1 | foreach ($customFields as $fieldName => $fieldClass) { |
|
223 | 1 | $this->addCustomField($fieldName, $fieldClass); |
|
224 | 1 | } |
|
225 | 1 | } |
|
226 | 130 | } |
|
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) |
|
266 | { |
||
267 | 101 | if (!$name) { |
|
268 | 1 | return null; |
|
269 | } |
||
270 | |||
271 | 101 | if ($this->translator->has($name)) { |
|
272 | 2 | $translatedName = $this->translator->get($name); |
|
273 | |||
274 | 2 | if (is_string($translatedName)) { |
|
275 | 2 | return $translatedName; |
|
276 | } |
||
277 | } |
||
278 | |||
279 | 99 | return ucfirst(str_replace('_', ' ', $name)); |
|
280 | } |
||
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) |
|
296 | { |
||
297 | 9 | $fieldRules = $field->getValidationRules(); |
|
298 | |||
299 | 9 | if (is_array($fieldRules)) { |
|
300 | $fieldRules = Rules::fromArray($fieldRules)->setFieldName($field->getNameKey()); |
||
301 | } |
||
302 | |||
303 | 9 | $formBuilder = $field->getParent()->getFormBuilder(); |
|
304 | 9 | $formBuilder->fireEvent(new AfterCollectingFieldRules($field, $fieldRules)); |
|
305 | |||
306 | 9 | return $fieldRules; |
|
307 | } |
||
308 | |||
309 | /** |
||
310 | * @param FormField[] $fields |
||
311 | * @return array |
||
312 | */ |
||
313 | 9 | public function mergeFieldsRules($fields) |
|
314 | { |
||
315 | 9 | $rules = new Rules([]); |
|
316 | |||
317 | 9 | foreach ($fields as $field) { |
|
318 | 9 | $rules->append($this->getFieldValidationRules($field)); |
|
319 | 9 | } |
|
320 | |||
321 | 9 | return $rules; |
|
322 | } |
||
323 | |||
324 | /** |
||
325 | * @param array $fields |
||
326 | * @return array |
||
327 | */ |
||
328 | 3 | public function mergeAttributes(array $fields) |
|
329 | { |
||
330 | 3 | $attributes = []; |
|
331 | 3 | foreach ($fields as $field) { |
|
332 | 3 | $attributes = array_merge($attributes, $field->getAllAttributes()); |
|
333 | 3 | } |
|
334 | |||
335 | 3 | return $attributes; |
|
336 | } |
||
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) |
|
346 | { |
||
347 | // Alter the form's child forms recursively |
||
348 | 3 | foreach ($form->getFields() as $name => $field) { |
|
349 | 3 | if (method_exists($field, 'alterFieldValues')) { |
|
350 | 2 | $fullName = $this->transformToDotSyntax($name); |
|
351 | |||
352 | 2 | $subValues = (array) Arr::get($values, $fullName); |
|
353 | 2 | $field->alterFieldValues($subValues); |
|
354 | 2 | Arr::set($values, $fullName, $subValues); |
|
355 | 2 | } |
|
356 | 3 | } |
|
357 | |||
358 | // Alter the form itself |
||
359 | 3 | $form->alterFieldValues($values); |
|
360 | 3 | } |
|
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) |
|
368 | { |
||
369 | // Alter the form itself |
||
370 | 9 | $messages = $form->alterValid($mainForm, $isValid); |
|
371 | |||
372 | // Add messages to the existing Bag |
||
373 | 9 | if ($messages) { |
|
374 | 1 | $messageBag = $mainForm->getValidator()->getMessageBag(); |
|
375 | 1 | $this->appendMessagesWithPrefix($messageBag, $form->getName(), $messages); |
|
376 | 1 | } |
|
377 | |||
378 | // Alter the form's child forms recursively |
||
379 | 9 | foreach ($form->getFields() as $name => $field) { |
|
380 | 9 | if (method_exists($field, 'alterValid')) { |
|
381 | 2 | $field->alterValid($mainForm, $isValid); |
|
382 | 2 | } |
|
383 | 9 | } |
|
384 | 9 | } |
|
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) |
|
392 | { |
||
393 | 1 | foreach ($keyedMessages as $key => $messages) { |
|
394 | 1 | if ($prefix) { |
|
395 | 1 | $key = $this->transformToDotSyntax($prefix . '[' . $key . ']'); |
|
396 | 1 | } |
|
397 | |||
398 | 1 | foreach ((array) $messages as $message) { |
|
399 | 1 | $messageBag->add($key, $message); |
|
400 | 1 | } |
|
401 | 1 | } |
|
402 | 1 | } |
|
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) |
|
444 | { |
||
445 | 70 | if (!$name || trim($name) == '') { |
|
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..