Conditions | 8 |
Paths | 48 |
Total Lines | 73 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 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 |
||
163 | public function update(UpdateTipRequest $request, $id) |
||
164 | { |
||
165 | // Update the Tech Tip |
||
166 | $tip = TechTip::findOrFail($id); |
||
167 | $tip->update([ |
||
168 | 'updated_id' => $request->user()->user_id, |
||
169 | 'tip_type_id' => $request->tip_type_id, |
||
170 | 'sticky' => $request->sticky, |
||
171 | 'subject' => $request->subject, |
||
172 | 'slug' => Str::slug($request->subject), |
||
173 | 'details' => $request->details, |
||
174 | ]); |
||
175 | |||
176 | // Update the equipment attached to the Tech Tip |
||
177 | $currentEquip = TechTipEquipment::where('tip_id', $id)->get(); |
||
178 | $newEquip = []; |
||
179 | |||
180 | foreach($request->equipment as $equip) |
||
181 | { |
||
182 | // If the laravel_through_key value exists, then it was an existing equipment that has stayed in place |
||
183 | if(isset($equip['laravel_through_key'])) |
||
184 | { |
||
185 | // Remove that piece from the current equipment list so it is not updated later |
||
186 | $currentEquip = $currentEquip->filter(function($i) use ($equip) |
||
187 | { |
||
188 | return $i->equip_id != $equip['equip_id']; |
||
189 | }); |
||
190 | } |
||
191 | else |
||
192 | { |
||
193 | $newEquip[] = $equip; |
||
194 | } |
||
195 | } |
||
196 | |||
197 | // Remove the Equipment left over in the CurrentEquipment array |
||
198 | foreach($currentEquip as $cur) |
||
199 | { |
||
200 | TechTipEquipment::find($cur->tip_equip_id)->delete(); |
||
201 | } |
||
202 | |||
203 | // Add the new equipment |
||
204 | foreach($newEquip as $new) |
||
205 | { |
||
206 | TechTipEquipment::create([ |
||
207 | 'tip_id' => $id, |
||
208 | 'equip_id' => $new['equip_id'], |
||
209 | ]); |
||
210 | } |
||
211 | |||
212 | // Remove any files that are no longer needed |
||
213 | foreach($request->removedFiles as $file) |
||
214 | { |
||
215 | TechTipFile::where('tip_id', $tip->tip_id)->where('file_id', $file)->first()->delete(); |
||
216 | $this->deleteFile($file); |
||
217 | } |
||
218 | |||
219 | // Add any additional files |
||
220 | $fileData = $request->session()->pull('new-file-upload'); |
||
221 | if($fileData) |
||
222 | { |
||
223 | foreach($fileData as $file) |
||
224 | { |
||
225 | TechTipFile::create([ |
||
226 | 'tip_id' => $tip->tip_id, |
||
227 | 'file_id' => $file->file_id, |
||
228 | ]); |
||
229 | } |
||
230 | } |
||
231 | |||
232 | event(new TechTipUpdatedEvent($tip)); |
||
233 | return redirect(route('tech-tips.show', $tip->slug))->with([ |
||
234 | 'message' => 'Tech Tip Updated', |
||
235 | 'type' => 'success', |
||
236 | ]); |
||
255 |