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 |
||
15 | class CrudController extends BaseController |
||
16 | { |
||
17 | use DispatchesJobs, ValidatesRequests; |
||
18 | |||
19 | public $data = []; |
||
20 | public $crud; |
||
21 | |||
22 | public function __construct() |
||
26 | |||
27 | /** |
||
28 | * Display all rows in the database for this entity. |
||
29 | * |
||
30 | * @return Response |
||
|
|||
31 | */ |
||
32 | public function index() |
||
33 | { |
||
34 | $this->crud->hasAccessOrFail('list'); |
||
35 | |||
36 | $this->data['crud'] = $this->crud; |
||
37 | $this->data['title'] = ucfirst($this->crud->entity_name_plural); |
||
38 | |||
39 | // get all entries if AJAX is not enabled |
||
40 | if (! $this->data['crud']->ajaxTable()) { |
||
41 | $this->data['entries'] = $this->data['crud']->getEntries(); |
||
42 | } |
||
43 | |||
44 | // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
||
45 | // $this->crud->getListView() returns 'list' by default, or 'list_ajax' if ajax was enabled |
||
46 | return view('crud::list', $this->data); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Show the form for creating inserting a new row. |
||
51 | * |
||
52 | * @return Response |
||
53 | */ |
||
54 | View Code Duplication | public function create() |
|
66 | |||
67 | /** |
||
68 | * Store a newly created resource in the database. |
||
69 | * |
||
70 | * @param StoreRequest $request - type injection used for validation using Requests |
||
71 | * |
||
72 | * @return \Illuminate\Http\RedirectResponse |
||
73 | */ |
||
74 | public function storeCrud(StoreRequest $request = null) |
||
75 | { |
||
76 | $this->crud->hasAccessOrFail('create'); |
||
77 | |||
78 | // fallback to global request instance |
||
79 | if (is_null($request)) { |
||
80 | $request = \Request::instance(); |
||
81 | } |
||
82 | |||
83 | // replace empty values with NULL, so that it will work with MySQL strict mode on |
||
84 | View Code Duplication | foreach ($request->input() as $key => $value) { |
|
85 | if (empty($value) && $value !== '0') { |
||
86 | $request->request->set($key, null); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | // insert item in the db |
||
91 | $item = $this->crud->create($request->except(['redirect_after_save', 'password', '_token'])); |
||
92 | |||
93 | // show a success message |
||
94 | \Alert::success(trans('backpack::crud.insert_success'))->flash(); |
||
95 | |||
96 | // redirect the user where he chose to be redirected |
||
97 | switch ($request->input('redirect_after_save')) { |
||
98 | case 'current_item_edit': |
||
99 | return \Redirect::to($this->crud->route.'/'.$item->getKey().'/edit'); |
||
100 | |||
101 | default: |
||
102 | return \Redirect::to($request->input('redirect_after_save')); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Show the form for editing the specified resource. |
||
108 | * |
||
109 | * @param int $id |
||
110 | * |
||
111 | * @return Response |
||
112 | */ |
||
113 | View Code Duplication | public function edit($id) |
|
128 | |||
129 | /** |
||
130 | * Update the specified resource in the database. |
||
131 | * |
||
132 | * @param UpdateRequest $request - type injection used for validation using Requests |
||
133 | * |
||
134 | * @return \Illuminate\Http\RedirectResponse |
||
135 | */ |
||
136 | public function updateCrud(UpdateRequest $request = null) |
||
137 | { |
||
138 | $this->crud->hasAccessOrFail('update'); |
||
139 | |||
140 | // fallback to global request instance |
||
141 | if (is_null($request)) { |
||
142 | $request = \Request::instance(); |
||
143 | } |
||
144 | |||
145 | // replace empty values with NULL, so that it will work with MySQL strict mode on |
||
146 | View Code Duplication | foreach ($request->input() as $key => $value) { |
|
147 | if (empty($value) && $value !== '0') { |
||
148 | $request->request->set($key, null); |
||
149 | } |
||
150 | } |
||
151 | |||
152 | // update the row in the db |
||
153 | $this->crud->update($request->get($this->crud->model->getKeyName()), |
||
154 | $request->except('redirect_after_save', '_token')); |
||
155 | |||
156 | // show a success message |
||
157 | \Alert::success(trans('backpack::crud.update_success'))->flash(); |
||
158 | |||
159 | return \Redirect::to($this->crud->route); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Display the specified resource. |
||
164 | * |
||
165 | * @param int $id |
||
166 | * |
||
167 | * @return Response |
||
168 | */ |
||
169 | View Code Duplication | public function show($id) |
|
181 | |||
182 | /** |
||
183 | * Display the revisions for specified resource. |
||
184 | * |
||
185 | * @param int $id |
||
186 | * |
||
187 | * @return Response |
||
188 | */ |
||
189 | View Code Duplication | public function listRevisions($id) |
|
203 | |||
204 | /** |
||
205 | * Restore a specific revision for the specified resource. |
||
206 | * |
||
207 | * Used via AJAX in the revisions view |
||
208 | * |
||
209 | * @param int $id |
||
210 | * |
||
211 | * @return JSON Response containing the new revision that was created from the update |
||
212 | * @return HTTP 500 if the request did not contain the revision ID |
||
213 | */ |
||
214 | public function restoreRevision($id) |
||
236 | |||
237 | /** |
||
238 | * Remove the specified resource from storage. |
||
239 | * |
||
240 | * @param int $id |
||
241 | * |
||
242 | * @return string |
||
243 | */ |
||
244 | public function destroy($id) |
||
250 | |||
251 | /** |
||
252 | * Reorder the items in the database using the Nested Set pattern. |
||
253 | * |
||
254 | * Database columns needed: id, parent_id, lft, rgt, depth, name/title |
||
255 | * |
||
256 | * @return Response |
||
257 | */ |
||
258 | public function reorder() |
||
274 | |||
275 | /** |
||
276 | * Save the new order, using the Nested Set pattern. |
||
277 | * |
||
278 | * Database columns needed: id, parent_id, lft, rgt, depth, name/title |
||
279 | * |
||
280 | * @return |
||
281 | */ |
||
282 | public function saveReorder() |
||
296 | |||
297 | /** |
||
298 | * Used with AJAX in the list view (datatables) to show extra information about that row that didn't fit in the table. |
||
299 | * It defaults to showing some dummy text. |
||
300 | * |
||
301 | * It's enabled by: |
||
302 | * - setting: $crud->details_row = true; |
||
303 | * - adding the details route for the entity; ex: Route::get('page/{id}/details', 'PageCrudController@showDetailsRow'); |
||
304 | * - adding a view with the following name to change what the row actually contains: app/resources/views/vendor/backpack/crud/details_row.blade.php |
||
305 | */ |
||
306 | public function showDetailsRow($id) |
||
316 | |||
317 | /** |
||
318 | * Respond with the JSON of one or more rows, depending on the POST parameters. |
||
319 | * @return JSON Array of cells in HTML form. |
||
320 | */ |
||
321 | public function search() |
||
358 | |||
359 | /** |
||
360 | * Checks if the given string is unique, and return existing entity. |
||
361 | * @return JSON containing success, message and data. |
||
362 | */ |
||
363 | public function unicity() |
||
393 | } |
||
394 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.