We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
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 |
||
18 | class CrudController extends BaseController |
||
19 | { |
||
20 | use DispatchesJobs, ValidatesRequests; |
||
21 | use AjaxTable, Reorder, Revisions, ShowDetailsRow; |
||
22 | |||
23 | public $data = []; |
||
24 | public $crud; |
||
25 | |||
26 | public function __construct() |
||
27 | { |
||
28 | $this->crud = new CrudPanel(); |
||
29 | |||
30 | // call the setup function inside this closure to also have the request there |
||
31 | // this way, developers can use things stored in session (auth variables, etc) |
||
32 | $this->middleware(function ($request, $next) { |
||
33 | $this->setup(); |
||
34 | |||
35 | return $next($request); |
||
36 | }); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Allow developers to set their configuration options for a CrudPanel. |
||
41 | */ |
||
42 | public function setup() |
||
43 | { |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Display all rows in the database for this entity. |
||
48 | * |
||
49 | * @return Response |
||
50 | */ |
||
51 | public function index() |
||
52 | { |
||
53 | $this->crud->hasAccessOrFail('list'); |
||
54 | |||
55 | $this->data['crud'] = $this->crud; |
||
56 | $this->data['title'] = ucfirst($this->crud->entity_name_plural); |
||
57 | |||
58 | // get all entries if AJAX is not enabled |
||
59 | if (! $this->data['crud']->ajaxTable()) { |
||
60 | $this->data['entries'] = $this->data['crud']->getEntries(); |
||
61 | } |
||
62 | |||
63 | // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
||
64 | // $this->crud->getListView() returns 'list' by default, or 'list_ajax' if ajax was enabled |
||
65 | return view('crud::list', $this->data); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Show the form for creating inserting a new row. |
||
70 | * |
||
71 | * @return Response |
||
72 | */ |
||
73 | public function create() |
||
74 | { |
||
75 | $this->crud->hasAccessOrFail('create'); |
||
76 | |||
77 | // prepare the fields you need to show |
||
78 | $this->data['crud'] = $this->crud; |
||
79 | $this->data['fields'] = $this->crud->getCreateFields(); |
||
80 | $this->data['title'] = trans('backpack::crud.add').' '.$this->crud->entity_name; |
||
81 | |||
82 | // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
||
83 | return view('crud::create', $this->data); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Store a newly created resource in the database. |
||
88 | * |
||
89 | * @param StoreRequest $request - type injection used for validation using Requests |
||
90 | * |
||
91 | * @return \Illuminate\Http\RedirectResponse |
||
92 | */ |
||
93 | public function storeCrud(StoreRequest $request = null) |
||
94 | { |
||
95 | $this->crud->hasAccessOrFail('create'); |
||
96 | |||
97 | // fallback to global request instance |
||
98 | if (is_null($request)) { |
||
99 | $request = \Request::instance(); |
||
100 | } |
||
101 | |||
102 | // replace empty values with NULL, so that it will work with MySQL strict mode on |
||
103 | foreach ($request->input() as $key => $value) { |
||
104 | if (empty($value) && $value !== '0') { |
||
105 | $request->request->set($key, null); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | // insert item in the db |
||
110 | $item = $this->crud->create($request->except(['redirect_after_save', '_token'])); |
||
111 | |||
112 | // show a success message |
||
113 | \Alert::success(trans('backpack::crud.insert_success'))->flash(); |
||
114 | |||
115 | // redirect the user where he chose to be redirected |
||
116 | switch ($request->input('redirect_after_save')) { |
||
117 | case 'current_item_edit': |
||
118 | return \Redirect::to($this->crud->route.'/'.$item->getKey().'/edit'); |
||
119 | |||
120 | default: |
||
121 | return \Redirect::to($request->input('redirect_after_save')); |
||
122 | } |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Show the form for editing the specified resource. |
||
127 | * |
||
128 | * @param int $id |
||
129 | * |
||
130 | * @return Response |
||
131 | */ |
||
132 | public function edit($id) |
||
133 | { |
||
134 | $this->crud->hasAccessOrFail('update'); |
||
135 | |||
136 | // get the info for that entry |
||
137 | $this->data['entry'] = $this->crud->getEntry($id); |
||
138 | $this->data['crud'] = $this->crud; |
||
139 | $this->data['fields'] = $this->crud->getUpdateFields($id); |
||
140 | $this->data['title'] = trans('backpack::crud.edit').' '.$this->crud->entity_name; |
||
141 | |||
142 | $this->data['id'] = $id; |
||
143 | |||
144 | // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
||
145 | return view('crud::edit', $this->data); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Update the specified resource in the database. |
||
150 | * |
||
151 | * @param UpdateRequest $request - type injection used for validation using Requests |
||
152 | * |
||
153 | * @return \Illuminate\Http\RedirectResponse |
||
154 | */ |
||
155 | public function updateCrud(UpdateRequest $request = null) |
||
156 | { |
||
157 | $this->crud->hasAccessOrFail('update'); |
||
158 | |||
159 | // fallback to global request instance |
||
160 | if (is_null($request)) { |
||
161 | $request = \Request::instance(); |
||
162 | } |
||
163 | |||
164 | // replace empty values with NULL, so that it will work with MySQL strict mode on |
||
165 | foreach ($request->input() as $key => $value) { |
||
166 | if (empty($value) && $value !== '0') { |
||
167 | $request->request->set($key, null); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | // update the row in the db |
||
172 | $this->crud->update($request->get($this->crud->model->getKeyName()), |
||
173 | $request->except('redirect_after_save', '_token')); |
||
174 | |||
175 | // show a success message |
||
176 | \Alert::success(trans('backpack::crud.update_success'))->flash(); |
||
177 | |||
178 | return \Redirect::to($this->crud->route); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Display the specified resource. |
||
183 | * |
||
184 | * @param int $id |
||
185 | * |
||
186 | * @return Response |
||
187 | */ |
||
188 | public function show($id) |
||
189 | { |
||
190 | $this->crud->hasAccessOrFail('show'); |
||
191 | |||
192 | // get the info for that entry |
||
193 | $this->data['entry'] = $this->crud->getEntry($id); |
||
194 | $this->data['crud'] = $this->crud; |
||
195 | $this->data['title'] = trans('backpack::crud.preview').' '.$this->crud->entity_name; |
||
196 | |||
197 | // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
||
198 | return view('crud::show', $this->data); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Remove the specified resource from storage. |
||
203 | * |
||
204 | * @param int $id |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | public function destroy($id) |
||
209 | { |
||
210 | $this->crud->hasAccessOrFail('delete'); |
||
211 | |||
212 | return $this->crud->delete($id); |
||
213 | } |
||
214 | <<<<<<< HEAD |
||
215 | |||
361 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.