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 namespace Kris\LaravelFormBuilder; |
||
| 9 | class FormHelper |
||
| 10 | { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @var View |
||
| 14 | */ |
||
| 15 | protected $view; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var TranslatorInterface |
||
| 19 | */ |
||
| 20 | protected $translator; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | protected $config; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var FormBuilder |
||
| 29 | */ |
||
| 30 | protected $formBuilder; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected static $reservedFieldNames = [ |
||
| 36 | 'save' |
||
| 37 | ]; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * All available field types |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected static $availableFieldTypes = [ |
||
| 45 | 'text' => 'InputType', |
||
| 46 | 'email' => 'InputType', |
||
| 47 | 'url' => 'InputType', |
||
| 48 | 'tel' => 'InputType', |
||
| 49 | 'search' => 'InputType', |
||
| 50 | 'password' => 'InputType', |
||
| 51 | 'hidden' => 'InputType', |
||
| 52 | 'number' => 'InputType', |
||
| 53 | 'date' => 'InputType', |
||
| 54 | 'file' => 'InputType', |
||
| 55 | 'image' => 'InputType', |
||
| 56 | 'color' => 'InputType', |
||
| 57 | 'datetime-local' => 'InputType', |
||
| 58 | 'month' => 'InputType', |
||
| 59 | 'range' => 'InputType', |
||
| 60 | 'time' => 'InputType', |
||
| 61 | 'week' => 'InputType', |
||
| 62 | 'select' => 'SelectType', |
||
| 63 | 'textarea' => 'TextareaType', |
||
| 64 | 'button' => 'ButtonType', |
||
| 65 | 'submit' => 'ButtonType', |
||
| 66 | 'reset' => 'ButtonType', |
||
| 67 | 'radio' => 'CheckableType', |
||
| 68 | 'checkbox' => 'CheckableType', |
||
| 69 | 'choice' => 'ChoiceType', |
||
| 70 | 'form' => 'ChildFormType', |
||
| 71 | 'entity' => 'EntityType', |
||
| 72 | 'collection' => 'CollectionType', |
||
| 73 | 'repeated' => 'RepeatedType', |
||
| 74 | 'static' => 'StaticType' |
||
| 75 | ]; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Custom types |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | private $customTypes = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param View $view |
||
| 86 | * @param TranslatorInterface $translator |
||
| 87 | * @param array $config |
||
| 88 | */ |
||
| 89 | 104 | public function __construct(View $view, TranslatorInterface $translator, array $config = []) |
|
| 90 | { |
||
| 91 | 104 | $this->view = $view; |
|
| 92 | 104 | $this->translator = $translator; |
|
| 93 | 104 | $this->config = $config; |
|
| 94 | 104 | $this->loadCustomTypes(); |
|
| 95 | 104 | } |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @param string $key |
||
| 99 | * @param string $default |
||
| 100 | * @return mixed |
||
| 101 | */ |
||
| 102 | 104 | public function getConfig($key, $default = null) |
|
| 103 | { |
||
| 104 | 104 | return array_get($this->config, $key, $default); |
|
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return View |
||
| 109 | */ |
||
| 110 | 35 | public function getView() |
|
| 111 | { |
||
| 112 | 35 | return $this->view; |
|
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Merge options array |
||
| 117 | * |
||
| 118 | * @param array $first |
||
| 119 | * @param array $second |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | 104 | public function mergeOptions(array $first, array $second) |
|
| 123 | { |
||
| 124 | 104 | return array_replace_recursive($first, $second); |
|
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get proper class for field type |
||
| 129 | * |
||
| 130 | * @param $type |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | 67 | public function getFieldType($type) |
|
| 134 | { |
||
| 135 | 67 | $types = array_keys(static::$availableFieldTypes); |
|
| 136 | |||
| 137 | 67 | if (!$type || trim($type) == '') { |
|
| 138 | 1 | throw new \InvalidArgumentException('Field type must be provided.'); |
|
| 139 | } |
||
| 140 | |||
| 141 | 66 | if (array_key_exists($type, $this->customTypes)) { |
|
| 142 | 2 | return $this->customTypes[$type]; |
|
| 143 | } |
||
| 144 | |||
| 145 | 64 | if (!in_array($type, $types)) { |
|
| 146 | 2 | throw new \InvalidArgumentException( |
|
| 147 | sprintf( |
||
| 148 | 2 | 'Unsupported field type [%s]. Available types are: %s', |
|
| 149 | $type, |
||
| 150 | 2 | join(', ', array_merge($types, array_keys($this->customTypes))) |
|
| 151 | ) |
||
| 152 | ); |
||
| 153 | } |
||
| 154 | |||
| 155 | 62 | $namespace = __NAMESPACE__.'\\Fields\\'; |
|
| 156 | |||
| 157 | 62 | return $namespace . static::$availableFieldTypes[$type]; |
|
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Convert array of attributes to html attributes |
||
| 162 | * |
||
| 163 | * @param $options |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | 83 | public function prepareAttributes($options) |
|
| 167 | { |
||
| 168 | 83 | if (!$options) { |
|
| 169 | 4 | return null; |
|
| 170 | } |
||
| 171 | |||
| 172 | 83 | $attributes = []; |
|
| 173 | |||
| 174 | 83 | foreach ($options as $name => $option) { |
|
| 175 | 83 | if ($option !== null) { |
|
| 176 | 83 | $name = is_numeric($name) ? $option : $name; |
|
| 177 | 83 | $attributes[] = $name.'="'.$option.'" '; |
|
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | 83 | return join('', $attributes); |
|
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Add custom field |
||
| 186 | * |
||
| 187 | * @param $name |
||
| 188 | * @param $class |
||
| 189 | */ |
||
| 190 | 3 | public function addCustomField($name, $class) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Load custom field types from config file |
||
| 201 | */ |
||
| 202 | 104 | private function loadCustomTypes() |
|
| 203 | { |
||
| 204 | 104 | $customFields = (array) $this->getConfig('custom_fields'); |
|
| 205 | |||
| 206 | 104 | if (!empty($customFields)) { |
|
| 207 | 1 | foreach ($customFields as $fieldName => $fieldClass) { |
|
| 208 | 1 | $this->addCustomField($fieldName, $fieldClass); |
|
| 209 | } |
||
| 210 | } |
||
| 211 | 104 | } |
|
| 212 | |||
| 213 | 5 | public function convertModelToArray($model) |
|
| 214 | { |
||
| 215 | 5 | if (!$model) { |
|
| 216 | 1 | return null; |
|
| 217 | } |
||
| 218 | |||
| 219 | 5 | if ($model instanceof Model) { |
|
| 220 | 1 | return $model->toArray(); |
|
| 221 | } |
||
| 222 | |||
| 223 | 5 | if ($model instanceof Collection) { |
|
| 224 | 2 | return $model->all(); |
|
| 225 | } |
||
| 226 | |||
| 227 | 5 | return $model; |
|
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Format the label to the proper format |
||
| 232 | * |
||
| 233 | * @param $name |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | 81 | public function formatLabel($name) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * @param FormField[] $fields |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | 7 | public function mergeFieldsRules($fields) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | 1 | public function mergeAttributes(array $fields) |
|
| 282 | { |
||
| 283 | 1 | $attributes = []; |
|
| 284 | 1 | foreach ($fields as $field) { |
|
| 285 | 1 | $attributes = array_merge($attributes, $field->getAllAttributes()); |
|
| 286 | } |
||
| 287 | |||
| 288 | 1 | return $attributes; |
|
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param string $string |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | 82 | public function transformToDotSyntax($string) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * @param string $string |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | 6 | public function transformToBracketSyntax($string) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * @return TranslatorInterface |
||
| 317 | */ |
||
| 318 | 3 | public function getTranslator() |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Check if field name is valid and not reserved |
||
| 325 | * |
||
| 326 | * @throws \InvalidArgumentException |
||
| 327 | * @param string $name |
||
| 328 | * @param string $className |
||
| 329 | */ |
||
| 330 | 53 | public function checkFieldName($name, $className) |
|
| 347 | } |
||
| 348 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: