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 | 125 | public function __construct(View $view, Translator $translator, array $config = []) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @param string $key |
||
| 106 | * @param string $default |
||
| 107 | * @return mixed |
||
| 108 | */ |
||
| 109 | 125 | public function getConfig($key, $default = null) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @return View |
||
| 116 | */ |
||
| 117 | 37 | public function getView() |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Merge options array. |
||
| 124 | * |
||
| 125 | * @param array $first |
||
| 126 | * @param array $second |
||
| 127 | * @return array |
||
| 128 | */ |
||
| 129 | 125 | 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 | 82 | public function getFieldType($type) |
|
| 141 | { |
||
| 142 | 82 | $types = array_keys(static::$availableFieldTypes); |
|
| 143 | |||
| 144 | 82 | if (!$type || trim($type) == '') { |
|
| 145 | 1 | throw new \InvalidArgumentException('Field type must be provided.'); |
|
| 146 | } |
||
| 147 | |||
| 148 | 81 | if ($this->hasCustomField($type)) { |
|
| 149 | 2 | return $this->customTypes[$type]; |
|
| 150 | } |
||
| 151 | |||
| 152 | 79 | if (!in_array($type, $types)) { |
|
| 153 | 2 | throw new \InvalidArgumentException( |
|
| 154 | 2 | sprintf( |
|
| 155 | 2 | 'Unsupported field type [%s]. Available types are: %s', |
|
| 156 | 2 | $type, |
|
| 157 | 2 | join(', ', array_merge($types, array_keys($this->customTypes))) |
|
| 158 | 2 | ) |
|
| 159 | 2 | ); |
|
| 160 | } |
||
| 161 | |||
| 162 | 77 | $namespace = __NAMESPACE__.'\\Fields\\'; |
|
| 163 | |||
| 164 | 77 | return $namespace . static::$availableFieldTypes[$type]; |
|
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Convert array of attributes to html attributes. |
||
| 169 | * |
||
| 170 | * @param $options |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | 98 | public function prepareAttributes($options) |
|
| 174 | { |
||
| 175 | 98 | if (!$options) { |
|
| 176 | 12 | return null; |
|
| 177 | } |
||
| 178 | |||
| 179 | 98 | $attributes = []; |
|
| 180 | |||
| 181 | 98 | foreach ($options as $name => $option) { |
|
| 182 | 98 | if ($option !== null) { |
|
| 183 | 98 | $name = is_numeric($name) ? $option : $name; |
|
| 184 | 98 | $attributes[] = $name.'="'.$option.'" '; |
|
| 185 | 98 | } |
|
| 186 | 98 | } |
|
| 187 | |||
| 188 | 98 | return join('', $attributes); |
|
| 189 | } |
||
| 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 | 125 | private function loadCustomTypes() |
|
| 210 | { |
||
| 211 | 125 | $customFields = (array) $this->getConfig('custom_fields'); |
|
| 212 | |||
| 213 | 125 | if (!empty($customFields)) { |
|
| 214 | 1 | foreach ($customFields as $fieldName => $fieldClass) { |
|
| 215 | 1 | $this->addCustomField($fieldName, $fieldClass); |
|
| 216 | 1 | } |
|
| 217 | 1 | } |
|
| 218 | 125 | } |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Check if custom field with provided name exists |
||
| 222 | * @param string $name |
||
| 223 | * @return boolean |
||
| 224 | */ |
||
| 225 | 82 | public function hasCustomField($name) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @param object $model |
||
| 232 | * @return object|null |
||
| 233 | */ |
||
| 234 | 2 | public function convertModelToArray($model) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Format the label to the proper format. |
||
| 253 | * |
||
| 254 | * @param $name |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | 96 | public function formatLabel($name) |
|
| 258 | { |
||
| 259 | 96 | if (!$name) { |
|
| 260 | 1 | return null; |
|
| 261 | } |
||
| 262 | |||
| 263 | 96 | if ($this->translator->has($name)) { |
|
| 264 | 1 | $translatedName = $this->translator->get($name); |
|
| 265 | |||
| 266 | 1 | if (is_string($translatedName)) { |
|
| 267 | 1 | return $translatedName; |
|
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | 95 | return ucfirst(str_replace('_', ' ', $name)); |
|
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param FormField $field |
||
| 276 | * @return RulesParser |
||
| 277 | */ |
||
| 278 | 97 | public function createRulesParser(FormField $field) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * @param FormField[] $fields |
||
| 285 | * @return array |
||
| 286 | */ |
||
| 287 | 9 | public function mergeFieldsRules($fields) |
|
| 288 | { |
||
| 289 | 9 | $rules = []; |
|
| 290 | 9 | $attributes = []; |
|
| 291 | 9 | $messages = []; |
|
| 292 | |||
| 293 | 9 | foreach ($fields as $field) { |
|
| 294 | 9 | if ($fieldRules = $field->getValidationRules()) { |
|
| 295 | 9 | $rules = array_merge($rules, $fieldRules['rules']); |
|
| 296 | 9 | $attributes = array_merge($attributes, $fieldRules['attributes']); |
|
| 297 | 9 | $messages = array_merge($messages, $fieldRules['error_messages']); |
|
| 298 | 9 | } |
|
| 299 | 9 | } |
|
| 300 | |||
| 301 | return [ |
||
| 302 | 9 | 'rules' => $rules, |
|
| 303 | 9 | 'attributes' => $attributes, |
|
| 304 | 'error_messages' => $messages |
||
| 305 | 9 | ]; |
|
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param array $fields |
||
| 310 | * @return array |
||
| 311 | */ |
||
| 312 | 3 | public function mergeAttributes(array $fields) |
|
| 313 | { |
||
| 314 | 3 | $attributes = []; |
|
| 315 | 3 | foreach ($fields as $field) { |
|
| 316 | 3 | $attributes = array_merge($attributes, $field->getAllAttributes()); |
|
| 317 | 3 | } |
|
| 318 | |||
| 319 | 3 | return $attributes; |
|
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Alter a form's values recursively according to its fields. |
||
| 324 | * |
||
| 325 | * @param Form $form |
||
| 326 | * @param array $values |
||
| 327 | * @return void |
||
| 328 | */ |
||
| 329 | 3 | public function alterFieldValues(Form $form, array &$values) |
|
| 330 | { |
||
| 331 | // Alter the form itself |
||
| 332 | 3 | $form->alterFieldValues($values); |
|
| 333 | |||
| 334 | // Alter the form's child forms recursively |
||
| 335 | 3 | foreach ($form->getFields() as $name => $field) { |
|
| 336 | 3 | if (method_exists($field, 'alterFieldValues')) { |
|
| 337 | 2 | $fullName = $this->transformToDotSyntax($name); |
|
| 338 | |||
| 339 | 2 | $subValues = Arr::get($values, $fullName); |
|
| 340 | 2 | $field->alterFieldValues($subValues); |
|
| 341 | 2 | Arr::set($values, $fullName, $subValues); |
|
| 342 | 2 | } |
|
| 343 | 3 | } |
|
| 344 | 3 | } |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Alter a form's validity recursively, and add messages with nested form prefix. |
||
| 348 | * |
||
| 349 | * @return void |
||
| 350 | */ |
||
| 351 | 9 | public function alterValid(Form $form, Form $mainForm, &$isValid) |
|
| 352 | { |
||
| 353 | // Alter the form itself |
||
| 354 | 9 | $messages = $form->alterValid($mainForm, $isValid); |
|
| 355 | |||
| 356 | // Add messages to the existing Bag |
||
| 357 | 9 | if ($messages) { |
|
| 358 | 1 | $messageBag = $mainForm->getValidator()->getMessageBag(); |
|
| 359 | 1 | $this->appendMessagesWithPrefix($messageBag, $form->getName(), $messages); |
|
| 360 | 1 | } |
|
| 361 | |||
| 362 | // Alter the form's child forms recursively |
||
| 363 | 9 | foreach ($form->getFields() as $name => $field) { |
|
| 364 | 9 | if (method_exists($field, 'alterValid')) { |
|
| 365 | 2 | $field->alterValid($mainForm, $isValid); |
|
| 366 | 2 | } |
|
| 367 | 9 | } |
|
| 368 | 9 | } |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Add unprefixed messages with prefix to a MessageBag. |
||
| 372 | * |
||
| 373 | * @return void |
||
| 374 | */ |
||
| 375 | 1 | public function appendMessagesWithPrefix(MessageBag $messageBag, $prefix, array $keyedMessages) |
|
| 376 | { |
||
| 377 | 1 | foreach ($keyedMessages as $key => $messages) { |
|
| 378 | 1 | if ($prefix) { |
|
| 379 | 1 | $key = $this->transformToDotSyntax($prefix . '[' . $key . ']'); |
|
| 380 | 1 | } |
|
| 381 | |||
| 382 | 1 | foreach ((array) $messages as $message) { |
|
| 383 | 1 | $messageBag->add($key, $message); |
|
| 384 | 1 | } |
|
| 385 | 1 | } |
|
| 386 | 1 | } |
|
| 387 | |||
| 388 | /** |
||
| 389 | * @param string $string |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | 97 | public function transformToDotSyntax($string) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * @param string $string |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | 6 | public function transformToBracketSyntax($string) |
|
| 402 | { |
||
| 403 | 6 | $name = explode('.', $string); |
|
| 404 | 6 | if ($name && count($name) == 1) { |
|
| 405 | return $name[0]; |
||
| 406 | } |
||
| 407 | |||
| 408 | 6 | $first = array_shift($name); |
|
| 409 | 6 | return $first . '[' . implode('][', $name) . ']'; |
|
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @return TranslatorInterface |
||
| 414 | */ |
||
| 415 | 3 | public function getTranslator() |
|
| 419 | |||
| 420 | /** |
||
| 421 | * Check if field name is valid and not reserved. |
||
| 422 | * |
||
| 423 | * @throws \InvalidArgumentException |
||
| 424 | * @param string $name |
||
| 425 | * @param string $className |
||
| 426 | */ |
||
| 427 | 66 | public function checkFieldName($name, $className) |
|
| 428 | { |
||
| 429 | 66 | if (!$name || trim($name) == '') { |
|
| 444 | } |
||
| 445 |
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..