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 |
||
| 14 | class UserController extends Controller |
||
| 15 | { |
||
| 16 | use ModelForm; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Index interface. |
||
| 20 | * |
||
| 21 | * @return Content |
||
| 22 | */ |
||
| 23 | public function index() |
||
| 24 | { |
||
| 25 | return Admin::content(function (Content $content) { |
||
| 26 | $content->header('All users'); |
||
| 27 | $content->description('description'); |
||
| 28 | |||
| 29 | $content->body($this->grid()); |
||
| 30 | }); |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Edit interface. |
||
| 35 | * |
||
| 36 | * @param $id |
||
| 37 | * |
||
| 38 | * @return Content |
||
| 39 | */ |
||
| 40 | public function edit($id) |
||
| 41 | { |
||
| 42 | return Admin::content(function (Content $content) use ($id) { |
||
| 43 | $content->header('Edit user'); |
||
| 44 | $content->description('description'); |
||
| 45 | |||
| 46 | $content->body($this->form()->edit($id)); |
||
| 47 | }); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Create interface. |
||
| 52 | * |
||
| 53 | * @return Content |
||
| 54 | */ |
||
| 55 | public function create() |
||
| 56 | { |
||
| 57 | return Admin::content(function (Content $content) { |
||
| 58 | $content->header('Create user'); |
||
| 59 | |||
| 60 | $content->body($this->form()); |
||
| 61 | }); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Make a grid builder. |
||
| 66 | * |
||
| 67 | * @return Grid |
||
| 68 | */ |
||
| 69 | protected function grid() |
||
| 70 | { |
||
| 71 | return Admin::grid(User::class, function (Grid $grid) { |
||
| 72 | $grid->id('ID')->sortable(); |
||
| 73 | |||
| 74 | $grid->username(); |
||
| 75 | $grid->email(); |
||
| 76 | $grid->mobile(); |
||
| 77 | $grid->full_name(); |
||
| 78 | $grid->avatar()->display(function ($avatar) { |
||
| 79 | return "<img src='{$avatar}' />"; |
||
| 80 | }); |
||
| 81 | $grid->profile()->postcode('Post code'); |
||
| 82 | $grid->profile()->address(); |
||
| 83 | $grid->position('Position'); |
||
| 84 | $grid->column('profile.color'); |
||
| 85 | $grid->profile()->start_at('开始时间'); |
||
| 86 | $grid->profile()->end_at('结束时间'); |
||
| 87 | |||
| 88 | $grid->column('column1_not_in_table')->display(function () { |
||
| 89 | return 'full name:'.$this->full_name; |
||
| 90 | }); |
||
| 91 | |||
| 92 | $grid->column('column2_not_in_table')->display(function () { |
||
| 93 | return $this->email.'#'.$this->profile['color']; |
||
| 94 | }); |
||
| 95 | |||
| 96 | $grid->tags()->display(function ($tags) { |
||
| 97 | $tags = collect($tags)->map(function ($tag) { |
||
| 98 | return "<code>{$tag['name']}</code>"; |
||
| 99 | })->toArray(); |
||
| 100 | |||
| 101 | return implode('', $tags); |
||
| 102 | }); |
||
| 103 | |||
| 104 | $grid->created_at(); |
||
| 105 | $grid->updated_at(); |
||
| 106 | |||
| 107 | $grid->filter(function ($filter) { |
||
| 108 | $filter->like('username'); |
||
| 109 | $filter->like('email'); |
||
| 110 | $filter->like('profile.postcode'); |
||
| 111 | $filter->between('profile.start_at')->datetime(); |
||
| 112 | $filter->between('profile.end_at')->datetime(); |
||
| 113 | }); |
||
| 114 | |||
| 115 | $grid->actions(function ($actions) { |
||
| 116 | if ($actions->getKey() % 2 == 0) { |
||
| 117 | $actions->append('<a href="/" class="btn btn-xs btn-danger">detail</a>'); |
||
| 118 | } |
||
| 119 | }); |
||
| 120 | }); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Make a form builder. |
||
| 125 | * |
||
| 126 | * @return Form |
||
| 127 | */ |
||
| 128 | protected function form() |
||
| 129 | { |
||
| 130 | Form::extend('map', Form\Field\Map::class); |
||
| 131 | Form::extend('editor', Form\Field\Editor::class); |
||
| 132 | |||
| 133 | return Admin::form(User::class, function (Form $form) { |
||
| 134 | $form->disableDeletion(); |
||
| 135 | |||
| 136 | $form->display('id', 'ID'); |
||
| 137 | $form->text('username'); |
||
| 138 | $form->email('email')->rules('required'); |
||
| 139 | $form->mobile('mobile'); |
||
| 140 | $form->image('avatar')->help('上传头像', 'fa-image'); |
||
| 141 | $form->ignore(['password_confirmation']); |
||
| 142 | $form->password('password')->rules('confirmed'); |
||
| 143 | $form->password('password_confirmation'); |
||
| 144 | |||
| 145 | $form->divider(); |
||
| 146 | |||
| 147 | $form->text('profile.first_name'); |
||
| 148 | $form->text('profile.last_name'); |
||
| 149 | $form->text('profile.postcode')->help('Please input your postcode'); |
||
| 150 | $form->textarea('profile.address')->rows(15); |
||
| 151 | $form->map('profile.latitude', 'profile.longitude', 'Position'); |
||
| 152 | $form->color('profile.color'); |
||
| 153 | $form->datetime('profile.start_at'); |
||
| 154 | $form->datetime('profile.end_at'); |
||
| 155 | |||
| 156 | $form->multipleSelect('tags', 'Tags')->options(Tag::all()->pluck('name', 'id')); //->rules('max:10|min:3'); |
||
| 157 | |||
| 158 | $form->display('created_at', 'Created At'); |
||
| 159 | $form->display('updated_at', 'Updated At'); |
||
| 160 | |||
| 161 | $form->html('<a html-field>html...</a>'); |
||
| 162 | }); |
||
| 163 | } |
||
| 164 | } |
||
| 165 |