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

DevSeeder::run()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 83
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 49
dl 0
loc 83
rs 8.8016
c 3
b 0
f 0
cc 5
nc 16
nop 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
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
use App\Models\RatingGuideQuestion;
12
use App\Models\RatingGuideAnswer;
13
14
class DevSeeder extends Seeder // phpcs:ignore
15
{
16
    /**
17
     * This seeder uses this email for an admin user.
18
     * Note: all seeded users have 'password' for a password.
19
     *
20
     * @var string
21
     */
22
    protected $adminEmail = '[email protected]';
23
24
    /**
25
     * This seeder connects all manager objects to this user.
26
     * Note: all seeded users have 'password' for a password.
27
     *
28
     * @var string
29
     */
30
    protected $managerEmail = '[email protected]';
31
32
    /**
33
     * This seeder attaches all applicant objects to this user.
34
     * Note: all seeded users have 'password' for a password.
35
     *
36
     * @var string
37
     */
38
    protected $applicantEmail = '[email protected]';
39
40
41
42
    /**
43
     * Run the database seeds.
44
     *
45
     * @return void
46
     */
47
    public function run() : void
48
    {
49
        $adminUser = User::where('email', $this->adminEmail)->first();
50
        if ($adminUser === null) {
51
            $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...
52
        }
53
54
        $managerUser = User::where('email', $this->managerEmail)->first();
55
        // Create the test manager if it does not exist yet
56 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...
57
            $managerUser = factory(User::class)->states('manager')->create(['email' => $this->managerEmail]);
58
            $managerUser->manager()->save(factory(Manager::class)->create([
59
                'user_id' => $managerUser->id
60
            ]));
61
        }
62
63
        factory(JobPoster::class, 3)->states('published')->create([
64
            'manager_id' => $managerUser->manager->id
65 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...
66
            $job->job_applications()->saveMany(factory(JobApplication::class, 5))->create([
67
                'job_poster_id' => $job->id
68
            ]);
69
            //Then create one application with a priority user
70
            $job->job_applications()->save(factory(JobApplication::class)->create([
71
                'job_poster_id' => $job->id,
72
                'applicant_id' => factory(Applicant::class)->create([
73
                        'user_id' => factory(User::class)->states('priority')->create()->id
74
                    ])->id
75
            ]));
76
        });
77
        factory(JobPoster::class, 3)->states('closed')->create([
78
            'manager_id' => $managerUser->manager->id
79 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...
80
            $job->job_applications()->saveMany(factory(JobApplication::class, 5))->create([
81
                'job_poster_id' => $job->id
82
            ]);
83
            //Then create one application with a priority user
84
            $job->job_applications()->save(factory(JobApplication::class)->create([
85
                'job_poster_id' => $job->id,
86
                'applicant_id' => factory(Applicant::class)->create([
87
                    'user_id' => factory(User::class)->state('priority')->create()->id
88
                ])->id
89
            ]));
90
        });
91
        factory(JobPoster::class, 3)->states('draft')->create([
92
            'manager_id' => $managerUser->manager->id
93
        ]);
94
        factory(JobPoster::class, 3)->states('review_requested')->create([
95
            'manager_id' => $managerUser->manager->id
96
        ]);
97
98
        // Create a Job Poster with an Assessment Plan
99
        $jobWithAssessment = factory(JobPoster::class)->states('draft')->create([
100
            'manager_id' => $managerUser->manager->id,
101
        ]);
102
        foreach ($jobWithAssessment->criteria as $criterion) {
103
            // Create an assessment for each criterion
104
            factory(Assessment::class)->states('withRatingGuide')->create([
105
                'criterion_id' => $criterion->id,
106
            ]);
107
        };
108
109
        $applicantUser = User::where('email', $this->applicantEmail)->first();
110 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...
111
            $applicantUser = factory(User::class)->states('applicant')->create([
112
                'email' => $this->applicantEmail
113
            ]);
114
            $applicantUser->applicant()->save(factory(Applicant::class)->create([
115
                'user_id' => $applicantUser->id
116
            ]));
117
        }
118
119
        // Add to application profile
120
        $applicantUser->applicant->references()->saveMany(factory(Reference::class, 3)->create([
121
            'applicant_id' => $applicantUser->applicant->id
122
        ]));
123
124
        // Create several applications for test user.
125
        $applicantUser->applicant->job_applications()->saveMany(factory(JobApplication::class, 3)->create([
126
            'applicant_id' => $applicantUser->applicant->id,
127
        ]));
128
        $applicantUser->applicant->job_applications()->saveMany(factory(JobApplication::class, 2)->states('draft')->create([
129
            'applicant_id' => $applicantUser->applicant->id,
130
        ]));
131
    }
132
}
133