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 namespace Distilleries\Expendable\States; |
||
| 6 | trait ExportStateTrait { |
||
| 7 | |||
| 8 | protected $export_form = 'Distilleries\Expendable\Forms\Export\ExportForm'; |
||
| 9 | // ------------------------------------------------------------------------------------------------ |
||
| 10 | // ------------------------------------------------------------------------------------------------ |
||
| 11 | // ------------------------------------------------------------------------------------------------ |
||
| 12 | |||
| 13 | 4 | View Code Duplication | public function getExport() |
| 14 | { |
||
| 15 | 4 | $form = FormBuilder::create($this->export_form, [ |
|
| 16 | 4 | 'model' => $this->model |
|
|
|
|||
| 17 | 3 | ]); |
|
| 18 | |||
| 19 | 4 | $form_content = view('expendable::admin.form.components.formgenerator.export', [ |
|
| 20 | 1 | 'form' => $form |
|
| 21 | 3 | ]); |
|
| 22 | 4 | $content = view('expendable::admin.form.state.form', [ |
|
| 23 | |||
| 24 | 4 | ]); |
|
| 25 | |||
| 26 | 4 | $this->layoutManager->add([ |
|
| 27 | 4 | 'form'=>$form_content, |
|
| 28 | 4 | 'content'=>$content, |
|
| 29 | 3 | ]); |
|
| 30 | |||
| 31 | 4 | return $this->layoutManager->render(); |
|
| 32 | } |
||
| 33 | |||
| 34 | // ------------------------------------------------------------------------------------------------ |
||
| 35 | |||
| 36 | 12 | public function postExport(Request $request) |
|
| 70 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: