Complex classes like FormDefault 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 FormDefault, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 21 | class FormDefault extends FormElements implements DisplayInterface, FormInterface | ||
| 22 | { | ||
| 23 | use HtmlAttributes; | ||
| 24 | |||
| 25 | /** | ||
| 26 | * View to render. | ||
| 27 | * @var string|\Illuminate\View\View | ||
| 28 | */ | ||
| 29 | protected $view = 'form.default'; | ||
| 30 | |||
| 31 | /** | ||
| 32 | * Form related class. | ||
| 33 | * @var string | ||
| 34 | */ | ||
| 35 | protected $class; | ||
| 36 | |||
| 37 | /** | ||
| 38 | * @var FormButtons | ||
| 39 | */ | ||
| 40 | protected $buttons; | ||
| 41 | |||
| 42 | /** | ||
| 43 | * Form related repository. | ||
| 44 | * @var RepositoryInterface | ||
| 45 | */ | ||
| 46 | protected $repository; | ||
| 47 | |||
| 48 | /** | ||
| 49 | * Form action url. | ||
| 50 | * @var string | ||
| 51 | */ | ||
| 52 | protected $action; | ||
| 53 | |||
| 54 | /** | ||
| 55 | * Form related model instance. | ||
| 56 | * @var Model | ||
| 57 | */ | ||
| 58 | protected $model; | ||
| 59 | |||
| 60 | /** | ||
| 61 | * Currently loaded model id. | ||
| 62 | * @var int | ||
| 63 | */ | ||
| 64 | protected $id; | ||
| 65 | |||
| 66 | /** | ||
| 67 | * Is form already initialized? | ||
| 68 | * @var bool | ||
| 69 | */ | ||
| 70 | protected $initialized = false; | ||
| 71 | |||
| 72 | /** | ||
| 73 | * FormDefault constructor. | ||
| 74 | * | ||
| 75 | * @param array $elements | ||
| 76 | */ | ||
| 77 | public function __construct(array $elements = []) | ||
| 78 |     { | ||
| 79 | parent::__construct($elements); | ||
| 80 | |||
| 81 | $this->setButtons( | ||
| 82 | app(FormButtonsInterface::class) | ||
| 83 | ); | ||
| 84 | } | ||
| 85 | |||
| 86 | /** | ||
| 87 | * Initialize form. | ||
| 88 | */ | ||
| 89 | public function initialize() | ||
| 90 |     { | ||
| 91 |         if ($this->initialized) { | ||
| 92 | return; | ||
| 93 | } | ||
| 94 | |||
| 95 | $this->initialized = true; | ||
| 96 | $this->repository = app(RepositoryInterface::class, [$this->class]); | ||
| 97 | $this->setModel( | ||
| 98 | $this->makeModel() | ||
| 99 | ); | ||
| 100 | |||
| 101 | parent::initialize(); | ||
| 102 | |||
| 103 |         if (! $this->hasHtmlAttribute('enctype')) { | ||
|  | |||
| 104 | // Try to find upload element | ||
| 105 |             $this->getElements()->each(function ($element) { | ||
| 106 | |||
| 107 | // TODO: this not works withs nested elements | ||
| 108 |                 if ($element instanceof Upload and ! $this->hasHtmlAttribute('enctype')) { | ||
| 109 |                     $this->setHtmlAttribute('enctype', 'multipart/form-data'); | ||
| 110 | } | ||
| 111 | }); | ||
| 112 | } | ||
| 113 | |||
| 114 | $this->getButtons()->setModelConfiguration( | ||
| 115 | $this->getModelConfiguration() | ||
| 116 | ); | ||
| 117 | } | ||
| 118 | |||
| 119 | /** | ||
| 120 | * @return FormButtons | ||
| 121 | */ | ||
| 122 | public function getButtons() | ||
| 123 |     { | ||
| 124 | return $this->buttons; | ||
| 125 | } | ||
| 126 | |||
| 127 | /** | ||
| 128 | * @param FormButtonsInterface $buttons | ||
| 129 | * | ||
| 130 | * @return $this | ||
| 131 | */ | ||
| 132 | public function setButtons(FormButtonsInterface $buttons) | ||
| 133 |     { | ||
| 134 | $this->buttons = $buttons; | ||
| 135 | |||
| 136 | return $this; | ||
| 137 | } | ||
| 138 | |||
| 139 | /** | ||
| 140 | * @return RepositoryInterface | ||
| 141 | */ | ||
| 142 | public function getRepository() | ||
| 143 |     { | ||
| 144 | return $this->repository; | ||
| 145 | } | ||
| 146 | |||
| 147 | /** | ||
| 148 | * @return string | ||
| 149 | */ | ||
| 150 | public function getAction() | ||
| 151 |     { | ||
| 152 | return $this->action; | ||
| 153 | } | ||
| 154 | |||
| 155 | /** | ||
| 156 | * @param string $action | ||
| 157 | * | ||
| 158 | * @return $this | ||
| 159 | */ | ||
| 160 | public function setAction($action) | ||
| 161 |     { | ||
| 162 | $this->action = $action; | ||
| 163 | |||
| 164 | return $this; | ||
| 165 | } | ||
| 166 | |||
| 167 | /** | ||
| 168 | * @return string | ||
| 169 | */ | ||
| 170 | public function getClass() | ||
| 171 |     { | ||
| 172 | return $this->class; | ||
| 173 | } | ||
| 174 | |||
| 175 | /** | ||
| 176 | * @param string $class | ||
| 177 | * | ||
| 178 | * @return $this | ||
| 179 | * @throws FormException | ||
| 180 | */ | ||
| 181 | public function setModelClass($class) | ||
| 182 |     { | ||
| 183 |         if (is_null($this->class)) { | ||
| 184 | $this->class = $class; | ||
| 185 | } | ||
| 186 | |||
| 187 | return $this; | ||
| 188 | } | ||
| 189 | |||
| 190 | /** | ||
| 191 | * @deprecated 4.5.0 | ||
| 192 | * @see getElements() | ||
| 193 | * | ||
| 194 | * @return Collection[] | ||
| 195 | */ | ||
| 196 | public function getItems() | ||
| 200 | |||
| 201 | /** | ||
| 202 | * @deprecated 4.5.0 | ||
| 203 | * @see setElements() | ||
| 204 | * | ||
| 205 | * @param array|FormElementInterface $items | ||
| 206 | * | ||
| 207 | * @return $this | ||
| 208 | */ | ||
| 209 | public function setItems($items) | ||
| 217 | |||
| 218 | /** | ||
| 219 | * @deprecated 4.5.0 | ||
| 220 | * @see addElement() | ||
| 221 | * | ||
| 222 | * @param FormElementInterface $item | ||
| 223 | * | ||
| 224 | * @return $this | ||
| 225 | */ | ||
| 226 | public function addItem($item) | ||
| 230 | |||
| 231 | /** | ||
| 232 | * Set currently loaded model id. | ||
| 233 | * | ||
| 234 | * @param int $id | ||
| 235 | */ | ||
| 236 | public function setId($id) | ||
| 244 | |||
| 245 | /** | ||
| 246 | * Get related form model configuration. | ||
| 247 | * @return ModelConfigurationInterface | ||
| 248 | */ | ||
| 249 | public function getModelConfiguration() | ||
| 253 | |||
| 254 | /** | ||
| 255 | * @return Model | ||
| 256 | */ | ||
| 257 | public function getModel() | ||
| 261 | |||
| 262 | /** | ||
| 263 | * @param Model $model | ||
| 264 | * | ||
| 265 | * @return $this | ||
| 266 | */ | ||
| 267 | public function setModel(Model $model) | ||
| 274 | |||
| 275 | /** | ||
| 276 | * Save instance. | ||
| 277 | * | ||
| 278 | * @param ModelConfigurationInterface $modelConfiguration | ||
| 279 | * | ||
| 280 | * @return bool | ||
| 281 | */ | ||
| 282 | public function saveForm(ModelConfigurationInterface $modelConfiguration) | ||
| 315 | |||
| 316 | /** | ||
| 317 | * @param Model $model | ||
| 318 | * | ||
| 319 | * @return void | ||
| 320 | */ | ||
| 321 | protected function saveBelongsToRelations(Model $model) | ||
| 330 | |||
| 331 | /** | ||
| 332 | * @param Model $model | ||
| 333 | * | ||
| 334 | * @return void | ||
| 335 | */ | ||
| 336 | protected function saveHasOneRelations(Model $model) | ||
| 348 | |||
| 349 | /** | ||
| 350 | * @param ModelConfigurationInterface $modelConfiguration | ||
| 351 | * | ||
| 352 | * @throws ValidationException | ||
| 353 | * @return void | ||
| 354 | */ | ||
| 355 | public function validateForm(ModelConfigurationInterface $modelConfiguration) | ||
| 379 | |||
| 380 | /** | ||
| 381 | * Get the instance as an array. | ||
| 382 | * | ||
| 383 | * @return array | ||
| 384 | */ | ||
| 385 | public function toArray() | ||
| 399 | |||
| 400 | /** | ||
| 401 | * @return Model | ||
| 402 | */ | ||
| 403 | protected function makeModel() | ||
| 409 | } | ||
| 410 | 
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: