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