Completed
Pull Request — dev (#319)
by Tristan
07:50
created

SkillsController::update()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 74
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 74
ccs 0
cts 41
cp 0
rs 6.9666
c 0
b 0
f 0
cc 12
nc 12
nop 2
crap 156

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace App\Http\Controllers;
4
5
use Illuminate\Support\Facades\Lang;
6
use Illuminate\Http\Request;
7
use Barryvdh\Debugbar\Facade as Debugbar;
8
use App\Models\Lookup\SkillLevel;
9
use App\Models\Lookup\SkillStatus;
10
use App\Models\SkillDeclaration;
11
use App\Models\Skill;
12
use App\Models\Applicant;
13
use App\Http\Controllers\Controller;
14
15
class SkillsController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class SkillsController
Loading history...
16
{
17
18
    /**
19
     * Display the Skills page associated with the applicant.
20
     *
21
     * @param  \App\Models\Applicant  $applicant
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
22
     * @return \Illuminate\Http\Response
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
23
     */
24
    public function show(Applicant $applicant)
0 ignored issues
show
Unused Code introduced by
The parameter $applicant is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

24
    public function show(/** @scrutinizer ignore-unused */ Applicant $applicant)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
25
    {
26
        //
27
28
    }
29
30
    /**
31
     * Show the form for editing the applicant's skills
32
     *
33
     * @param  Request  $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 15 spaces after parameter type; 2 found
Loading history...
34
     * @param  \App\Models\Applicant  $applicant
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
35
     * @return \Illuminate\Http\Response
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
36
     */
37
    public function edit(Request $request, Applicant $applicant)
38
    {
39
        return view('applicant/profile_03_skills', [
1 ignored issue
show
Bug Best Practice introduced by
The expression return view('applicant/p....update', $applicant))) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
40
            'applicant' => $applicant,
41
            'profile' => Lang::get('applicant/profile_skills'),
42
            'skill_template' => Lang::get('common/skills'),
43
            'relative_template' => Lang::get('common/relatives'),
44
            'skills' => Skill::all(),
45
            'skill_levels' => SkillLevel::all(),
46
            'form_submit_action' => route('profile.skills.update', $applicant),
0 ignored issues
show
Bug introduced by
$applicant of type App\Models\Applicant is incompatible with the type array expected by parameter $parameters of route(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
            'form_submit_action' => route('profile.skills.update', /** @scrutinizer ignore-type */ $applicant),
Loading history...
47
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
48
    }
49
50
    /**
51
     * Update the applicant's profile in storage.
52
     *
53
     * @param  \Illuminate\Http\Request  $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
54
     * @param  \App\Models\Applicant  $applicant
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 2 found
Loading history...
55
     * @return \Illuminate\Http\Response
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
56
     */
57
    public function update(Request $request, Applicant $applicant)
58
    {
59
60
        $input = $request->input();
61
62
        Debugbar::info($input);
63
64
        $skillDeclarations = $input['skill_declarations'];
65
        $claimedStatusId = SkillStatus::where('name', 'claimed')->firstOrFail()->id;
66
67
        //Delete old skill declarations that weren't resubmitted
68
        //Note: this must be done before adding new ones, so we don't delete
69
        // them right after adding them
70
        foreach($applicant->skill_declarations as $oldDeclaration) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
71
            //Check if none were resubmitted, or if this specific one wasn't
72
            $type = $oldDeclaration->skill->skill_type->name;
73
            if (!isset($skillDeclarations['old']) ||
74
                !isset($skillDeclarations['old'][$type]) ||
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
75
                !isset($skillDeclarations['old'][$type][$oldDeclaration->id])) {
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
76
                $oldDeclaration->delete();
77
            }
78
        }
79
80
        //Save new skill declarartions
81
        if (isset($skillDeclarations['new'])) {
82
            foreach($skillDeclarations['new'] as $skillType => $typeInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
83
                foreach($typeInput as $skillDeclarationInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
84
                    $skillDeclaration = new SkillDeclaration();
85
                    $skillDeclaration->applicant_id = $applicant->id;
86
                    $skillDeclaration->skill_id = $skillDeclarationInput['skill_id'];
87
                    $skillDeclaration->skill_status_id = $claimedStatusId;
88
                    $skillDeclaration->fill([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
89
                        'description' => $skillDeclarationInput['description'],
90
                        'skill_level_id' => $skillDeclarationInput['skill_level_id'],
91
                    ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
92
                    $skillDeclaration->save();
93
94
                    $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references');
95
                    $skillDeclaration->references()->sync($referenceIds);
96
97
                    $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples');
98
                    $skillDeclaration->work_samples()->sync($sampleIds);
99
                }
100
            }
101
        }
102
103
        //Update old declarations
104
        if (isset($skillDeclarations['old'])) {
105
            foreach($skillDeclarations['old'] as $skillType => $typeInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
106
                foreach($typeInput as $id=>$skillDeclarationInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
107
                    //Ensure this declaration belongs to this applicant
108
                    $skillDeclaration = $applicant->skill_declarations->firstWhere('id', $id);
109
                    if ($skillDeclaration != null) {
110
                        //skill_id and skill_status cannot be changed
111
                        $skillDeclaration->fill([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
112
                            'description' => $skillDeclarationInput['description'],
113
                            'skill_level_id' => $skillDeclarationInput['skill_level_id'],
114
                        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
115
                        $skillDeclaration->save();
116
117
                        $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references');
118
                        $skillDeclaration->references()->sync($referenceIds);
119
120
                        $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples');
121
                        $skillDeclaration->work_samples()->sync($sampleIds);
122
                    } else {
123
                        Debugbar::warning('Applicant '.$applicant->id.' attempted to update skill declaration with invalid id '.$id);
124
                    }
125
                }
126
            }
127
        }
128
129
130
        return redirect( route('profile.skills.edit', $applicant) );
1 ignored issue
show
Bug Best Practice introduced by
The expression return redirect(route('p...lls.edit', $applicant)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
Bug introduced by
$applicant of type App\Models\Applicant is incompatible with the type array expected by parameter $parameters of route(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

130
        return redirect( route('profile.skills.edit', /** @scrutinizer ignore-type */ $applicant) );
Loading history...
131
        // return view('applicant/profile_03_skills', [
132
        //     'applicant' => $applicant->fresh(),
133
        //     'profile' => Lang::get('applicant/profile_skills'),
134
        //     'skill_template' => Lang::get('common/skills'),
135
        //     'relative_template' => Lang::get('common/relatives'),
136
        //     'skills' => Skill::all(),
137
        //     'skill_levels' => SkillLevel::all(),
138
        //     'form_submit_action' => route('profile.skills.update', $applicant),
139
        // ]);
140
    }
141
142
}
143