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() |
||
21 | { |
||
22 | $this->middleware('auth'); |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * Display index page and process dataTable ajax request. |
||
27 | * |
||
28 | * @param \App\DataTables\DevicesDataTable $dataTable |
||
29 | * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
||
30 | */ |
||
31 | public function index(DevicesDataTable $dataTable) |
||
32 | { |
||
33 | $trashed = Device::onlyTrashed()->get(); |
||
34 | return $dataTable->render('device.index', compact('trashed')); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Show create device page. |
||
39 | * |
||
40 | * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
41 | */ |
||
42 | public function create() |
||
43 | { |
||
44 | return view('device.index'); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Show the given device. |
||
49 | * |
||
50 | * @param string $id |
||
51 | * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
52 | */ |
||
53 | public function show($id) |
||
54 | { |
||
55 | $device = Device::findOrFail($id); |
||
56 | return view('device.show', [ 'device' => $device ]); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * View the edit device page |
||
61 | * |
||
62 | * @param string $id |
||
63 | * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
64 | */ |
||
65 | public function edit($id) |
||
66 | { |
||
67 | //Get the device with the given id |
||
68 | $device = Device::publicDashData()->findOrFail($id); |
||
69 | //Get the devices location |
||
70 | $location = $device->location()->select('id', 'name', 'site_id')->first(); |
||
71 | |||
72 | //Get the site id and location id if they exist and if not assign 0 |
||
73 | $site_id = $location->site_id ?? 0; |
||
74 | $location_id = $location->id ?? 0; |
||
75 | |||
76 | //Get all sites with the current site first |
||
77 | $sites = Site::select('id', 'name')->orderByRaw("id = ? DESC", $site_id) |
||
78 | ->orderBy('name', 'ASC')->get(); |
||
79 | //Get all locations for the selected site with the selected location first |
||
80 | $locations = Location::select('id', 'name')->where('site_id', '=', $sites[0]->id ?? 0) |
||
81 | ->orderByRaw("id = ? DESC", $location_id)->orderBy('name', 'ASC')->get(); |
||
82 | |||
83 | return view('device.edit', [ 'device' => $device, 'locations' => $locations, 'sites' => $sites ]); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Update the given device. |
||
88 | * |
||
89 | * @param EditDevice $request |
||
90 | * @param string $id |
||
91 | * @return \Illuminate\Http\RedirectResponse |
||
92 | */ |
||
93 | public function update(EditDevice $request, $id) |
||
94 | { |
||
95 | $device = Device::findOrFail($id); |
||
96 | |||
97 | //Get the site id of the old or newly created site |
||
98 | if (!empty($request->input('new_site_name'))) |
||
99 | { |
||
100 | //Create a new site |
||
101 | $site = Site::create(['name' => $request->input('new_site_name')]); |
||
102 | $site_id = $site->id; |
||
103 | } |
||
104 | else |
||
105 | $site_id = $request->input('site_id'); |
||
106 | |||
107 | //Get the location id of the old or newly created location |
||
108 | if (!empty($request->input('new_location_name'))) |
||
109 | { |
||
110 | //Create a new location |
||
111 | $location = Location::create(['name' => $request->input('new_location_name'), 'site_id' => $site_id]); |
||
112 | $location_id = $location->id; |
||
113 | } |
||
114 | else |
||
115 | $location_id = $request->input('location_id'); |
||
116 | |||
117 | //Update the device |
||
118 | $device->location_id = $location_id; |
||
119 | $device->name = $request->input('name'); |
||
120 | $device->open_time = $request->input('open_time'); |
||
121 | $device->close_time = $request->input('close_time'); |
||
122 | $device->update_rate = $request->input('update_rate'); |
||
123 | $device->image_rate = $request->input('image_rate'); |
||
124 | $device->sensor_rate = $request->input('sensor_rate'); |
||
125 | //Check if the cover_command needs to be updated |
||
126 | if ($request->input('command') != null) |
||
127 | { |
||
128 | //If device is currently opening, closing or in an error state don't update command |
||
129 | if (!$device->isReadyForCommand()) |
||
130 | return response()->json("Device is currently in use.", 403); |
||
|
|||
131 | |||
132 | $command = $request->input('command'); |
||
133 | |||
134 | //If command is to unlock the device then check if the device should be open or closed based on the schedule |
||
135 | if ($request->command === 'unlock') |
||
136 | { |
||
137 | if ($device->isDuringScheduleOpen()) |
||
138 | $command = 'open'; |
||
139 | else |
||
140 | $command = 'close'; |
||
141 | } |
||
142 | $device->cover_command = $command; |
||
143 | } |
||
144 | |||
145 | $device->save(); |
||
146 | |||
147 | if (\Request::ajax()) |
||
148 | return response()->json(['success' => 'Device updated successfully']); |
||
149 | else |
||
150 | return redirect()->route('device.show', $id) |
||
1 ignored issue
–
show
|
|||
151 | ->with('success', 'Device updated successfully'); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Deletes a device. |
||
156 | * |
||
157 | * @param string $id |
||
158 | * @return \Illuminate\Http\RedirectResponse |
||
159 | */ |
||
160 | View Code Duplication | public function destroy($id) |
|
161 | { |
||
162 | $device = Device::withTrashed()->findOrFail($id); |
||
163 | |||
164 | if ($device->trashed()) |
||
165 | { |
||
166 | //If the device was already deleted then permanently delete it |
||
167 | $device->forceDelete($device->id); |
||
168 | } |
||
169 | else |
||
170 | { |
||
171 | //Remove the location from the device |
||
172 | $device->location_id = null; |
||
173 | $device->save(); |
||
174 | |||
175 | //Soft delete the device the first time |
||
176 | $device->delete(); |
||
177 | } |
||
178 | |||
179 | return redirect()->route('device.index') |
||
180 | ->with('success','Device deleted successfully'); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Restores a device. |
||
185 | * |
||
186 | * @param string $id |
||
187 | * @return \Illuminate\Http\RedirectResponse |
||
188 | */ |
||
189 | View Code Duplication | public function restore($id) |
|
198 | } |
||
199 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.