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 DeviceController extends Controller |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Create a new controller instance. |
||
| 18 | * |
||
| 19 | */ |
||
| 20 | public function __construct() |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Display index page and process dataTable ajax request. |
||
| 29 | * |
||
| 30 | * @param \App\DataTables\DevicesDataTable $dataTable |
||
| 31 | * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
||
| 32 | */ |
||
| 33 | public function index(DevicesDataTable $dataTable) |
||
| 34 | { |
||
| 35 | $trashed = Device::onlyTrashed()->get(); |
||
| 36 | return $dataTable->render('device.index', compact('trashed')); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Show create device page. |
||
| 41 | * |
||
| 42 | * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 43 | */ |
||
| 44 | public function create() |
||
| 45 | { |
||
| 46 | return view('device.create'); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Show the given device. |
||
| 51 | * |
||
| 52 | * @param string $id |
||
| 53 | * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 54 | */ |
||
| 55 | public function show($id) |
||
| 56 | { |
||
| 57 | $device = Device::findOrFail($id); |
||
| 58 | return view('device.show', [ 'device' => $device ]); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * View the edit device page or the edit device modal |
||
| 63 | * |
||
| 64 | * @param string $id |
||
| 65 | * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 66 | */ |
||
| 67 | public function edit($id) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Get the locations with the given site id |
||
| 104 | * Return null if the site does not have any locations |
||
| 105 | * |
||
| 106 | * @param int $site_id |
||
| 107 | * @return Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection |
||
| 108 | */ |
||
| 109 | public function locations($site_id) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Get the devices details |
||
| 121 | * Return 404 error if the device is not found |
||
| 122 | * |
||
| 123 | * @param int $id |
||
| 124 | * @return Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection |
||
| 125 | */ |
||
| 126 | public function details($id) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Update the given device. |
||
| 135 | * |
||
| 136 | * @param EditDevice $request |
||
| 137 | * @param string $id |
||
| 138 | * @return \Illuminate\Http\RedirectResponse |
||
| 139 | */ |
||
| 140 | public function update(EditDevice $request, $id) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Deletes a device. |
||
| 199 | * |
||
| 200 | * @param string $id |
||
| 201 | * @return \Illuminate\Http\RedirectResponse |
||
| 202 | */ |
||
| 203 | View Code Duplication | public function destroy($id) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Restores a device. |
||
| 221 | * |
||
| 222 | * @param string $id |
||
| 223 | * @return \Illuminate\Http\RedirectResponse |
||
| 224 | */ |
||
| 225 | View Code Duplication | public function restore($id) |
|
| 234 | } |
||
| 235 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.