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:
| 1 | <?php |
||
| 13 | class FileController extends Controller |
||
| 14 | { |
||
| 15 | use ModelForm; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Index interface. |
||
| 19 | * |
||
| 20 | * @return Content |
||
| 21 | */ |
||
| 22 | public function index() |
||
| 23 | { |
||
| 24 | return Admin::content(function (Content $content) { |
||
| 25 | $content->header('header'); |
||
| 26 | $content->description('description'); |
||
| 27 | |||
| 28 | $content->body($this->grid()); |
||
| 29 | }); |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Edit interface. |
||
| 34 | * |
||
| 35 | * @param $id |
||
| 36 | * |
||
| 37 | * @return Content |
||
| 38 | */ |
||
| 39 | public function edit($id) |
||
| 40 | { |
||
| 41 | return Admin::content(function (Content $content) use ($id) { |
||
| 42 | $content->header('header'); |
||
| 43 | $content->description('description'); |
||
| 44 | |||
| 45 | $content->body($this->form()->edit($id)); |
||
| 46 | }); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Create interface. |
||
| 51 | * |
||
| 52 | * @return Content |
||
| 53 | */ |
||
| 54 | public function create() |
||
| 55 | { |
||
| 56 | return Admin::content(function (Content $content) { |
||
| 57 | $content->header('Upload file'); |
||
| 58 | |||
| 59 | $content->body($this->form()); |
||
| 60 | }); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Make a grid builder. |
||
| 65 | * |
||
| 66 | * @return Grid |
||
| 67 | */ |
||
| 68 | protected function grid() |
||
| 69 | { |
||
| 70 | return Admin::grid(File::class, function (Grid $grid) { |
||
| 71 | $grid->id('ID')->sortable(); |
||
| 72 | |||
| 73 | $grid->created_at(); |
||
| 74 | $grid->updated_at(); |
||
| 75 | }); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Make a form builder. |
||
| 80 | * |
||
| 81 | * @return Form |
||
| 82 | */ |
||
| 83 | protected function form() |
||
| 84 | { |
||
| 85 | return Admin::form(File::class, function (Form $form) { |
||
| 86 | $form->display('id', 'ID'); |
||
| 87 | |||
| 88 | $form->file('file1'); |
||
| 89 | $form->file('file2'); |
||
| 90 | $form->file('file3'); |
||
| 91 | $form->file('file4'); |
||
| 92 | $form->file('file5'); |
||
| 93 | $form->file('file6'); |
||
| 94 | |||
| 95 | $form->display('created_at', 'Created At'); |
||
| 96 | $form->display('updated_at', 'Updated At'); |
||
| 97 | }); |
||
| 98 | } |
||
| 99 | } |
||
| 100 |