Complex classes like FormBuilder 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 FormBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Arcanedev\LaravelHtml; |
||
17 | class FormBuilder extends Builder implements FormBuilderContract |
||
18 | { |
||
19 | /* ----------------------------------------------------------------- |
||
20 | | Properties |
||
21 | | ----------------------------------------------------------------- |
||
22 | */ |
||
23 | |||
24 | /** |
||
25 | * The HTML builder instance. |
||
26 | * |
||
27 | * @var \Arcanedev\LaravelHtml\Contracts\HtmlBuilder |
||
28 | */ |
||
29 | protected $html; |
||
30 | |||
31 | /** |
||
32 | * The URL generator instance. |
||
33 | * |
||
34 | * @var \Illuminate\Contracts\Routing\UrlGenerator |
||
35 | */ |
||
36 | protected $url; |
||
37 | |||
38 | /** |
||
39 | * The CSRF token used by the form builder. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $csrfToken; |
||
44 | |||
45 | /** |
||
46 | * The session store implementation. |
||
47 | * |
||
48 | * @var \Illuminate\Contracts\Session\Session |
||
49 | */ |
||
50 | protected $session; |
||
51 | |||
52 | /** |
||
53 | * The current model instance for the form. |
||
54 | * |
||
55 | * @var \Illuminate\Database\Eloquent\Model |
||
56 | */ |
||
57 | protected $model; |
||
58 | |||
59 | /** |
||
60 | * An array of label names we've created. |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $labels = []; |
||
65 | |||
66 | /** |
||
67 | * The reserved form open attributes. |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $reserved = ['method', 'url', 'route', 'action', 'files']; |
||
72 | |||
73 | /** |
||
74 | * The form methods that should be spoofed, in uppercase. |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $spoofedMethods = ['DELETE', 'PATCH', 'PUT']; |
||
79 | |||
80 | /** |
||
81 | * The types of inputs to not fill values on by default. |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | protected $skipValueTypes = ['file', 'password', 'checkbox', 'radio']; |
||
86 | |||
87 | /* ----------------------------------------------------------------- |
||
88 | | Constructor |
||
89 | | ----------------------------------------------------------------- |
||
90 | */ |
||
91 | |||
92 | /** |
||
93 | * Create a new form builder instance. |
||
94 | * |
||
95 | * @param \Arcanedev\LaravelHtml\Contracts\HtmlBuilder $html |
||
96 | * @param \Illuminate\Contracts\Routing\UrlGenerator $url |
||
97 | * @param \Illuminate\Contracts\Session\Session $session |
||
98 | */ |
||
99 | 303 | public function __construct( |
|
110 | |||
111 | /* ----------------------------------------------------------------- |
||
112 | | Getters & Setters |
||
113 | | ----------------------------------------------------------------- |
||
114 | */ |
||
115 | |||
116 | /** |
||
117 | * Get the session store implementation. |
||
118 | * |
||
119 | * @return \Illuminate\Contracts\Session\Session |
||
120 | */ |
||
121 | 3 | public function getSessionStore() |
|
125 | |||
126 | /** |
||
127 | * Set the session store implementation. |
||
128 | * |
||
129 | * @param \Illuminate\Contracts\Session\Session $session |
||
130 | * |
||
131 | * @return self |
||
132 | */ |
||
133 | 303 | public function setSessionStore(Session $session) |
|
139 | |||
140 | /** |
||
141 | * Set the model instance on the form builder. |
||
142 | * |
||
143 | * @param \Illuminate\Database\Eloquent\Model $model |
||
144 | * |
||
145 | * @return self |
||
146 | */ |
||
147 | 33 | public function setModel($model) |
|
153 | |||
154 | /** |
||
155 | * Get the model instance on the form builder. |
||
156 | * |
||
157 | * @return \Illuminate\Database\Eloquent\Model |
||
158 | */ |
||
159 | public function getModel() |
||
163 | |||
164 | 249 | /** |
|
165 | 97 | * Get the ID attribute for a field name. |
|
166 | * |
||
167 | 234 | * @param string $name |
|
168 | 82 | * @param array $attributes |
|
169 | * |
||
170 | 228 | * @return string |
|
171 | */ |
||
172 | public function getIdAttribute($name, array $attributes) |
||
182 | |||
183 | 222 | /** |
|
184 | 80 | * Get the value that should be assigned to the field. |
|
185 | * |
||
186 | 213 | * @param string $name |
|
187 | 79 | * @param mixed $value |
|
188 | * |
||
189 | 207 | * @return mixed |
|
190 | 147 | */ |
|
191 | public function getValueAttribute($name, $value = null) |
||
206 | 24 | ||
207 | /** |
||
208 | 24 | * Get the model value that should be assigned to the field. |
|
209 | 8 | * |
|
210 | 24 | * @param string $name |
|
211 | * @param \Illuminate\Database\Eloquent\Model $model |
||
212 | * |
||
213 | * @return mixed |
||
214 | */ |
||
215 | private function getModelValueAttribute($name, $model = null) |
||
235 | |||
236 | 222 | /** |
|
237 | * Get a value from the session's old input. |
||
238 | * |
||
239 | * @param string $name |
||
240 | * |
||
241 | * @return mixed |
||
242 | */ |
||
243 | public function old($name) |
||
249 | |||
250 | /** |
||
251 | * Transform key from array to dot syntax. |
||
252 | * |
||
253 | * @param string $key |
||
254 | * |
||
255 | * @return string |
||
256 | 48 | */ |
|
257 | private function transformKey($key) |
||
261 | |||
262 | /** |
||
263 | * Determine if the old input is empty. |
||
264 | * |
||
265 | * @return bool |
||
266 | */ |
||
267 | public function oldInputIsEmpty() |
||
271 | |||
272 | /** |
||
273 | * Parse the form action method. |
||
274 | * |
||
275 | 48 | * @param string $method |
|
276 | * |
||
277 | 48 | * @return string |
|
278 | */ |
||
279 | private function getMethod($method) |
||
285 | 48 | ||
286 | /* ----------------------------------------------------------------- |
||
287 | 48 | | Main Methods |
|
288 | 6 | | ----------------------------------------------------------------- |
|
289 | 2 | */ |
|
290 | |||
291 | /** |
||
292 | * Open up a new HTML form. |
||
293 | * |
||
294 | 48 | * @param array $options |
|
295 | 48 | * |
|
296 | 16 | * @return \Illuminate\Support\HtmlString |
|
297 | */ |
||
298 | public function open(array $options = []) |
||
334 | 6 | ||
335 | /** |
||
336 | 6 | * Create a new model based form builder. |
|
337 | * |
||
338 | * @param mixed $model |
||
339 | * @param array $options |
||
340 | * |
||
341 | * @return \Illuminate\Support\HtmlString |
||
342 | */ |
||
343 | public function model($model, array $options = []) |
||
349 | |||
350 | 21 | /** |
|
351 | * Close the current form. |
||
352 | * |
||
353 | * @return \Illuminate\Support\HtmlString |
||
354 | */ |
||
355 | public function close() |
||
362 | |||
363 | 15 | /** |
|
364 | * Generate a hidden field with the current CSRF token. |
||
365 | 15 | * |
|
366 | * @return \Illuminate\Support\HtmlString |
||
367 | 15 | */ |
|
368 | 15 | public function token() |
|
376 | |||
377 | /** |
||
378 | * Create a form label element. |
||
379 | * |
||
380 | * @param string $name |
||
381 | * @param string $value |
||
382 | 189 | * @param array $options |
|
383 | * @param bool $escaped |
||
384 | 189 | * |
|
385 | 189 | * @return \Illuminate\Support\HtmlString |
|
386 | 63 | */ |
|
387 | public function label($name, $value = null, array $options = [], $escaped = true) |
||
395 | 51 | ||
396 | /** |
||
397 | * Create a form input field. |
||
398 | * |
||
399 | * @param string $type |
||
400 | 189 | * @param string $name |
|
401 | * @param string $value |
||
402 | 189 | * @param array $options |
|
403 | * |
||
404 | * @return \Illuminate\Support\HtmlString |
||
405 | */ |
||
406 | public function input($type, $name, $value = null, array $options = []) |
||
428 | |||
429 | 12 | /** |
|
430 | 3 | * Create a text input field. |
|
431 | * |
||
432 | * @param string $name |
||
433 | * @param string $value |
||
434 | * @param array $options |
||
435 | * |
||
436 | * @return \Illuminate\Support\HtmlString |
||
437 | */ |
||
438 | public function text($name, $value = null, array $options = []) |
||
442 | |||
443 | 30 | /** |
|
444 | * Create a password input field. |
||
445 | * |
||
446 | * @param string $name |
||
447 | * @param array $options |
||
448 | * |
||
449 | * @return \Illuminate\Support\HtmlString |
||
450 | */ |
||
451 | public function password($name, array $options = []) |
||
455 | 9 | ||
456 | /** |
||
457 | 9 | * Create a hidden input field. |
|
458 | * |
||
459 | * @param string $name |
||
460 | * @param string $value |
||
461 | * @param array $options |
||
462 | * |
||
463 | * @return \Illuminate\Support\HtmlString |
||
464 | */ |
||
465 | public function hidden($name, $value = null, array $options = []) |
||
469 | 9 | ||
470 | /** |
||
471 | 9 | * Create an e-mail input field. |
|
472 | * |
||
473 | * @param string $name |
||
474 | * @param string $value |
||
475 | * @param array $options |
||
476 | * |
||
477 | * @return \Illuminate\Support\HtmlString |
||
478 | */ |
||
479 | public function email($name, $value = null, array $options = []) |
||
483 | 9 | ||
484 | /** |
||
485 | 9 | * Create a tel input field. |
|
486 | * |
||
487 | * @param string $name |
||
488 | * @param string $value |
||
489 | * @param array $options |
||
490 | * |
||
491 | * @return \Illuminate\Support\HtmlString |
||
492 | */ |
||
493 | public function tel($name, $value = null, array $options = []) |
||
497 | 12 | ||
498 | /** |
||
499 | 12 | * Create a number input field. |
|
500 | 3 | * |
|
501 | 1 | * @param string $name |
|
502 | * @param string $value |
||
503 | 12 | * @param array $options |
|
504 | * |
||
505 | * @return \Illuminate\Support\HtmlString |
||
506 | */ |
||
507 | public function number($name, $value = null, array $options = []) |
||
511 | |||
512 | /** |
||
513 | * Create a date input field. |
||
514 | * |
||
515 | 12 | * @param string $name |
|
516 | * @param string $value |
||
517 | 12 | * @param array $options |
|
518 | 6 | * |
|
519 | 2 | * @return \Illuminate\Support\HtmlString |
|
520 | */ |
||
521 | 12 | public function date($name, $value = null, array $options = []) |
|
529 | |||
530 | /** |
||
531 | * Create a datetime input field. |
||
532 | * |
||
533 | 12 | * @param string $name |
|
534 | * @param string $value |
||
535 | 12 | * @param array $options |
|
536 | 6 | * |
|
537 | 2 | * @return \Illuminate\Support\HtmlString |
|
538 | */ |
||
539 | 12 | public function datetime($name, $value = null, array $options = []) |
|
547 | |||
548 | /** |
||
549 | * Create a datetime-local input field. |
||
550 | * |
||
551 | 9 | * @param string $name |
|
552 | * @param string $value |
||
553 | 9 | * @param array $options |
|
554 | * |
||
555 | * @return \Illuminate\Support\HtmlString |
||
556 | */ |
||
557 | public function datetimeLocal($name, $value = null, array $options = []) |
||
565 | 6 | ||
566 | /** |
||
567 | 6 | * Create a time input field. |
|
568 | * |
||
569 | * @param string $name |
||
570 | * @param string $value |
||
571 | * @param array $options |
||
572 | * |
||
573 | * @return \Illuminate\Support\HtmlString |
||
574 | */ |
||
575 | public function time($name, $value = null, array $options = []) |
||
579 | |||
580 | 9 | /** |
|
581 | * Create a url input field. |
||
582 | * |
||
583 | * @param string $name |
||
584 | * @param string $value |
||
585 | * @param array $options |
||
586 | * |
||
587 | * @return \Illuminate\Support\HtmlString |
||
588 | */ |
||
589 | public function url($name, $value = null, array $options = []) |
||
593 | |||
594 | 18 | /** |
|
595 | 18 | * Create a file input field. |
|
596 | 6 | * |
|
597 | * @param string $name |
||
598 | * @param array $options |
||
599 | * |
||
600 | * @return \Illuminate\Support\HtmlString |
||
601 | 18 | */ |
|
602 | 18 | public function file($name, array $options = []) |
|
606 | |||
607 | /** |
||
608 | * Create a textarea input field. |
||
609 | * |
||
610 | 18 | * @param string $name |
|
611 | * @param string $value |
||
612 | 18 | * @param array $options |
|
613 | * |
||
614 | * @return \Illuminate\Support\HtmlString |
||
615 | */ |
||
616 | public function textarea($name, $value = null, array $options = []) |
||
638 | |||
639 | /** |
||
640 | * Set the text area size on the attributes. |
||
641 | * |
||
642 | * @param array $options |
||
643 | * |
||
644 | 9 | * @return array |
|
645 | */ |
||
646 | 9 | private function setTextAreaSize(array $options) |
|
660 | |||
661 | /** |
||
662 | * Set the text area size using the quick "size" attribute. |
||
663 | * |
||
664 | 42 | * @param array $options |
|
665 | * |
||
666 | * @return array |
||
667 | */ |
||
668 | protected function setQuickTextAreaSize(array $options) |
||
674 | 1 | ||
675 | /** |
||
676 | 42 | * Create a select box field. |
|
677 | * |
||
678 | 42 | * @param string $name |
|
679 | 30 | * @param array|\Illuminate\Support\Collection $list |
|
680 | 10 | * @param string $selected |
|
681 | * @param array $attributes |
||
682 | * @param array $options |
||
683 | * |
||
684 | * @return \Illuminate\Support\HtmlString |
||
685 | 42 | */ |
|
686 | public function select($name, $list = [], $selected = null, array $attributes = [], array $options = []) |
||
727 | |||
728 | /** |
||
729 | * Create a select range field. |
||
730 | * |
||
731 | * @param string $name |
||
732 | * @param string $begin |
||
733 | 3 | * @param string $end |
|
734 | * @param string $selected |
||
735 | 3 | * @param array $options |
|
736 | 3 | * |
|
737 | 3 | * @return \Illuminate\Support\HtmlString |
|
738 | 1 | */ |
|
739 | public function selectRange($name, $begin, $end, $selected = null, array $options = []) |
||
745 | |||
746 | /** |
||
747 | * Create a select year field. |
||
748 | * |
||
749 | * @param string $name |
||
750 | * @param string $begin |
||
751 | 3 | * @param string $end |
|
752 | * @param string $selected |
||
753 | 3 | * @param array $options |
|
754 | * |
||
755 | 3 | * @return \Illuminate\Support\HtmlString |
|
756 | 3 | */ |
|
757 | 1 | public function selectYear($name, $begin, $end, $selected = null, array $options = []) |
|
764 | |||
765 | /** |
||
766 | * Create a select month field. |
||
767 | * |
||
768 | * @param string $name |
||
769 | * @param string $selected |
||
770 | * @param array $options |
||
771 | 39 | * @param string $format |
|
772 | * |
||
773 | 39 | * @return \Illuminate\Support\HtmlString |
|
774 | 17 | */ |
|
775 | 39 | public function selectMonth($name, $selected = null, array $options = [], $format = '%B') |
|
785 | |||
786 | /** |
||
787 | 6 | * Get the select option for the given value. |
|
788 | * |
||
789 | 6 | * @param string $display |
|
790 | * @param string $value |
||
791 | 6 | * @param string $selected |
|
792 | 6 | * @param array $attributes |
|
793 | 2 | * |
|
794 | * @return string |
||
795 | 6 | */ |
|
796 | private function getSelectOption($display, $value, $selected, array $attributes = []) |
||
802 | |||
803 | /** |
||
804 | * Create an option group form element. |
||
805 | * |
||
806 | * @param array $list |
||
807 | 39 | * @param string $label |
|
808 | * @param string $selected |
||
809 | 39 | * @param array $attributes |
|
810 | 39 | * |
|
811 | * @return string |
||
812 | 39 | */ |
|
813 | private function optionGroup(array $list, $label, $selected, array $attributes = []) |
||
823 | 3 | ||
824 | /** |
||
825 | 3 | * Create a select element option. |
|
826 | 3 | * |
|
827 | 3 | * @param string $display |
|
828 | * @param string $value |
||
829 | 3 | * @param string $selected |
|
830 | * @param array $attributes |
||
831 | * |
||
832 | * @return string |
||
833 | */ |
||
834 | private function option($display, $value, $selected, array $attributes = []) |
||
841 | |||
842 | 39 | /** |
|
843 | 12 | * Create a placeholder select element option. |
|
844 | * |
||
845 | * @param string $display |
||
846 | 30 | * @param string $selected |
|
847 | * |
||
848 | * @return string |
||
849 | */ |
||
850 | private function placeholderOption($display, $selected) |
||
857 | |||
858 | /** |
||
859 | 12 | * Determine if the value is selected. |
|
860 | * |
||
861 | 12 | * @param string $value |
|
862 | * @param string $selected |
||
863 | * |
||
864 | * @return string|null |
||
865 | */ |
||
866 | private function getSelectedValue($value, $selected) |
||
874 | 6 | ||
875 | /** |
||
876 | 6 | * Create a checkbox input field. |
|
877 | 3 | * |
|
878 | 1 | * @param string $name |
|
879 | * @param mixed $value |
||
880 | 6 | * @param bool|null $checked |
|
881 | * @param array $options |
||
882 | * |
||
883 | * @return \Illuminate\Support\HtmlString |
||
884 | */ |
||
885 | public function checkbox($name, $value = 1, $checked = null, array $options = []) |
||
889 | |||
890 | /** |
||
891 | * Create a radio button input field. |
||
892 | * |
||
893 | * @param string $name |
||
894 | 21 | * @param mixed $value |
|
895 | * @param bool $checked |
||
896 | 21 | * @param array $options |
|
897 | * |
||
898 | 21 | * @return \Illuminate\Support\HtmlString |
|
899 | 18 | */ |
|
900 | 6 | public function radio($name, $value = null, $checked = null, array $options = []) |
|
906 | |||
907 | /** |
||
908 | * Create a checkable input field. |
||
909 | * |
||
910 | * @param string $type |
||
911 | * @param string $name |
||
912 | * @param mixed $value |
||
913 | * @param bool|null $checked |
||
914 | * @param array $options |
||
915 | 21 | * |
|
916 | * @return \Illuminate\Support\HtmlString |
||
917 | */ |
||
918 | 21 | protected function checkable($type, $name, $value, $checked, array $options) |
|
928 | |||
929 | /** |
||
930 | * Get the check state for a checkable input. |
||
931 | * |
||
932 | * @param string $type |
||
933 | * @param string $name |
||
934 | * @param mixed $value |
||
935 | * @param bool|null $checked |
||
936 | * |
||
937 | * @return bool |
||
938 | 12 | */ |
|
939 | private function getCheckedState($type, $name, $value, $checked) |
||
952 | |||
953 | /** |
||
954 | 6 | * Get the check state for a checkbox input. |
|
955 | 3 | * |
|
956 | * @param string $name |
||
957 | * @param mixed $value |
||
958 | 6 | * @param bool|null $checked |
|
959 | * |
||
960 | * @return bool |
||
961 | */ |
||
962 | private function getCheckboxCheckedState($name, $value, $checked) |
||
984 | 18 | ||
985 | /** |
||
986 | 18 | * Get the check state for a radio input. |
|
987 | * |
||
988 | * @param string $name |
||
989 | * @param mixed $value |
||
990 | * @param bool|null $checked |
||
991 | * |
||
992 | * @return bool |
||
993 | */ |
||
994 | private function getRadioCheckedState($name, $value, $checked) |
||
1000 | |||
1001 | /** |
||
1002 | * Determine if old input or model input exists for a key. |
||
1003 | * |
||
1004 | * @param string $name |
||
1005 | * |
||
1006 | * @return bool |
||
1007 | */ |
||
1008 | private function missingOldAndModel($name) |
||
1012 | |||
1013 | 3 | /** |
|
1014 | * Create a HTML reset input element. |
||
1015 | 3 | * |
|
1016 | * @param string $value |
||
1017 | * @param array $attributes |
||
1018 | * |
||
1019 | * @return \Illuminate\Support\HtmlString |
||
1020 | */ |
||
1021 | public function reset($value, array $attributes = []) |
||
1025 | |||
1026 | 3 | /** |
|
1027 | * Create a HTML image input element. |
||
1028 | 3 | * |
|
1029 | * @param string $url |
||
1030 | * @param string $name |
||
1031 | * @param array $attributes |
||
1032 | * |
||
1033 | * @return \Illuminate\Support\HtmlString |
||
1034 | */ |
||
1035 | public function image($url, $name = null, array $attributes = []) |
||
1041 | 3 | ||
1042 | 3 | /** |
|
1043 | 1 | * Create a submit button element. |
|
1044 | * |
||
1045 | 3 | * @param string $value |
|
1046 | 3 | * @param array $options |
|
1047 | 1 | * |
|
1048 | * @return \Illuminate\Support\HtmlString |
||
1049 | */ |
||
1050 | public function submit($value = null, array $options = []) |
||
1054 | |||
1055 | /** |
||
1056 | * Create a button element. |
||
1057 | * |
||
1058 | * @param string $value |
||
1059 | 9 | * @param array $options |
|
1060 | * |
||
1061 | 9 | * @return \Illuminate\Support\HtmlString |
|
1062 | */ |
||
1063 | public function button($value = null, array $options = []) |
||
1073 | |||
1074 | /** |
||
1075 | * Create a color input field. |
||
1076 | 48 | * |
|
1077 | * @param string $name |
||
1078 | * @param string $value |
||
1079 | * @param array $options |
||
1080 | * |
||
1081 | 48 | * @return \Illuminate\Support\HtmlString |
|
1082 | 15 | */ |
|
1083 | public function color($name, $value = null, array $options = []) |
||
1087 | 33 | ||
1088 | /* ----------------------------------------------------------------- |
||
1089 | | Other Methods |
||
1090 | | ----------------------------------------------------------------- |
||
1091 | 3 | */ |
|
1092 | |||
1093 | /** |
||
1094 | 30 | * Get the form action from the options. |
|
1095 | * |
||
1096 | * @param array $options |
||
1097 | * |
||
1098 | * @return string |
||
1099 | */ |
||
1100 | private function getAction(array $options) |
||
1120 | 3 | ||
1121 | 1 | /** |
|
1122 | 3 | * Get the action for a "url" option. |
|
1123 | * |
||
1124 | * @param array|string $options |
||
1125 | * |
||
1126 | * @return string |
||
1127 | */ |
||
1128 | private function getUrlAction($options) |
||
1134 | 3 | ||
1135 | 1 | /** |
|
1136 | 3 | * Get the action for a "route" option. |
|
1137 | * |
||
1138 | * @param array|string $options |
||
1139 | * |
||
1140 | * @return string |
||
1141 | */ |
||
1142 | private function getRouteAction($options) |
||
1148 | 48 | ||
1149 | /** |
||
1150 | * Get the action for an "action" option. |
||
1151 | * |
||
1152 | * @param array|string $options |
||
1153 | 48 | * |
|
1154 | 6 | * @return string |
|
1155 | 2 | */ |
|
1156 | private function getControllerAction($options) |
||
1162 | 7 | ||
1163 | /** |
||
1164 | 48 | * Get the form appendage for the given method. |
|
1165 | * |
||
1166 | * @param string $method |
||
1167 | * |
||
1168 | * @return string |
||
1169 | */ |
||
1170 | private function getAppendage($method) |
||
1190 | } |
||
1191 |
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: