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 AddonController extends Controller |
||
| 15 | { |
||
| 16 | public function __construct() |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Display a listing of the resource. |
||
| 30 | * |
||
| 31 | * @return \Response |
||
| 32 | */ |
||
| 33 | public function index() |
||
| 41 | |||
| 42 | // public function getAddons() |
||
| 43 | // { |
||
| 44 | // return \Datatable::collection($this->addon->get()) |
||
| 45 | // ->addColumn('#', function ($model) { |
||
| 46 | // return "<input type='checkbox' value=".$model->id.' name=select[] id=check>'; |
||
| 47 | // }) |
||
| 48 | // ->showColumns('name', 'regular_price', 'selling_price') |
||
| 49 | // ->addColumn('associated', function ($model) { |
||
| 50 | // $relations = new ProductAddonRelation(); |
||
| 51 | // $relations = $relations->where('addon_id', $model->id)->get(); |
||
| 52 | // $products = []; |
||
| 53 | // foreach ($relations as $key => $relation) { |
||
| 54 | // $products[$key] = $this->product->where('id', $relation->product_id)->first()->name; |
||
| 55 | // } |
||
| 56 | |||
| 57 | // return implode(',', $products); |
||
| 58 | // }) |
||
| 59 | // ->addColumn('action', function ($model) { |
||
| 60 | // return '<a href='.url('addons/'.$model->id.'/edit')." class='btn btn-sm btn-primary'>Edit</a>"; |
||
| 61 | // }) |
||
| 62 | // ->searchColumns('name') |
||
| 63 | // ->orderColumns('name') |
||
| 64 | // ->make(); |
||
| 65 | // } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Show the form for creating a new resource. |
||
| 69 | * |
||
| 70 | * @return \Response |
||
| 71 | */ |
||
| 72 | View Code Duplication | public function create() |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Store a newly created resource in storage. |
||
| 86 | * |
||
| 87 | * @return \Response |
||
| 88 | */ |
||
| 89 | public function store(AddonRequest $request) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Display the specified resource. |
||
| 111 | * |
||
| 112 | * @param int $id |
||
| 113 | * |
||
| 114 | * @return \Response |
||
| 115 | */ |
||
| 116 | public function show($id) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Show the form for editing the specified resource. |
||
| 123 | * |
||
| 124 | * @param int $id |
||
| 125 | * |
||
| 126 | * @return \Response |
||
| 127 | */ |
||
| 128 | public function edit($id) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Update the specified resource in storage. |
||
| 145 | * |
||
| 146 | * @param int $id |
||
| 147 | * |
||
| 148 | * @return \Response |
||
| 149 | */ |
||
| 150 | public function update($id, AddonRequest $request) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Remove the specified resource from storage. |
||
| 180 | * |
||
| 181 | * @param int $id |
||
| 182 | * |
||
| 183 | * @return \Response |
||
| 184 | */ |
||
| 185 | View Code Duplication | public function destroy(Request $request) |
|
| 232 | } |
||
| 233 |
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: