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 array $customConfig |
||
| 108 | * @return mixed |
||
| 109 | */ |
||
| 110 | 129 | public function getConfig($key = null, $default = null, $customConfig = []) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @return View |
||
| 123 | */ |
||
| 124 | 38 | public function getView() |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Merge options array. |
||
| 131 | * |
||
| 132 | * @param array $first |
||
| 133 | * @param array $second |
||
| 134 | * @return array |
||
| 135 | */ |
||
| 136 | 129 | public function mergeOptions(array $first, array $second) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Get proper class for field type. |
||
| 143 | * |
||
| 144 | * @param $type |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | 86 | public function getFieldType($type) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Convert array of attributes to html attributes. |
||
| 176 | * |
||
| 177 | * @param $options |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | 102 | public function prepareAttributes($options) |
|
| 181 | { |
||
| 182 | 102 | if (!$options) { |
|
| 183 | 13 | return null; |
|
| 184 | } |
||
| 185 | |||
| 186 | 102 | $attributes = []; |
|
| 187 | |||
| 188 | 102 | foreach ($options as $name => $option) { |
|
| 189 | 102 | if ($option !== null) { |
|
| 190 | 102 | $name = is_numeric($name) ? $option : $name; |
|
| 191 | 102 | $attributes[] = $name.'="'.$option.'" '; |
|
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | 102 | return join('', $attributes); |
|
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Add custom field. |
||
| 200 | * |
||
| 201 | * @param $name |
||
| 202 | * @param $class |
||
| 203 | */ |
||
| 204 | 3 | public function addCustomField($name, $class) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Load custom field types from config file. |
||
| 215 | */ |
||
| 216 | 129 | private function loadCustomTypes() |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Check if custom field with provided name exists |
||
| 229 | * @param string $name |
||
| 230 | * @return boolean |
||
| 231 | */ |
||
| 232 | 86 | public function hasCustomField($name) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * @param object $model |
||
| 239 | * @return object|null |
||
| 240 | */ |
||
| 241 | 4 | public function convertModelToArray($model) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Format the label to the proper format. |
||
| 260 | * |
||
| 261 | * @param $name |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | 100 | public function formatLabel($name) |
|
| 265 | { |
||
| 266 | 100 | if (!$name) { |
|
| 267 | 1 | return null; |
|
| 268 | } |
||
| 269 | |||
| 270 | 100 | if ($this->translator->has($name)) { |
|
| 271 | 2 | $translatedName = $this->translator->get($name); |
|
| 272 | |||
| 273 | 2 | if (is_string($translatedName)) { |
|
| 274 | 2 | return $translatedName; |
|
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | 98 | return ucfirst(str_replace('_', ' ', $name)); |
|
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param FormField $field |
||
| 283 | * @return RulesParser |
||
| 284 | */ |
||
| 285 | 101 | public function createRulesParser(FormField $field) |
|
| 286 | { |
||
| 287 | 101 | return new RulesParser($field); |
|
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param FormField[] $fields |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | 9 | public function mergeFieldsRules($fields) |
|
| 295 | { |
||
| 296 | 9 | $rules = []; |
|
| 297 | 9 | $attributes = []; |
|
| 298 | 9 | $messages = []; |
|
| 299 | |||
| 300 | 9 | foreach ($fields as $field) { |
|
| 301 | 9 | if ($fieldRules = $field->getValidationRules()) { |
|
| 302 | 9 | $rules = array_merge($rules, $fieldRules['rules']); |
|
| 303 | 9 | $attributes = array_merge($attributes, $fieldRules['attributes']); |
|
| 304 | 9 | $messages = array_merge($messages, $fieldRules['error_messages']); |
|
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | return [ |
||
| 309 | 9 | 'rules' => $rules, |
|
| 310 | 9 | 'attributes' => $attributes, |
|
| 311 | 9 | 'error_messages' => $messages |
|
| 312 | ]; |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param array $fields |
||
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | 3 | public function mergeAttributes(array $fields) |
|
| 320 | { |
||
| 321 | 3 | $attributes = []; |
|
| 322 | 3 | foreach ($fields as $field) { |
|
| 323 | 3 | $attributes = array_merge($attributes, $field->getAllAttributes()); |
|
| 324 | } |
||
| 325 | |||
| 326 | 3 | return $attributes; |
|
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Alter a form's values recursively according to its fields. |
||
| 331 | * |
||
| 332 | * @param Form $form |
||
| 333 | * @param array $values |
||
| 334 | * @return void |
||
| 335 | */ |
||
| 336 | 3 | public function alterFieldValues(Form $form, array &$values) |
|
| 337 | { |
||
| 338 | // Alter the form itself |
||
| 339 | 3 | $form->alterFieldValues($values); |
|
| 340 | |||
| 341 | // Alter the form's child forms recursively |
||
| 342 | 3 | foreach ($form->getFields() as $name => $field) { |
|
| 343 | 3 | if (method_exists($field, 'alterFieldValues')) { |
|
| 344 | 2 | $fullName = $this->transformToDotSyntax($name); |
|
| 345 | |||
| 346 | 2 | $subValues = Arr::get($values, $fullName); |
|
| 347 | 2 | $field->alterFieldValues($subValues); |
|
| 348 | 3 | Arr::set($values, $fullName, $subValues); |
|
| 349 | } |
||
| 350 | } |
||
| 351 | 3 | } |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Alter a form's validity recursively, and add messages with nested form prefix. |
||
| 355 | * |
||
| 356 | * @return void |
||
| 357 | */ |
||
| 358 | 9 | public function alterValid(Form $form, Form $mainForm, &$isValid) |
|
| 359 | { |
||
| 360 | // Alter the form itself |
||
| 361 | 9 | $messages = $form->alterValid($mainForm, $isValid); |
|
| 362 | |||
| 363 | // Add messages to the existing Bag |
||
| 364 | 9 | if ($messages) { |
|
| 365 | 1 | $messageBag = $mainForm->getValidator()->getMessageBag(); |
|
| 366 | 1 | $this->appendMessagesWithPrefix($messageBag, $form->getName(), $messages); |
|
| 367 | } |
||
| 368 | |||
| 369 | // Alter the form's child forms recursively |
||
| 370 | 9 | foreach ($form->getFields() as $name => $field) { |
|
| 371 | 9 | if (method_exists($field, 'alterValid')) { |
|
| 372 | 9 | $field->alterValid($mainForm, $isValid); |
|
| 373 | } |
||
| 374 | } |
||
| 375 | 9 | } |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Add unprefixed messages with prefix to a MessageBag. |
||
| 379 | * |
||
| 380 | * @return void |
||
| 381 | */ |
||
| 382 | 1 | public function appendMessagesWithPrefix(MessageBag $messageBag, $prefix, array $keyedMessages) |
|
| 383 | { |
||
| 384 | 1 | foreach ($keyedMessages as $key => $messages) { |
|
| 385 | 1 | if ($prefix) { |
|
| 386 | 1 | $key = $this->transformToDotSyntax($prefix . '[' . $key . ']'); |
|
| 387 | } |
||
| 388 | |||
| 389 | 1 | foreach ((array) $messages as $message) { |
|
| 390 | 1 | $messageBag->add($key, $message); |
|
| 391 | } |
||
| 392 | } |
||
| 393 | 1 | } |
|
| 394 | |||
| 395 | /** |
||
| 396 | * @param string $string |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | 101 | public function transformToDotSyntax($string) |
|
| 400 | { |
||
| 401 | 101 | return str_replace(['.', '[]', '[', ']'], ['_', '', '.', ''], $string); |
|
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param string $string |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | 6 | public function transformToBracketSyntax($string) |
|
| 409 | { |
||
| 410 | 6 | $name = explode('.', $string); |
|
| 411 | 6 | if ($name && count($name) == 1) { |
|
| 412 | return $name[0]; |
||
| 413 | } |
||
| 414 | |||
| 415 | 6 | $first = array_shift($name); |
|
| 416 | 6 | return $first . '[' . implode('][', $name) . ']'; |
|
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @return TranslatorInterface |
||
| 421 | */ |
||
| 422 | 3 | public function getTranslator() |
|
| 423 | { |
||
| 424 | 3 | return $this->translator; |
|
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Check if field name is valid and not reserved. |
||
| 429 | * |
||
| 430 | * @throws \InvalidArgumentException |
||
| 431 | * @param string $name |
||
| 432 | * @param string $className |
||
| 433 | */ |
||
| 434 | 69 | public function checkFieldName($name, $className) |
|
| 451 | } |
||
| 452 |
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..