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 |
||
| 11 | trait UpdateHandlerTrait |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Handle update action. |
||
| 15 | * |
||
| 16 | * @param Request $request |
||
| 17 | * @param $id |
||
| 18 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
| 19 | * @throws PermissionDenied |
||
| 20 | * @throws UnauthorizedException |
||
| 21 | */ |
||
| 22 | 4 | public function handleUpdate(Request $request, $id) |
|
| 23 | { |
||
| 24 | 4 | $this->beforeInit(); |
|
| 25 | 4 | $this->init(); |
|
| 26 | 4 | $this->bound(); |
|
| 27 | |||
| 28 | 4 | if (!$this->can('update')) { |
|
| 29 | 2 | throw UnauthorizedException::forPermissions(['update']); |
|
| 30 | } |
||
| 31 | |||
| 32 | 2 | $model = $this->crud()->repo()->find($id); |
|
| 33 | 2 | if (!$this->crud()->actions()->isAllowed('edit', $model)) { |
|
| 34 | 1 | throw new PermissionDenied(); |
|
| 35 | } |
||
| 36 | |||
| 37 | 1 | $fields = $this->crud()->getFieldsWithoutMarkup(); |
|
| 38 | |||
| 39 | 1 | $inputs = []; |
|
| 40 | /** @var AbstractField $field */ |
||
| 41 | 1 | View Code Duplication | foreach ($fields as $field) { |
|
|
|||
| 42 | 1 | if ($field->belongsToArray()) { |
|
| 43 | 1 | $inputs += [$field->name() => $request->input($field->getDotPatternName())]; |
|
| 44 | } |
||
| 45 | } |
||
| 46 | 1 | $request->replace( |
|
| 47 | 1 | $request->all() + $inputs |
|
| 48 | ); |
||
| 49 | |||
| 50 | 1 | $data = []; |
|
| 51 | 1 | $additional = []; |
|
| 52 | /** @var AbstractField $field */ |
||
| 53 | 1 | foreach ($fields as $field) { |
|
| 54 | 1 | if ($field->hidden('edit') || $field->isReadonly() || $field->shouldSkip($request)) { |
|
| 55 | continue; |
||
| 56 | } |
||
| 57 | |||
| 58 | 1 | $field->beforeUpdate($model); |
|
| 59 | |||
| 60 | 1 | if ($field->belongsToArray()) { |
|
| 61 | 1 | $additional[$field->getAncestorName()][$field->getDescendantName()] = $field->value($request); |
|
| 62 | 1 | continue; |
|
| 63 | } |
||
| 64 | |||
| 65 | 1 | $data += [$field->name() => $field->value($request)]; |
|
| 66 | } |
||
| 67 | |||
| 68 | 1 | $data = $data + $additional; |
|
| 69 | |||
| 70 | 1 | $model = $this->crud()->repo()->update($id, $data); |
|
| 71 | /** @var AbstractField $field */ |
||
| 72 | 1 | foreach ($fields as $field) { |
|
| 73 | 1 | $field->afterUpdate($model, $request); |
|
| 74 | } |
||
| 75 | 1 | $this->idEntity = $model->getKey(); |
|
| 76 | |||
| 77 | 1 | return redirect($this->crud()->listUrl()); |
|
| 78 | } |
||
| 79 | |||
| 80 | abstract protected function beforeInit(); |
||
| 85 | } |
||
| 86 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.