Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Form 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 Form, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
75 | class Form |
||
76 | { |
||
77 | /** |
||
78 | * Eloquent model of the form. |
||
79 | * |
||
80 | * @var Model |
||
81 | */ |
||
82 | protected $model; |
||
83 | |||
84 | /** |
||
85 | * @var \Illuminate\Validation\Validator |
||
86 | */ |
||
87 | protected $validator; |
||
88 | |||
89 | /** |
||
90 | * @var Builder |
||
91 | */ |
||
92 | protected $builder; |
||
93 | |||
94 | /** |
||
95 | * Submitted callback. |
||
96 | * |
||
97 | * @var Closure |
||
98 | */ |
||
99 | protected $submitted; |
||
100 | |||
101 | /** |
||
102 | * Saving callback. |
||
103 | * |
||
104 | * @var Closure |
||
105 | */ |
||
106 | protected $saving; |
||
107 | |||
108 | /** |
||
109 | * Saved callback. |
||
110 | * |
||
111 | * @var Closure |
||
112 | */ |
||
113 | protected $saved; |
||
114 | |||
115 | /** |
||
116 | * Data for save to current model from input. |
||
117 | * |
||
118 | * @var array |
||
119 | */ |
||
120 | protected $updates = []; |
||
121 | |||
122 | /** |
||
123 | * Data for save to model's relations from input. |
||
124 | * |
||
125 | * @var array |
||
126 | */ |
||
127 | protected $relations = []; |
||
128 | |||
129 | /** |
||
130 | * Input data. |
||
131 | * |
||
132 | * @var array |
||
133 | */ |
||
134 | protected $inputs = []; |
||
135 | |||
136 | /** |
||
137 | * Available fields. |
||
138 | * |
||
139 | * @var array |
||
140 | */ |
||
141 | public static $availableFields = []; |
||
142 | |||
143 | /** |
||
144 | * Ignored saving fields. |
||
145 | * |
||
146 | * @var array |
||
147 | */ |
||
148 | protected $ignored = []; |
||
149 | |||
150 | /** |
||
151 | * Collected field assets. |
||
152 | * |
||
153 | * @var array |
||
154 | */ |
||
155 | protected static $collectedAssets = []; |
||
156 | |||
157 | /** |
||
158 | * @var Form\Tab |
||
159 | */ |
||
160 | protected $tab = null; |
||
161 | |||
162 | /** |
||
163 | * Remove flag in `has many` form. |
||
164 | */ |
||
165 | const REMOVE_FLAG_NAME = '_remove_'; |
||
166 | |||
167 | /** |
||
168 | * Field rows in form. |
||
169 | * |
||
170 | * @var array |
||
171 | */ |
||
172 | public $rows = []; |
||
173 | |||
174 | /** |
||
175 | * Create a new form instance. |
||
176 | * |
||
177 | * @param $model |
||
178 | * @param \Closure $callback |
||
179 | */ |
||
180 | public function __construct($model, Closure $callback) |
||
181 | { |
||
182 | $this->model = $model; |
||
183 | |||
184 | $this->builder = new Builder($this); |
||
185 | |||
186 | $callback($this); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @param Field $field |
||
191 | * |
||
192 | * @return $this |
||
193 | */ |
||
194 | public function pushField(Field $field) |
||
195 | { |
||
196 | $field->setForm($this); |
||
197 | |||
198 | $this->builder->fields()->push($field); |
||
199 | |||
200 | return $this; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @return Model |
||
205 | */ |
||
206 | public function model() |
||
207 | { |
||
208 | return $this->model; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @return Builder |
||
213 | */ |
||
214 | public function builder() |
||
215 | { |
||
216 | return $this->builder; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Generate a edit form. |
||
221 | * |
||
222 | * @param $id |
||
223 | * |
||
224 | * @return $this |
||
225 | */ |
||
226 | public function edit($id) |
||
227 | { |
||
228 | $this->builder->setMode(Builder::MODE_EDIT); |
||
229 | $this->builder->setResourceId($id); |
||
230 | |||
231 | $this->setFieldValue($id); |
||
232 | |||
233 | return $this; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @param $id |
||
238 | * |
||
239 | * @return $this |
||
240 | */ |
||
241 | public function view($id) |
||
242 | { |
||
243 | $this->builder->setMode(Builder::MODE_VIEW); |
||
244 | $this->builder->setResourceId($id); |
||
245 | |||
246 | $this->setFieldValue($id); |
||
247 | |||
248 | return $this; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Use tab to split form. |
||
253 | * |
||
254 | * @param string $title |
||
255 | * @param Closure $content |
||
256 | * |
||
257 | * @return $this |
||
258 | */ |
||
259 | public function tab($title, Closure $content, $active = false) |
||
260 | { |
||
261 | $this->getTab()->append($title, $content, $active); |
||
262 | |||
263 | return $this; |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Get Tab instance. |
||
268 | * |
||
269 | * @return Tab |
||
270 | */ |
||
271 | public function getTab() |
||
272 | { |
||
273 | if (is_null($this->tab)) { |
||
274 | $this->tab = new Tab($this); |
||
275 | } |
||
276 | |||
277 | return $this->tab; |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Destroy data entity and remove files. |
||
282 | * |
||
283 | * @param $id |
||
284 | * |
||
285 | * @return mixed |
||
286 | */ |
||
287 | public function destroy($id) |
||
288 | { |
||
289 | $ids = explode(',', $id); |
||
290 | |||
291 | foreach ($ids as $id) { |
||
292 | if (empty($id)) { |
||
293 | continue; |
||
294 | } |
||
295 | $this->deleteFilesAndImages($id); |
||
296 | $this->model->find($id)->delete(); |
||
297 | } |
||
298 | |||
299 | return true; |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Remove files or images in record. |
||
304 | * |
||
305 | * @param $id |
||
306 | */ |
||
307 | protected function deleteFilesAndImages($id) |
||
308 | { |
||
309 | $data = $this->model->with($this->getRelations()) |
||
|
|||
310 | ->findOrFail($id)->toArray(); |
||
311 | |||
312 | $this->builder->fields()->filter(function ($field) { |
||
313 | return $field instanceof Field\File; |
||
314 | })->each(function (File $file) use ($data) { |
||
315 | $file->setOriginal($data); |
||
316 | |||
317 | $file->destroy(); |
||
318 | }); |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Store a new record. |
||
323 | * |
||
324 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\Http\JsonResponse |
||
325 | */ |
||
326 | public function store() |
||
327 | { |
||
328 | $data = Input::all(); |
||
329 | |||
330 | // Handle validation errors. |
||
331 | if ($validationMessages = $this->validationMessages($data)) { |
||
332 | return back()->withInput()->withErrors($validationMessages); |
||
333 | } |
||
334 | |||
335 | if (($response = $this->prepare($data)) instanceof Response) { |
||
336 | return $response; |
||
337 | } |
||
338 | |||
339 | View Code Duplication | DB::transaction(function () { |
|
340 | $inserts = $this->prepareInsert($this->updates); |
||
341 | |||
342 | foreach ($inserts as $column => $value) { |
||
343 | $this->model->setAttribute($column, $value); |
||
344 | } |
||
345 | |||
346 | $this->model->save(); |
||
347 | |||
348 | $this->updateRelation($this->relations); |
||
349 | }); |
||
350 | |||
351 | if (($response = $this->complete($this->saved)) instanceof Response) { |
||
352 | return $response; |
||
353 | } |
||
354 | |||
355 | if ($response = $this->ajaxResponse(trans('admin.save_succeeded'))) { |
||
356 | return $response; |
||
357 | } |
||
358 | |||
359 | return $this->redirectAfterStore(); |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * Get RedirectResponse after store. |
||
364 | * |
||
365 | * @return \Illuminate\Http\RedirectResponse |
||
366 | */ |
||
367 | View Code Duplication | protected function redirectAfterStore() |
|
368 | { |
||
369 | admin_toastr(trans('admin.save_succeeded')); |
||
370 | |||
371 | $url = Input::get(Builder::PREVIOUS_URL_KEY) ?: $this->resource(0); |
||
372 | |||
373 | return redirect($url); |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * Get ajax response. |
||
378 | * |
||
379 | * @param string $message |
||
380 | * |
||
381 | * @return bool|\Illuminate\Http\JsonResponse |
||
382 | */ |
||
383 | protected function ajaxResponse($message) |
||
384 | { |
||
385 | $request = Request::capture(); |
||
386 | |||
387 | // ajax but not pjax |
||
388 | if ($request->ajax() && !$request->pjax()) { |
||
389 | return response()->json([ |
||
390 | 'status' => true, |
||
391 | 'message' => $message, |
||
392 | ]); |
||
393 | } |
||
394 | |||
395 | return false; |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Prepare input data for insert or update. |
||
400 | * |
||
401 | * @param array $data |
||
402 | * |
||
403 | * @return mixed |
||
404 | */ |
||
405 | protected function prepare($data = []) |
||
406 | { |
||
407 | if (($response = $this->callSubmitted()) instanceof Response) { |
||
408 | return $response; |
||
409 | } |
||
410 | |||
411 | $this->inputs = $this->removeIgnoredFields($data); |
||
412 | |||
413 | if (($response = $this->callSaving()) instanceof Response) { |
||
414 | return $response; |
||
415 | } |
||
416 | |||
417 | $this->relations = $this->getRelationInputs($this->inputs); |
||
418 | |||
419 | $this->updates = array_except($this->inputs, array_keys($this->relations)); |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * Remove ignored fields from input. |
||
424 | * |
||
425 | * @param array $input |
||
426 | * |
||
427 | * @return array |
||
428 | */ |
||
429 | protected function removeIgnoredFields($input) |
||
430 | { |
||
431 | array_forget($input, $this->ignored); |
||
432 | |||
433 | return $input; |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * Get inputs for relations. |
||
438 | * |
||
439 | * @param array $inputs |
||
440 | * |
||
441 | * @return array |
||
442 | */ |
||
443 | protected function getRelationInputs($inputs = []) |
||
444 | { |
||
445 | $relations = []; |
||
446 | |||
447 | foreach ($inputs as $column => $value) { |
||
448 | if (method_exists($this->model, $column)) { |
||
449 | $relation = call_user_func([$this->model, $column]); |
||
450 | |||
451 | if ($relation instanceof Relation) { |
||
452 | $relations[$column] = $value; |
||
453 | } |
||
454 | } |
||
455 | } |
||
456 | |||
457 | return $relations; |
||
458 | } |
||
459 | |||
460 | /** |
||
461 | * Call submitted callback. |
||
462 | * |
||
463 | * @return mixed |
||
464 | */ |
||
465 | protected function callSubmitted() |
||
466 | { |
||
467 | if ($this->submitted instanceof Closure) { |
||
468 | return call_user_func($this->submitted, $this); |
||
469 | } |
||
470 | } |
||
471 | |||
472 | /** |
||
473 | * Call saving callback. |
||
474 | * |
||
475 | * @return mixed |
||
476 | */ |
||
477 | protected function callSaving() |
||
478 | { |
||
479 | if ($this->saving instanceof Closure) { |
||
480 | return call_user_func($this->saving, $this); |
||
481 | } |
||
482 | } |
||
483 | |||
484 | /** |
||
485 | * Callback after saving a Model. |
||
486 | * |
||
487 | * @param Closure|null $callback |
||
488 | * |
||
489 | * @return mixed|null |
||
490 | */ |
||
491 | protected function complete(Closure $callback = null) |
||
492 | { |
||
493 | if ($callback instanceof Closure) { |
||
494 | return $callback($this); |
||
495 | } |
||
496 | } |
||
497 | |||
498 | /** |
||
499 | * Handle update. |
||
500 | * |
||
501 | * @param int $id |
||
502 | * |
||
503 | * @return \Symfony\Component\HttpFoundation\Response |
||
504 | */ |
||
505 | public function update($id) |
||
506 | { |
||
507 | $data = Input::all(); |
||
508 | |||
509 | $isEditable = $this->isEditable($data); |
||
510 | |||
511 | $data = $this->handleEditable($data); |
||
512 | |||
513 | $data = $this->handleFileDelete($data); |
||
514 | |||
515 | if ($this->handleOrderable($id, $data)) { |
||
516 | return response([ |
||
517 | 'status' => true, |
||
518 | 'message' => trans('admin.update_succeeded'), |
||
519 | ]); |
||
520 | } |
||
521 | |||
522 | /* @var Model $this->model */ |
||
523 | $this->model = $this->model->with($this->getRelations())->findOrFail($id); |
||
524 | |||
525 | $this->setFieldOriginalValue(); |
||
526 | |||
527 | // Handle validation errors. |
||
528 | if ($validationMessages = $this->validationMessages($data)) { |
||
529 | if (!$isEditable) { |
||
530 | return back()->withInput()->withErrors($validationMessages); |
||
531 | } else { |
||
532 | return response()->json(['errors' => array_dot($validationMessages->getMessages())], 422); |
||
533 | } |
||
534 | } |
||
535 | |||
536 | if (($response = $this->prepare($data)) instanceof Response) { |
||
537 | return $response; |
||
538 | } |
||
539 | |||
540 | View Code Duplication | DB::transaction(function () { |
|
541 | $updates = $this->prepareUpdate($this->updates); |
||
542 | |||
543 | foreach ($updates as $column => $value) { |
||
544 | /* @var Model $this->model */ |
||
545 | $this->model->setAttribute($column, $value); |
||
546 | } |
||
547 | |||
548 | $this->model->save(); |
||
549 | |||
550 | $this->updateRelation($this->relations); |
||
551 | }); |
||
552 | |||
553 | if (($result = $this->complete($this->saved)) instanceof Response) { |
||
554 | return $result; |
||
555 | } |
||
556 | |||
557 | if ($response = $this->ajaxResponse(trans('admin.update_succeeded'))) { |
||
558 | return $response; |
||
559 | } |
||
560 | |||
561 | return $this->redirectAfterUpdate(); |
||
562 | } |
||
563 | |||
564 | /** |
||
565 | * Get RedirectResponse after update. |
||
566 | * |
||
567 | * @return \Illuminate\Http\RedirectResponse |
||
568 | */ |
||
569 | View Code Duplication | protected function redirectAfterUpdate() |
|
570 | { |
||
571 | admin_toastr(trans('admin.update_succeeded')); |
||
572 | |||
573 | $url = Input::get(Builder::PREVIOUS_URL_KEY) ?: $this->resource(-1); |
||
574 | |||
575 | return redirect($url); |
||
576 | } |
||
577 | |||
578 | /** |
||
579 | * Check if request is from editable. |
||
580 | * |
||
581 | * @param array $input |
||
582 | * |
||
583 | * @return bool |
||
584 | */ |
||
585 | protected function isEditable(array $input = []) |
||
586 | { |
||
587 | return array_key_exists('_editable', $input); |
||
588 | } |
||
589 | |||
590 | /** |
||
591 | * Handle editable update. |
||
592 | * |
||
593 | * @param array $input |
||
594 | * |
||
595 | * @return array |
||
596 | */ |
||
597 | protected function handleEditable(array $input = []) |
||
609 | |||
610 | /** |
||
611 | * @param array $input |
||
612 | * |
||
613 | * @return array |
||
614 | */ |
||
615 | protected function handleFileDelete(array $input = []) |
||
626 | |||
627 | /** |
||
628 | * Handle orderable update. |
||
629 | * |
||
630 | * @param int $id |
||
631 | * @param array $input |
||
632 | * |
||
633 | * @return bool |
||
634 | */ |
||
635 | protected function handleOrderable($id, array $input = []) |
||
649 | |||
650 | /** |
||
651 | * Update relation data. |
||
652 | * |
||
653 | * @param array $relationsData |
||
654 | * |
||
655 | * @return void |
||
656 | */ |
||
657 | protected function updateRelation($relationsData) |
||
735 | |||
736 | /** |
||
737 | * Prepare input data for update. |
||
738 | * |
||
739 | * @param array $updates |
||
740 | * @param bool $oneToOneRelation If column is one-to-one relation. |
||
741 | * |
||
742 | * @return array |
||
743 | */ |
||
744 | protected function prepareUpdate(array $updates, $oneToOneRelation = false) |
||
775 | |||
776 | /** |
||
777 | * @param string|array $columns |
||
778 | * @param bool $oneToOneRelation |
||
779 | * |
||
780 | * @return bool |
||
781 | */ |
||
782 | protected function invalidColumn($columns, $oneToOneRelation = false) |
||
793 | |||
794 | /** |
||
795 | * Prepare input data for insert. |
||
796 | * |
||
797 | * @param $inserts |
||
798 | * |
||
799 | * @return array |
||
800 | */ |
||
801 | protected function prepareInsert($inserts) |
||
824 | |||
825 | /** |
||
826 | * Is input data is has-one relation. |
||
827 | * |
||
828 | * @param array $inserts |
||
829 | * |
||
830 | * @return bool |
||
831 | */ |
||
832 | protected function isHasOneRelation($inserts) |
||
846 | |||
847 | /** |
||
848 | * Set submitted callback. |
||
849 | * |
||
850 | * @param Closure $callback |
||
851 | * |
||
852 | * @return void |
||
853 | */ |
||
854 | public function submitted(Closure $callback) |
||
858 | |||
859 | /** |
||
860 | * Set saving callback. |
||
861 | * |
||
862 | * @param Closure $callback |
||
863 | * |
||
864 | * @return void |
||
865 | */ |
||
866 | public function saving(Closure $callback) |
||
870 | |||
871 | /** |
||
872 | * Set saved callback. |
||
873 | * |
||
874 | * @param callable $callback |
||
875 | * |
||
876 | * @return void |
||
877 | */ |
||
878 | public function saved(Closure $callback) |
||
882 | |||
883 | /** |
||
884 | * Ignore fields to save. |
||
885 | * |
||
886 | * @param string|array $fields |
||
887 | * |
||
888 | * @return $this |
||
889 | */ |
||
890 | public function ignore($fields) |
||
896 | |||
897 | /** |
||
898 | * @param array $data |
||
899 | * @param string|array $columns |
||
900 | * |
||
901 | * @return array|mixed |
||
902 | */ |
||
903 | View Code Duplication | protected function getDataByColumn($data, $columns) |
|
921 | |||
922 | /** |
||
923 | * Find field object by column. |
||
924 | * |
||
925 | * @param $column |
||
926 | * |
||
927 | * @return mixed |
||
928 | */ |
||
929 | protected function getFieldByColumn($column) |
||
941 | |||
942 | /** |
||
943 | * Set original data for each field. |
||
944 | * |
||
945 | * @return void |
||
946 | */ |
||
947 | protected function setFieldOriginalValue() |
||
957 | |||
958 | /** |
||
959 | * Set all fields value in form. |
||
960 | * |
||
961 | * @param $id |
||
962 | * |
||
963 | * @return void |
||
964 | */ |
||
965 | protected function setFieldValue($id) |
||
979 | |||
980 | /** |
||
981 | * Don't snake case attributes. |
||
982 | * |
||
983 | * @param Model $model |
||
984 | * |
||
985 | * @return void |
||
986 | */ |
||
987 | protected static function doNotSnakeAttributes(Model $model) |
||
993 | |||
994 | /** |
||
995 | * Get validation messages. |
||
996 | * |
||
997 | * @param array $input |
||
998 | * |
||
999 | * @return MessageBag|bool |
||
1000 | */ |
||
1001 | protected function validationMessages($input) |
||
1019 | |||
1020 | /** |
||
1021 | * Merge validation messages from input validators. |
||
1022 | * |
||
1023 | * @param \Illuminate\Validation\Validator[] $validators |
||
1024 | * |
||
1025 | * @return MessageBag |
||
1026 | */ |
||
1027 | protected function mergeValidationMessages($validators) |
||
1037 | |||
1038 | /** |
||
1039 | * Get all relations of model from callable. |
||
1040 | * |
||
1041 | * @return array |
||
1042 | */ |
||
1043 | public function getRelations() |
||
1069 | |||
1070 | /** |
||
1071 | * Set action for form. |
||
1072 | * |
||
1073 | * @param string $action |
||
1074 | * |
||
1075 | * @return $this |
||
1076 | */ |
||
1077 | public function setAction($action) |
||
1083 | |||
1084 | /** |
||
1085 | * Set field and label width in current form. |
||
1086 | * |
||
1087 | * @param int $fieldWidth |
||
1088 | * @param int $labelWidth |
||
1089 | * |
||
1090 | * @return $this |
||
1091 | */ |
||
1092 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
1103 | |||
1104 | /** |
||
1105 | * Set view for form. |
||
1106 | * |
||
1107 | * @param string $view |
||
1108 | * |
||
1109 | * @return $this |
||
1110 | */ |
||
1111 | public function setView($view) |
||
1117 | |||
1118 | /** |
||
1119 | * Add a row in form. |
||
1120 | * |
||
1121 | * @param Closure $callback |
||
1122 | * |
||
1123 | * @return $this |
||
1124 | */ |
||
1125 | public function row(Closure $callback) |
||
1131 | |||
1132 | /** |
||
1133 | * Tools setting for form. |
||
1134 | * |
||
1135 | * @param Closure $callback |
||
1136 | */ |
||
1137 | public function tools(Closure $callback) |
||
1141 | |||
1142 | /** |
||
1143 | * Disable form submit. |
||
1144 | * |
||
1145 | * @return $this |
||
1146 | */ |
||
1147 | public function disableSubmit() |
||
1153 | |||
1154 | /** |
||
1155 | * Disable form reset. |
||
1156 | * |
||
1157 | * @return $this |
||
1158 | */ |
||
1159 | public function disableReset() |
||
1165 | |||
1166 | /** |
||
1167 | * Get current resource route url. |
||
1168 | * |
||
1169 | * @param int $slice |
||
1170 | * |
||
1171 | * @return string |
||
1172 | */ |
||
1173 | public function resource($slice = -2) |
||
1183 | |||
1184 | /** |
||
1185 | * Render the form contents. |
||
1186 | * |
||
1187 | * @return string |
||
1188 | */ |
||
1189 | public function render() |
||
1197 | |||
1198 | /** |
||
1199 | * Get or set input data. |
||
1200 | * |
||
1201 | * @param string $key |
||
1202 | * @param null $value |
||
1203 | * |
||
1204 | * @return array|mixed |
||
1205 | */ |
||
1206 | public function input($key, $value = null) |
||
1214 | |||
1215 | /** |
||
1216 | * Register builtin fields. |
||
1217 | * |
||
1218 | * @return void |
||
1219 | */ |
||
1220 | public static function registerBuiltinFields() |
||
1275 | |||
1276 | /** |
||
1277 | * Register custom field. |
||
1278 | * |
||
1279 | * @param string $abstract |
||
1280 | * @param string $class |
||
1281 | * |
||
1282 | * @return void |
||
1283 | */ |
||
1284 | public static function extend($abstract, $class) |
||
1288 | |||
1289 | /** |
||
1290 | * Remove registered field. |
||
1291 | * |
||
1292 | * @param array|string $abstract |
||
1293 | */ |
||
1294 | public static function forget($abstract) |
||
1298 | |||
1299 | /** |
||
1300 | * Find field class. |
||
1301 | * |
||
1302 | * @param string $method |
||
1303 | * |
||
1304 | * @return bool|mixed |
||
1305 | */ |
||
1306 | public static function findFieldClass($method) |
||
1307 | { |
||
1308 | $class = array_get(static::$availableFields, $method); |
||
1309 | |||
1310 | if (class_exists($class)) { |
||
1311 | return $class; |
||
1312 | } |
||
1313 | |||
1314 | return false; |
||
1315 | } |
||
1316 | |||
1317 | /** |
||
1318 | * Collect assets required by registered field. |
||
1319 | * |
||
1320 | * @return array |
||
1321 | */ |
||
1322 | public static function collectFieldAssets() |
||
1347 | /** |
||
1348 | * Collect rules of all fields. |
||
1349 | * |
||
1350 | * @return array |
||
1351 | */ |
||
1352 | public function getRules() |
||
1353 | { |
||
1354 | $this->builder()->getRules(); |
||
1355 | } |
||
1356 | |||
1357 | /** |
||
1358 | * Collect validation Messages of all fields. |
||
1359 | * |
||
1360 | * @return array |
||
1361 | */ |
||
1362 | public function getRuleMessages() |
||
1363 | { |
||
1364 | $this->builder()->getRuleMessages(); |
||
1365 | } |
||
1366 | |||
1367 | /** |
||
1368 | * Getter. |
||
1369 | * |
||
1370 | * @param string $name |
||
1371 | * |
||
1372 | * @return array|mixed |
||
1373 | */ |
||
1374 | public function __get($name) |
||
1378 | |||
1379 | /** |
||
1380 | * Setter. |
||
1381 | * |
||
1382 | * @param string $name |
||
1383 | * @param $value |
||
1384 | */ |
||
1385 | public function __set($name, $value) |
||
1389 | |||
1390 | /** |
||
1391 | * Generate a Field object and add to form builder if Field exists. |
||
1392 | * |
||
1393 | * @param string $method |
||
1394 | * @param array $arguments |
||
1395 | * |
||
1396 | * @return Field|void |
||
1397 | */ |
||
1398 | View Code Duplication | public function __call($method, $arguments) |
|
1410 | |||
1411 | /** |
||
1412 | * Render the contents of the form when casting to string. |
||
1413 | * |
||
1414 | * @return string |
||
1415 | */ |
||
1416 | public function __toString() |
||
1420 | } |
||
1421 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: