| Conditions | 6 |
| Paths | 4 |
| Total Lines | 62 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 148 | public function update(Request $request, $id) |
||
| 149 | { |
||
| 150 | $request->validate([ |
||
| 151 | 'name' => [ |
||
| 152 | 'required', |
||
| 153 | 'string', |
||
| 154 | Rule::unique('system_types')->ignore($id, 'sys_id'), |
||
| 155 | 'regex:/^[a-zA-Z0-9_ ]*$/' |
||
| 156 | ] |
||
| 157 | ]); |
||
| 158 | |||
| 159 | // Update the system name |
||
| 160 | $sys = SystemTypes::find($id)->update([ |
||
|
|
|||
| 161 | 'name' => $request->name |
||
| 162 | ]); |
||
| 163 | |||
| 164 | // Update the order of the existing data fields |
||
| 165 | $i = 0; |
||
| 166 | foreach($request->dataOptions as $data) |
||
| 167 | { |
||
| 168 | $dataID = SystemCustDataTypes::where('name', $data)->first(); |
||
| 169 | |||
| 170 | $dataType = SystemCustDataFields::where('sys_id', $id)->where('data_type_id', $dataID->data_type_id)->update([ |
||
| 171 | 'order' => $i |
||
| 172 | ]); |
||
| 173 | |||
| 174 | $i++; |
||
| 175 | } |
||
| 176 | |||
| 177 | // Process any new data fields |
||
| 178 | if(!empty($request->newDataOptions)) |
||
| 179 | { |
||
| 180 | foreach($request->newDataOptions as $field) |
||
| 181 | { |
||
| 182 | if(!empty($field)) |
||
| 183 | { |
||
| 184 | if(isset($field['value'])) |
||
| 185 | { |
||
| 186 | $dataID = $field['value']; |
||
| 187 | } |
||
| 188 | else |
||
| 189 | { |
||
| 190 | $newField = SystemCustDataTypes::create([ |
||
| 191 | 'name' => $field['label'] |
||
| 192 | ]); |
||
| 193 | $dataID = $newField->data_type_id; |
||
| 194 | } |
||
| 195 | |||
| 196 | SystemCustDataFields::create([ |
||
| 197 | 'sys_id' => $id, |
||
| 198 | 'data_type_id' => $dataID, |
||
| 199 | 'order' => $i |
||
| 200 | ]); |
||
| 201 | $i++; |
||
| 202 | } |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | Log::info('System Updated', ['sys_name' => $request->name, 'user_id' => Auth::user()->user_id]); |
||
| 207 | $request->session()->flash('success', 'System Updated'); |
||
| 208 | |||
| 209 | return response()->json(['success' => true]); |
||
| 210 | } |
||
| 223 |