Test Setup Failed
Branch feature/job-builder/work-env-r... (305963)
by Grant
52:57 queued 34:52
created

DevSeeder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 119
Duplicated Lines 31.93 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 38
loc 119
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 38 85 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
use Illuminate\Database\Seeder;
4
use App\Models\Manager;
5
use App\Models\User;
6
use App\Models\JobPoster;
7
use App\Models\Applicant;
8
use App\Models\JobApplication;
9
use App\Models\Reference;
10
use App\Models\Assessment;
11
12
class DevSeeder extends Seeder // phpcs:ignore
13
{
14
    /**
15
     * This seeder uses this email for an admin user.
16
     * Note: all seeded users have 'password' for a password.
17
     *
18
     * @var string
19
     */
20
    protected $adminEmail = '[email protected]';
21
22
    /**
23
     * This seeder connects all manager objects to this user.
24
     * Note: all seeded users have 'password' for a password.
25
     *
26
     * @var string
27
     */
28
    protected $managerEmail = '[email protected]';
29
30
    /**
31
     * This seeder attaches all applicant objects to this user.
32
     * Note: all seeded users have 'password' for a password.
33
     *
34
     * @var string
35
     */
36
    protected $applicantEmail = '[email protected]';
37
38
39
40
    /**
41
     * Run the database seeds.
42
     *
43
     * @return void
44
     */
45
    public function run() : void
46
    {
47
        $adminUser = User::where('email', $this->adminEmail)->first();
48
        if ($adminUser === null) {
49
            $adminUser = factory(User::class)->states('admin')->create(['email' => $this->adminEmail]);
0 ignored issues
show
Unused Code introduced by
$adminUser is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
50
        }
51
52
        $managerUser = User::where('email', $this->managerEmail)->first();
53
        // Create the test manager if it does not exist yet
54 View Code Duplication
        if ($managerUser === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
            $managerUser = factory(User::class)->states('manager')->create(['email' => $this->managerEmail]);
56
            $managerUser->manager()->save(factory(Manager::class)->create([
57
                'user_id' => $managerUser->id
58
            ]));
59
        }
60
61
        factory(JobPoster::class, 3)->states('published')->create([
62
            'manager_id' => $managerUser->manager->id
63 View Code Duplication
        ])->each(function ($job) : void {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
            $job->job_applications()->saveMany(factory(JobApplication::class, 5))->create([
65
                'job_poster_id' => $job->id
66
            ]);
67
            //Then create one application with a priority user
68
            $job->job_applications()->save(factory(JobApplication::class)->create([
69
                'job_poster_id' => $job->id,
70
                'applicant_id' => factory(Applicant::class)->create([
71
                        'user_id' => factory(User::class)->states('priority')->create()->id
72
                    ])->id
73
            ]));
74
        });
75
        factory(JobPoster::class, 3)->states('closed')->create([
76
            'manager_id' => $managerUser->manager->id
77 View Code Duplication
        ])->each(function ($job) : void {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
            $job->job_applications()->saveMany(factory(JobApplication::class, 5))->create([
79
                'job_poster_id' => $job->id
80
            ]);
81
            //Then create one application with a priority user
82
            $job->job_applications()->save(factory(JobApplication::class)->create([
83
                'job_poster_id' => $job->id,
84
                'applicant_id' => factory(Applicant::class)->create([
85
                    'user_id' => factory(User::class)->state('priority')->create()->id
86
                ])->id
87
            ]));
88
        });
89
        factory(JobPoster::class, 3)->states('draft')->create([
90
            'manager_id' => $managerUser->manager->id
91
        ]);
92
        factory(JobPoster::class, 3)->states('review_requested')->create([
93
            'manager_id' => $managerUser->manager->id
94
        ]);
95
96
        // Create a Job Poster with an Assessment Plan
97
        $jobWithAssessment = factory(JobPoster::class)->states('draft')->create([
98
            'manager_id' => $managerUser->manager->id,
99
        ]);
100
        foreach ($jobWithAssessment->criteria as $criterion) {
101
            // Create an assessment for each criterion
102
            factory(Assessment::class)->states('withRatingGuide')->create([
103
                'criterion_id' => $criterion->id,
104
            ]);
105
        };
106
107
        $applicantUser = User::where('email', $this->applicantEmail)->first();
108 View Code Duplication
        if ($applicantUser === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
            $applicantUser = factory(User::class)->states('applicant')->create([
110
                'email' => $this->applicantEmail
111
            ]);
112
            $applicantUser->applicant()->save(factory(Applicant::class)->create([
113
                'user_id' => $applicantUser->id
114
            ]));
115
        }
116
117
        // Add to application profile
118
        $applicantUser->applicant->references()->saveMany(factory(Reference::class, 3)->create([
119
            'applicant_id' => $applicantUser->applicant->id
120
        ]));
121
122
        // Create several applications for test user.
123
        $applicantUser->applicant->job_applications()->saveMany(factory(JobApplication::class, 3)->create([
124
            'applicant_id' => $applicantUser->applicant->id,
125
        ]));
126
        $applicantUser->applicant->job_applications()->saveMany(factory(JobApplication::class, 2)->states('draft')->create([
127
            'applicant_id' => $applicantUser->applicant->id,
128
        ]));
129
    }
130
}
131