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 |
||
| 20 | class FormDefault extends FormElements implements DisplayInterface, FormInterface |
||
| 21 | { |
||
| 22 | use HtmlAttributes; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * View to render. |
||
| 26 | * @var string|\Illuminate\View\View |
||
| 27 | */ |
||
| 28 | protected $view = 'form.default'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Form related class. |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $class; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var FormButtons |
||
| 38 | */ |
||
| 39 | protected $buttons; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Form related repository. |
||
| 43 | * @var RepositoryInterface |
||
| 44 | */ |
||
| 45 | protected $repository; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Form action url. |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $action; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Form related model instance. |
||
| 55 | * @var Model |
||
| 56 | */ |
||
| 57 | protected $model; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Currently loaded model id. |
||
| 61 | * @var int |
||
| 62 | */ |
||
| 63 | protected $id; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Is form already initialized? |
||
| 67 | * @var bool |
||
| 68 | */ |
||
| 69 | protected $initialized = false; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * FormDefault constructor. |
||
| 73 | * |
||
| 74 | * @param array $elements |
||
| 75 | */ |
||
| 76 | public function __construct(array $elements = []) |
||
| 77 | { |
||
| 78 | parent::__construct($elements); |
||
| 79 | |||
| 80 | $this->setButtons( |
||
| 81 | app(FormButtonsInterface::class) |
||
| 82 | ); |
||
| 83 | |||
| 84 | $this->initializePackage(); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Initialize form. |
||
| 89 | */ |
||
| 90 | public function initialize() |
||
| 91 | { |
||
| 92 | if ($this->initialized) { |
||
| 93 | return; |
||
| 94 | } |
||
| 95 | |||
| 96 | $this->initialized = true; |
||
| 97 | $this->repository = app(RepositoryInterface::class, [$this->class]); |
||
| 98 | |||
| 99 | $this->setModel(app($this->class)); |
||
| 100 | |||
| 101 | parent::initialize(); |
||
| 102 | |||
| 103 | $this->getElements()->each(function ($element) { |
||
| 104 | if ($element instanceof Upload and ! $this->hasHtmlAttribute('enctype')) { |
||
| 105 | $this->setHtmlAttribute('enctype', 'multipart/form-data'); |
||
| 106 | } |
||
| 107 | }); |
||
| 108 | |||
| 109 | $this->setHtmlAttribute('method', 'POST'); |
||
| 110 | |||
| 111 | $this->getButtons()->setModelConfiguration( |
||
| 112 | $this->getModelConfiguration() |
||
| 113 | ); |
||
| 114 | |||
| 115 | $this->includePackage(); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @return FormButtons |
||
| 120 | */ |
||
| 121 | public function getButtons() |
||
| 122 | { |
||
| 123 | return $this->buttons; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param FormButtonsInterface $buttons |
||
| 128 | * |
||
| 129 | * @return $this |
||
| 130 | */ |
||
| 131 | public function setButtons(FormButtonsInterface $buttons) |
||
| 132 | { |
||
| 133 | $this->buttons = $buttons; |
||
| 134 | |||
| 135 | return $this; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @return RepositoryInterface |
||
| 140 | */ |
||
| 141 | public function getRepository() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return string|\Illuminate\View\View |
||
| 148 | */ |
||
| 149 | public function getView() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param \Illuminate\View\View|string $view |
||
| 156 | * |
||
| 157 | * @return $this |
||
| 158 | */ |
||
| 159 | public function setView($view) |
||
| 160 | { |
||
| 161 | $this->view = $view; |
||
| 162 | |||
| 163 | return $this; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | public function getAction() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param string $action |
||
| 176 | * |
||
| 177 | * @return $this |
||
| 178 | */ |
||
| 179 | public function setAction($action) |
||
| 180 | { |
||
| 181 | if (is_null($this->action)) { |
||
| 182 | $this->action = $action; |
||
| 183 | } |
||
| 184 | |||
| 185 | $this->setHtmlAttribute('action', $this->action); |
||
| 186 | |||
| 187 | return $this; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public function getClass() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param string $class |
||
| 200 | * |
||
| 201 | * @return $this |
||
| 202 | */ |
||
| 203 | public function setModelClass($class) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @deprecated 4.5.0 |
||
| 214 | * @see getElements() |
||
| 215 | * |
||
| 216 | * @return Collection[] |
||
| 217 | */ |
||
| 218 | public function getItems() |
||
| 219 | { |
||
| 220 | return $this->getElements(); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @deprecated 4.5.0 |
||
| 225 | * @see setElements() |
||
| 226 | * |
||
| 227 | * @param array|FormElementInterface $items |
||
| 228 | * |
||
| 229 | * @return $this |
||
| 230 | */ |
||
| 231 | public function setItems($items) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @deprecated 4.5.0 |
||
| 242 | * @see addElement() |
||
| 243 | * |
||
| 244 | * @param FormElementInterface $item |
||
| 245 | * |
||
| 246 | * @return $this |
||
| 247 | */ |
||
| 248 | public function addItem($item) |
||
| 249 | { |
||
| 250 | return $this->addElement($item); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Set currently loaded model id. |
||
| 255 | * |
||
| 256 | * @param int $id |
||
| 257 | */ |
||
| 258 | public function setId($id) |
||
| 259 | { |
||
| 260 | if (is_null($this->id) and ! is_null($id) and ($model = $this->getRepository()->find($id))) { |
||
| 261 | $this->id = $id; |
||
| 262 | $this->setModel($model); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Get related form model configuration. |
||
| 268 | * @return ModelConfigurationInterface |
||
| 269 | */ |
||
| 270 | public function getModelConfiguration() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @return Model |
||
| 277 | */ |
||
| 278 | public function getModel() |
||
| 279 | { |
||
| 280 | return $this->model; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param Model $model |
||
| 285 | * |
||
| 286 | * @return $this |
||
| 287 | */ |
||
| 288 | public function setModel(Model $model) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Save instance. |
||
| 301 | * |
||
| 302 | * @param ModelConfigurationInterface $modelConfiguration |
||
| 303 | * |
||
| 304 | * @return bool |
||
| 305 | */ |
||
| 306 | public function saveForm(ModelConfigurationInterface $modelConfiguration) |
||
| 337 | |||
| 338 | protected function saveBelongsToRelations() |
||
| 349 | |||
| 350 | protected function saveHasOneRelations() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @param ModelConfigurationInterface $modelConfiguration |
||
| 367 | * |
||
| 368 | * @return \Illuminate\Contracts\Validation\Validator|null|bool |
||
| 369 | */ |
||
| 370 | public function validateForm(ModelConfigurationInterface $modelConfiguration) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Get the instance as an array. |
||
| 395 | * |
||
| 396 | * @return array |
||
| 397 | */ |
||
| 398 | public function toArray() |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
||
| 411 | */ |
||
| 412 | public function render() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | public function __toString() |
||
| 424 | } |
||
| 425 |
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: