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

ReferencesController::update()   C

Complexity

Conditions 16
Paths 12

Size

Total Lines 113
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 272

Importance

Changes 0
Metric Value
eloc 70
dl 0
loc 113
ccs 0
cts 70
cp 0
rs 5.5666
c 0
b 0
f 0
cc 16
nc 12
nop 2
crap 272

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\Http\Controllers\Controller;
9
use App\Models\Skill;
10
use App\Models\Applicant;
11
use App\Models\Reference;
12
use App\Models\Lookup\Relationship;
13
use App\Models\Project;
14
15
class ReferencesController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ReferencesController
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 references
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_04_references', [
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_references'),
42
            'relative_template' => Lang::get('common/relatives'),
43
            'skills' => Skill::all(),
44
            'relationships' => Relationship::all(),
45
            'form_submit_action' => route('profile.references.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

45
            'form_submit_action' => route('profile.references.update', /** @scrutinizer ignore-type */ $applicant),
Loading history...
46
        ]);
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...
47
    }
48
49
    /**
50
     * Update the applicant's references in storage.
51
     *
52
     * @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...
53
     * @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...
54
     * @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...
55
     */
56
    public function update(Request $request, Applicant $applicant)
57
    {
58
        $input = $request->input();
59
60
        Debugbar::info($input);
61
62
        $references = $input['references'];
63
64
        //Delete old references that weren't resubmitted
65
        //Note: this must be done before adding new references, so we don't delete
66
        // them right after adding them
67
        foreach($applicant->references as $oldReference) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
68
            //Check if no references were resubmitted, or if this specific one wasn't
69
            if (!isset($references['old']) ||
70
                !isset($references['old'][$oldReference->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...
71
                $oldReference->delete();
72
            }
73
        }
74
75
        //Save new references
76
        if (isset($references['new'])) {
77
            foreach($references['new'] as $referenceInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
78
                $reference = new Reference();
79
                $reference->applicant_id = $applicant->id;
80
                $reference->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...
81
                    'name' => $referenceInput['name'],
82
                    'email' => $referenceInput['email'],
83
                    'relationship_id' => $referenceInput['relationship_id'],
84
                    'description' => $referenceInput['description'],
85
                ]);
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...
86
87
                $reference->save();
88
89
                $projectIds = [];
90
                $projects = $referenceInput['projects'];
91
                if (isset($projects['new'])) {
92
                    foreach($projects['new'] as $projectInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
93
                        $project = new Project();
94
                        $project->applicant_id = $applicant->id;
95
                        $project->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...
96
                            'name' => $projectInput['name'],
97
                            'start_date' => $projectInput['start_date'],
98
                            'end_date' => $projectInput['end_date'],
99
                        ]);
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...
100
                        $project->save();
101
                        $projectIds[] = $project->id;
102
                    }
103
                }
104
                //Sync attaches the specified ids, and detaches all others
105
                $reference->projects()->sync($projectIds);
106
107
108
                $skillDeclarationIds =$this->getRelativeIds($referenceInput, 'skills');
109
                $reference->skill_declarations()->sync($skillDeclarationIds);
110
            }
111
        }
112
113
        //Update old references
114
        if (isset($references['old'])) {
115
            foreach($references['old'] as $id=>$referenceInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
116
                //Ensure this reference belongs to this applicant
117
                $reference = $applicant->references->firstWhere('id', $id);
118
                if ($reference != null) {
119
                    $reference->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...
120
                        'name' => $referenceInput['name'],
121
                        'email' => $referenceInput['email'],
122
                        'relationship_id' => $referenceInput['relationship_id'],
123
                        'description' => $referenceInput['description'],
124
                    ]);
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...
125
                    $reference->save();
126
127
                    $projectIds = [];
128
                    $projects = $referenceInput['projects'];
129
                    if (isset($projects['new'])) {
130
                        foreach($projects['new'] as $projectInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
131
                            $project = new Project();
132
                            $project->applicant_id = $applicant->id;
133
                            $project->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...
134
                                'name' => $projectInput['name'],
135
                                'start_date' => $projectInput['start_date'],
136
                                'end_date' => $projectInput['end_date'],
137
                            ]);
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...
138
                            $project->save();
139
                            $projectIds[] = $project->id;
140
                        }
141
                    }
142
                    if (isset($projects['old'])) {
143
                        foreach($projects['old'] as $projectId=>$projectInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
144
                            //Ensure this project belongs to this applicant
145
                            $project = $applicant->projects->firstWhere('id', $projectId);
146
                            if ($project != null) {
147
                                $project->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...
148
                                    'name' => $projectInput['name'],
149
                                    'start_date' => $projectInput['start_date'],
150
                                    'end_date' => $projectInput['end_date'],
151
                                ]);
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...
152
                                $project->save();
153
                                $projectIds[] = $project->id;
154
                            }
155
                        }
156
                    }
157
                    //Sync attaches the specified ids, and detaches all others
158
                    $reference->projects()->sync($projectIds);
159
160
                    $skillDeclarationIds =$this->getRelativeIds($referenceInput, 'skills');
161
                    $reference->skill_declarations()->sync($skillDeclarationIds);
162
                } else {
163
                    Debugbar::warning('Applicant '.$applicant->id.' attempted to update reference with invalid id '.$id);
164
                }
165
            }
166
        }
167
168
        return redirect( route('profile.references.edit', $applicant) );
1 ignored issue
show
Bug Best Practice introduced by
The expression return redirect(route('p...ces.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

168
        return redirect( route('profile.references.edit', /** @scrutinizer ignore-type */ $applicant) );
Loading history...
169
        // return view('applicant/profile_04_references', [
170
        //     'applicant' => $applicant,
171
        //     'profile' => Lang::get('applicant/profile_references'),
172
        //     'relative_template' => Lang::get('common/relatives'),
173
        //     'skills' => Skill::all(),
174
        //     'relationships' => Relationship::all(),
175
        //     'form_submit_action' => route('profile.references.update', $applicant),
176
        // ]);
177
    }
178
179
}
180