| Conditions | 11 |
| Paths | 12 |
| Total Lines | 62 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 132 |
| 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 |
||
| 54 | public function update(Request $request, Applicant $applicant) |
||
| 55 | { |
||
| 56 | |||
| 57 | $input = $request->input(); |
||
| 58 | |||
| 59 | $workSamples = $input['work_samples']; |
||
| 60 | |||
| 61 | //Delete old workSamples that weren't resubmitted |
||
| 62 | //Note: this must be done before adding new workSamples, so we don't delete |
||
| 63 | // them right after adding them |
||
| 64 | foreach($applicant->work_samples as $oldWorkSample) { |
||
| 65 | //Check if no workSamples were resubmitted, or if this specific one wasn't |
||
| 66 | if (!isset($workSamples['old']) || |
||
| 67 | !isset($workSamples['old'][$oldWorkSample->id])) { |
||
| 68 | $oldWorkSample->delete(); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | //Save new workSamples |
||
| 73 | if (isset($workSamples['new'])) { |
||
| 74 | foreach($workSamples['new'] as $workSampleInput) { |
||
| 75 | $workSample = new WorkSample(); |
||
| 76 | $workSample->applicant_id = $applicant->id; |
||
| 77 | $workSample->fill([ |
||
| 78 | 'name' => $workSampleInput['name'], |
||
| 79 | 'date_created' => isset($workSampleInput['date_created']) ? $workSampleInput['date_created'] : null, |
||
| 80 | 'file_type_id' => $workSampleInput['file_type_id'], |
||
| 81 | 'url' => $workSampleInput['url'], |
||
| 82 | 'description' => $workSampleInput['description'], |
||
| 83 | ]); |
||
| 84 | |||
| 85 | $workSample->save(); |
||
| 86 | |||
| 87 | $skillDeclarationIds =$this->getRelativeIds($workSampleInput, 'skills'); |
||
| 88 | $workSample->skill_declarations()->sync($skillDeclarationIds); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | //Update old workSamples |
||
| 93 | if (isset($workSamples['old'])) { |
||
| 94 | foreach($workSamples['old'] as $id=>$workSampleInput) { |
||
| 95 | //Ensure this workSample belongs to this applicant |
||
| 96 | $workSample = $applicant->work_samples->firstWhere('id', $id); |
||
| 97 | if ($workSample != null) { |
||
| 98 | $workSample->fill([ |
||
| 99 | 'name' => $workSampleInput['name'], |
||
| 100 | 'date_created' => isset($workSampleInput['date_created']) ? $workSampleInput['date_created'] : null, |
||
| 101 | 'file_type_id' => $workSampleInput['file_type_id'], |
||
| 102 | 'url' => $workSampleInput['url'], |
||
| 103 | 'description' => $workSampleInput['description'], |
||
| 104 | ]); |
||
| 105 | $workSample->save(); |
||
| 106 | |||
| 107 | $skillDeclarationIds =$this->getRelativeIds($workSampleInput, 'skills'); |
||
| 108 | $workSample->skill_declarations()->sync($skillDeclarationIds); |
||
| 109 | } else { |
||
| 110 | Debugbar::warning('Applicant '.$applicant->id.' attempted to update workSample with invalid id '.$id); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | return redirect( route('profile.work_samples.edit', $applicant) ); |
||
|
1 ignored issue
–
show
|
|||
| 116 | // Debugbar::info($input); |
||
| 128 |