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 DeviceController extends Controller |
||
16 | { |
||
17 | /** |
||
18 | * Create a new controller instance. |
||
19 | * |
||
20 | */ |
||
21 | public function __construct() |
||
25 | |||
26 | /** |
||
27 | * Display index page and process dataTable ajax request. |
||
28 | * |
||
29 | * @param \App\DataTables\DevicesDataTable $dataTable |
||
30 | * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
||
31 | */ |
||
32 | public function index(DevicesDataTable $dataTable) |
||
37 | |||
38 | /** |
||
39 | * Show create device page. |
||
40 | * |
||
41 | * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
42 | */ |
||
43 | public function create() |
||
47 | |||
48 | /** |
||
49 | * Show the given device. |
||
50 | * |
||
51 | * @param string $id |
||
52 | * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
53 | */ |
||
54 | public function show($id) |
||
55 | { |
||
56 | $device = Device::findOrFail($id); |
||
57 | |||
58 | $charts = []; |
||
59 | foreach ($device->sensors as $sensor) { |
||
60 | $data = $sensor->last_month_daily_avg_data; |
||
61 | $charts[$sensor->id] = Charts::create('line', 'highcharts') |
||
62 | ->title($sensor->name) |
||
63 | ->elementLabel($sensor->type) |
||
64 | ->labels($data->pluck('date')) |
||
65 | ->values($data->pluck('value')) |
||
66 | ->responsive(true); |
||
67 | } |
||
68 | |||
69 | return view('device.show', [ 'device' => $device, 'charts' => $charts ]); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * View the edit device page |
||
74 | * |
||
75 | * @param string $id |
||
76 | * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
77 | */ |
||
78 | public function edit($id) |
||
98 | |||
99 | /** |
||
100 | * Update the given device. |
||
101 | * |
||
102 | * @param EditDevice $request |
||
103 | * @param string $id |
||
104 | * @return \Illuminate\Http\RedirectResponse |
||
105 | */ |
||
106 | public function update(EditDevice $request, $id) |
||
177 | |||
178 | /** |
||
179 | * Deletes a device. |
||
180 | * |
||
181 | * @param string $id |
||
182 | * @return \Illuminate\Http\RedirectResponse |
||
183 | */ |
||
184 | View Code Duplication | public function destroy($id) |
|
206 | |||
207 | /** |
||
208 | * Restores a device. |
||
209 | * |
||
210 | * @param string $id |
||
211 | * @return \Illuminate\Http\RedirectResponse |
||
212 | */ |
||
213 | View Code Duplication | public function restore($id) |
|
222 | } |
||
223 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.