Conditions | 14 |
Paths | 12 |
Total Lines | 74 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 210 |
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 | Debugbar::info($input); |
||
60 | |||
61 | $skillDeclarations = $input['skill_declarations']; |
||
62 | $claimedStatusId = SkillStatus::where('name', 'claimed')->firstOrFail()->id; |
||
63 | |||
64 | //Delete old skill declarations that weren't resubmitted |
||
65 | //Note: this must be done before adding new ones, so we don't delete |
||
66 | // them right after adding them |
||
67 | foreach($applicant->skill_declarations as $oldDeclaration) { |
||
68 | //Check if none were resubmitted, or if this specific one wasn't |
||
69 | $type = $oldDeclaration->skill->skill_type->name; |
||
70 | if (!isset($skillDeclarations['old']) || |
||
71 | !isset($skillDeclarations['old'][$type]) || |
||
72 | !isset($skillDeclarations['old'][$type][$oldDeclaration->id])) { |
||
73 | $oldDeclaration->delete(); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | //Save new skill declarartions |
||
78 | if (isset($skillDeclarations['new'])) { |
||
79 | foreach($skillDeclarations['new'] as $skillType => $typeInput) { |
||
80 | foreach($typeInput as $skillDeclarationInput) { |
||
81 | $skillDeclaration = new SkillDeclaration(); |
||
82 | $skillDeclaration->applicant_id = $applicant->id; |
||
83 | $skillDeclaration->skill_id = $skillDeclarationInput['skill_id']; |
||
84 | $skillDeclaration->skill_status_id = $claimedStatusId; |
||
85 | $skillDeclaration->fill([ |
||
86 | 'description' => $skillDeclarationInput['description'], |
||
87 | 'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null, |
||
88 | ]); |
||
89 | $skillDeclaration->save(); |
||
90 | |||
91 | $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references'); |
||
92 | $skillDeclaration->references()->sync($referenceIds); |
||
93 | |||
94 | $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples'); |
||
95 | $skillDeclaration->work_samples()->sync($sampleIds); |
||
96 | } |
||
97 | } |
||
98 | } |
||
99 | |||
100 | //Update old declarations |
||
101 | if (isset($skillDeclarations['old'])) { |
||
102 | foreach($skillDeclarations['old'] as $skillType => $typeInput) { |
||
103 | foreach($typeInput as $id=>$skillDeclarationInput) { |
||
104 | //Ensure this declaration belongs to this applicant |
||
105 | $skillDeclaration = $applicant->skill_declarations->firstWhere('id', $id); |
||
106 | if ($skillDeclaration != null) { |
||
107 | //skill_id and skill_status cannot be changed |
||
108 | $skillDeclaration->fill([ |
||
109 | 'description' => $skillDeclarationInput['description'], |
||
110 | 'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null, |
||
111 | ]); |
||
112 | $skillDeclaration->save(); |
||
113 | |||
114 | $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references'); |
||
115 | $skillDeclaration->references()->sync($referenceIds); |
||
116 | |||
117 | $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples'); |
||
118 | $skillDeclaration->work_samples()->sync($sampleIds); |
||
119 | } else { |
||
120 | Debugbar::warning('Applicant '.$applicant->id.' attempted to update skill declaration with invalid id '.$id); |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 | } |
||
125 | |||
126 | |||
127 | return redirect( route('profile.skills.edit', $applicant) ); |
||
1 ignored issue
–
show
|
|||
128 | } |
||
150 |