| Conditions | 17 |
| Paths | 5760 |
| Total Lines | 71 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 306 |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 106 | public function update(EditDevice $request, $id) |
||
| 107 | { |
||
| 108 | $device = Device::findOrFail($id); |
||
| 109 | $site_id = null; |
||
|
|
|||
| 110 | $location_id = null; |
||
| 111 | |||
| 112 | //Get the site id and location id for the device if they are not null |
||
| 113 | if ($request->input('new_site_name') != null || $request->input('site_id') != null) |
||
| 114 | { |
||
| 115 | //Get the site id of the old or newly created site |
||
| 116 | if (!empty($request->input('new_site_name'))) |
||
| 117 | { |
||
| 118 | //Create a new site |
||
| 119 | $site = Site::create(['name' => $request->input('new_site_name')]); |
||
| 120 | $site_id = $site->id; |
||
| 121 | } else |
||
| 122 | $site_id = $request->input('site_id'); |
||
| 123 | |||
| 124 | //Get the location id of the old or newly created location |
||
| 125 | if (!empty($request->input('new_location_name'))) |
||
| 126 | { |
||
| 127 | //Create a new location |
||
| 128 | $location = Location::create(['name' => $request->input('new_location_name'), 'site_id' => $site_id]); |
||
| 129 | $location_id = $location->id; |
||
| 130 | } else |
||
| 131 | $location_id = $request->input('location_id'); |
||
| 132 | } |
||
| 133 | |||
| 134 | //Update the device |
||
| 135 | if ($location_id != null) |
||
| 136 | $device->location_id = $location_id; |
||
| 137 | if ($request->input('name') != null) |
||
| 138 | $device->name = $request->input('name'); |
||
| 139 | if ($request->input('open_time') != null) |
||
| 140 | $device->open_time = $request->input('open_time'); |
||
| 141 | if ($request->input('close_time') != null) |
||
| 142 | $device->close_time = $request->input('close_time'); |
||
| 143 | if ($request->input('update_rate') != null) |
||
| 144 | $device->update_rate = $request->input('update_rate'); |
||
| 145 | if ($request->input('image_rate') != null) |
||
| 146 | $device->image_rate = $request->input('image_rate'); |
||
| 147 | if ($request->input('sensor_rate') != null) |
||
| 148 | $device->sensor_rate = $request->input('sensor_rate'); |
||
| 149 | //Check if the cover_command needs to be updated |
||
| 150 | if ($request->input('command') != null) |
||
| 151 | { |
||
| 152 | //If device is currently opening, closing or in an error state don't update command |
||
| 153 | if (!$device->isReadyForCommand()) |
||
| 154 | return response()->json("Device is currently in use.", 403); |
||
| 155 | |||
| 156 | $command = $request->input('command'); |
||
| 157 | |||
| 158 | //If command is to unlock the device then check if the device should be open or closed based on the schedule |
||
| 159 | if ($request->command === 'unlock') |
||
| 160 | { |
||
| 161 | if ($device->isDuringScheduleOpen()) |
||
| 162 | $command = 'open'; |
||
| 163 | else |
||
| 164 | $command = 'close'; |
||
| 165 | } |
||
| 166 | $device->cover_command = $command; |
||
| 167 | } |
||
| 168 | |||
| 169 | $device->save(); |
||
| 170 | |||
| 171 | if (\Request::ajax()) |
||
| 172 | return response()->json(['success' => 'Device updated successfully']); |
||
| 173 | else |
||
| 174 | return redirect()->route('device.show', $id) |
||
|
1 ignored issue
–
show
|
|||
| 175 | ->with('success', 'Device updated successfully'); |
||
| 176 | } |
||
| 177 | |||
| 223 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.