Test Setup Failed
Push — feature/word_counter ( e4aaca...45e6a8 )
by Yonathan
14:36
created

ReferencesController::update()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 5.0187

Importance

Changes 0
Metric Value
cc 5
nc 8
nop 2
dl 0
loc 53
ccs 30
cts 33
cp 0.9091
crap 5.0187
rs 8.7143
c 0
b 0
f 0

How to fix   Long Method   

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
3
namespace App\Http\Controllers;
4
5
use Illuminate\Support\Facades\Lang;
6
use Illuminate\Support\Facades\Log;
7
use Illuminate\Http\Request;
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\Project;
13
use App\Models\Lookup\Relationship;
14
use App\Services\Validation\Requests\UpdateReferenceValidator;
15
16
class ReferencesController extends Controller
17
{
18
19
    /**
20
     * Show the form for editing the logged-in applicant's references
21
     *
22
     * @param  Request $request
23
     * @return \Illuminate\Http\RedirectResponse
24
     */
25 1
    public function editAuthenticated(Request $request): \Illuminate\Http\RedirectResponse
26
    {
27 1
        $applicant = $request->user()->applicant;
28 1
        return redirect(route('profile.references.edit', $applicant));
29
    }
30
31
    /**
32
     * Show the form for editing the applicant's references
33
     *
34
     * @param Request   $request   Incoming request object.
35
     * @param Applicant $applicant Incoming applicant object.
36
     *
37
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
38
     */
39 1
    public function edit(Request $request, Applicant $applicant)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

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

Loading history...
40
    {
41 1
        $applicant->load([
42 1
            'references.projects',
43
            'skill_declarations.skill',
44
        ]);
45
46 1
        return view('applicant/profile_04_references', [
47 1
            'applicant' => $applicant,
48 1
            'profile' => Lang::get('applicant/profile_references'),
49
        ]);
50
    }
51
52
    /**
53
     * Update or create a reference with the supplied data.
54
     *
55
     * @param \Illuminate\Http\Request   $request   The incoming request object.
56
     * @param \App\Models\Reference|null $reference The reference to update. If null, a new one should be created.
57
     *
58
     * @return
59
     */
60 2
    public function update(Request $request, ?Reference $reference = null)
61
    {
62 2
        $validator = new UpdateReferenceValidator();
63 2
        $validator->validate($request->input());
0 ignored issues
show
Documentation introduced by
$request->input() is of type string|array|null, but the function expects a array<integer,*>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
64
65 2
        if ($reference === null) {
66 1
            $reference = new Reference();
67 1
            $reference->applicant_id = $request->user()->applicant->id;
68
        }
69 2
        $reference->fill([
70 2
            'name' => $request->input('name'),
71 2
            'email' => $request->input('email'),
72 2
            'relationship_id' => $request->input('relationship_id'),
73 2
            'description' => $request->input('description'),
74
        ]);
75 2
        $reference->save();
76
77 2
        $reference->load('projects');
78
79
        //TODO: As soon as you can interact with projects outside of references,
80
        //  this will become a dangerous operation
81 2
        $reference->projects()->delete();
82
83 2
        $newProjects = [];
84 2
        if ($request->input('projects')) {
85 2
            foreach ($request->input('projects') as $projectInput) {
0 ignored issues
show
Bug introduced by
The expression $request->input('projects') of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
86 2
                $project = new Project();
87 2
                $project->applicant_id = $reference->applicant_id;
88 2
                $project->fill([
89 2
                    'name' => $projectInput['name'],
90 2
                    'start_date' => $projectInput['start_date'],
91 2
                    'end_date' => $projectInput['end_date'],
92
                ]);
93 2
                $project->save();
94 2
                $newProjects[] = $project->id;
95
                // $reference->projects()->attach($project);
96
            }
97
        }
98 2
        $reference->projects()->sync($newProjects);
99
100
        //Attach relatives
101 2
        $skillIds = $this->getRelativeIds($request->input(), 'skills');
102 2
        $reference->skill_declarations()->sync($skillIds);
103
104
        // if an ajax request, return the new object
105 2
        if ($request->ajax()) {
106
            $reference->load('relationship');
107
            $reference->load('projects');
108
            return $reference->toJson();
109
        } else {
110 2
            return redirect()->back();
111
        }
112
    }
113
114
    /**
115
     * Delete the particular reference from storage.
116
     *
117
     * @param  \Illuminate\Http\Request $request
118
     * @param  \App\Models\Reference    $reference
119
     * @return \Illuminate\Http\Response
120
     */
121 1
    public function destroy(Request $request, Reference $reference)
122
    {
123 1
        $this->authorize('delete', $reference);
124
125
        //TODO: when projects exist independently on profile, delete seperatley
126 1
        $reference->projects()->delete();
127
128 1
        $reference->delete();
129
130 1
        if ($request->ajax()) {
131
            return [
132
                "message" => 'Reference deleted'
133
            ];
134
        }
135
136 1
        return redirect()->back();
137
    }
138
}
139