Passed
Push — task/job-page-performance ( e0bcd3...e4f7e1 )
by Chris
07:38
created

JobController   B

Complexity

Total Complexity 50

Size/Duplication

Total Lines 584
Duplicated Lines 0 %

Test Coverage

Coverage 72.82%

Importance

Changes 0
Metric Value
wmc 50
eloc 275
dl 0
loc 584
ccs 193
cts 265
cp 0.7282
rs 8.4
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A fillAndSaveJobPosterTasks() 0 24 5
A populateDefaultQuestions() 0 35 3
A index() 0 23 1
B populateCreateView() 0 97 4
A fillAndSaveJobPosterQuestions() 0 26 5
A create() 0 3 1
A fillAndSaveJobPoster() 0 37 2
A managerIndex() 0 32 4
A submitForReview() 0 19 2
B show() 0 70 8
A store() 0 26 3
B fillAndSaveJobPosterCriteria() 0 40 10
A destroy() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like JobController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use JobController, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Support\Facades\Lang;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\Facades\Log;
8
use Illuminate\Support\Facades\Mail;
9
use Illuminate\Http\RedirectResponse;
10
use Illuminate\Http\Request;
11
use Illuminate\Http\Response;
12
use Illuminate\View\View;
13
use App\Http\Controllers\Controller;
14
15
use Carbon\Carbon;
16
17
use App\Mail\JobPosterReviewRequested;
18
19
use App\Models\JobPoster;
20
use App\Models\JobPosterQuestion;
21
use App\Models\Lookup\JobTerm;
22
use App\Models\Lookup\Province;
23
use App\Models\Lookup\SecurityClearance;
24
use App\Models\Lookup\LanguageRequirement;
25
use App\Models\Lookup\CitizenshipDeclaration;
26
use App\Models\Lookup\Department;
27
use App\Models\Lookup\SkillLevel;
28
use App\Models\Lookup\CriteriaType;
29
use App\Models\Lookup\VeteranStatus;
30
use App\Models\JobApplication;
31
use App\Models\Criteria;
32
use App\Models\Skill;
33
use App\Models\JobPosterKeyTask;
34
35
use App\Services\Validation\JobPosterValidator;
36
use Jenssegers\Date\Date;
37
38
class JobController extends Controller
39
{
40
    /**
41
     * Display a listing of JobPosters.
42
     *
43
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
44
     */
45
    public function index()
46
    {
47
        $now = Carbon::now();
48
49
        // Find published jobs that are currently open for applications.
50
        // Eager load required relationships: Department, Province, JobTerm.
51
        // Eager load the count of submitted applications, to prevent the relationship
52
        // from being actually loaded and firing off events.
53
        $jobs = JobPoster::where('open_date_time', '<=', $now)
54
            ->where('close_date_time', '>=', $now)
55
            ->where('published', true)
56
            ->with([
57
                'department',
58
                'province',
59
                'job_term',
60
            ])
61
            ->withCount([
62
                'submitted_applications',
63
            ])
64
            ->get();
65
        return view('applicant/job_index', [
66
            'job_index' => Lang::get('applicant/job_index'),
67
            'jobs' => $jobs
68
        ]);
69
    }
70
71
    /**
72
     * Display a listing of a manager's JobPosters.
73
     *
74
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
75
     */
76 1
    public function managerIndex()
77
    {
78 1
        $manager = Auth::user()->manager;
0 ignored issues
show
Bug introduced by
Accessing manager on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
79
80 1
        $veteran_applications = [];
81 1
        $citizen_applications = [];
82 1
        $other_applications = [];
83
84 1
        foreach ($manager->job_posters as $job) {
85 1
            $job->submitted_applications->load(['veteran_status', 'citizenship_declaration']);
86
            $veteran_applications[$job->id] = $job->submitted_applications->filter(function ($application) {
87
                return $application->veteran_status->name !== "none" &&
88
                    $application->citizenship_declaration->name === "citizen";
89 1
            });
90
            $citizen_applications[$job->id] = $job->submitted_applications->filter(function ($application) {
91
                return $application->veteran_status->name === "none" &&
92
                    $application->citizenship_declaration->name === "citizen";
93 1
            });
94
            $other_applications[$job->id] = $job->submitted_applications->filter(function ($application) {
95
                return $application->citizenship_declaration->name !== "citizen";
96 1
            });
97
        }
98
99 1
        return view('manager/job_index', [
100
            /*Localization Strings*/
101 1
            'jobs_l10n' => Lang::get('manager/job_index'),
102
103
            /* Data */
104 1
            'jobs' => $manager->job_posters,
105 1
            'veteran_applications' => $veteran_applications,
106 1
            'citizen_applications' => $citizen_applications,
107 1
            'other_applications' => $other_applications,
108
        ]);
109
    }
110
111
    /**
112
     * Submit the Job Poster for review.
113
     *
114
     * @param \Illuminate\Http\Request $request   Incoming request object.
115
     * @param \App\Models\JobPoster    $jobPoster Job Poster object.
116
     *
117
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
118
     */
119 1
    public function submitForReview(Request $request, JobPoster $jobPoster)
120
    {
121
        // Update review request timestamp
122 1
        $jobPoster->review_requested_at = new Date();
123 1
        $jobPoster->save();
124 1
        $jobPoster->refresh();
125
126
        // Send email
127 1
        $reviewer_email = config('mail.reviewer_email');
128 1
        if (isset($reviewer_email)) {
129 1
            Mail::to($reviewer_email)->send(new JobPosterReviewRequested($jobPoster, Auth::user()));
0 ignored issues
show
Bug introduced by
It seems like Illuminate\Support\Facades\Auth::user() can also be of type null; however, parameter $manager of App\Mail\JobPosterReviewRequested::__construct() does only seem to accept App\Models\User, maybe add an additional type check? ( Ignorable by Annotation )

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

129
            Mail::to($reviewer_email)->send(new JobPosterReviewRequested($jobPoster, /** @scrutinizer ignore-type */ Auth::user()));
Loading history...
130
        } else {
131
            Log::error('The reviewer email environment variable is not set.');
132
        }
133
134 1
        return view('manager/job_index/job', [
135
            /*Localization Strings*/
136 1
            'jobs_l10n' => Lang::get('manager/job_index'),
137 1
            'job' => $jobPoster
138
        ]);
139
    }
140
141
    /**
142
     * Delete a draft Job Poster.
143
     *
144
     * @param \Illuminate\Http\Request $request   Incoming request object.
145
     * @param \App\Models\JobPoster    $jobPoster Job Poster object.
146
     *
147
     * @return void
148
     */
149
    public function destroy(Request $request, JobPoster $jobPoster) : void
150
    {
151
        $jobPoster->delete();
152
    }
153
154
    /**
155
     * Display the specified job poster.
156
     *
157
     * @param \Illuminate\Http\Request $request   Incoming request object.
158
     * @param \App\Models\JobPoster    $jobPoster Job Poster object.
159
     *
160
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
161
     */
162 3
    public function show(Request $request, JobPoster $jobPoster)
163
    {
164 3
        $user = Auth::user();
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
165
166
        //TODO: Improve workplace photos, and reference them in template direction from WorkEnvironment model
167 3
        $workplacePhotos = [];
168 3
        foreach ($jobPoster->manager->work_environment->workplace_photo_captions as $photoCaption) {
169
            $workplacePhotos[] = [
170
                'description' => $photoCaption->description,
171
                'url' => '/images/user.png'
172
            ];
173
        }
174
175
        //TODO: replace route('manager.show',manager.id) in templates with link using slug
176
177
        $criteria = [
178 3
            'essential' => $jobPoster->criteria->filter(
179
                function ($value, $key) {
180 2
                    return $value->criteria_type->name == 'essential';
181 3
                }
182
            ),
183 3
            'asset' => $jobPoster->criteria->filter(
184
                function ($value, $key) {
185 2
                    return $value->criteria_type->name == 'asset';
186 3
                }
187
            ),
188
        ];
189
190 3
        $jobLang = Lang::get('applicant/job_post');
191
192 3
        $applyButton = [];
193 3
        if (!$jobPoster->published && $this->authorize('update', $jobPoster)) {
194
            $applyButton = [
195 1
                'href' => route('manager.jobs.edit', $jobPoster->id),
196 1
                'title' => $jobLang['apply']['edit_link_title'],
197 1
                'text' => $jobLang['apply']['edit_link_label'],
198
            ];
199 2
        } elseif (Auth::check() && $jobPoster->isOpen()) {
200
            $applyButton = [
201 1
                'href' => route('job.application.edit.1', $jobPoster->id),
202 1
                'title' => $jobLang['apply']['apply_link_title'],
203 1
                'text' => $jobLang['apply']['apply_link_label'],
204
            ];
205 1
        } elseif (Auth::guest() && $jobPoster->isOpen()) {
206
            $applyButton = [
207 1
                'href' => route('job.application.edit.1', $jobPoster->id),
208 1
                'title' => $jobLang['apply']['login_link_title'],
209 1
                'text' => $jobLang['apply']['login_link_label'],
210
            ];
211
        } else {
212
            $applyButton = [
213
                'href' => null,
214
                'title' => null,
215
                'text' => $jobLang['apply']['job_closed_label'],
216
            ];
217
        }
218
219 3
        return view(
220 3
            'applicant/job_post',
221
            [
222 3
                'job_post' => $jobLang,
223 3
                'manager' => $jobPoster->manager,
224 3
                'manager_profile_photo_url' => '/images/user.png', //TODO get real photo
225 3
                'team_culture' => $jobPoster->manager->team_culture,
226 3
                'work_environment' => $jobPoster->manager->work_environment,
227 3
                'workplace_photos' => $workplacePhotos,
228 3
                'job' => $jobPoster,
229 3
                'criteria' => $criteria,
230 3
                'apply_button' => $applyButton,
231 3
                'skill_template' => Lang::get('common/skills'),
232
            ]
233
        );
234
    }
235
236
    /**
237
     * Display the form for creating a new Job Poster
238
     *
239
     * @param \Illuminate\Http\Request $request Incoming request object.
240
     *
241
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view
242
     */
243 1
    public function create(Request $request)
244
    {
245 1
        return $this->populateCreateView($request);
246
    }
247
248
    /**
249
     * Display the form for editing an existing Job Poster
250
     *
251
     * @param \Illuminate\Http\Request $request   Incoming request object.
252
     * @param \App\Models\JobPoster    $jobPoster Job Poster object.
253
     *
254
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view
255
     */
256 1
    public function edit(Request $request, JobPoster $jobPoster)
257
    {
258 1
        return $this->populateCreateView($request, $jobPoster);
259
    }
260
261
    /**
262
     * Get the manager from the request object and check if creating or editing
263
     *
264
     * @param \Illuminate\Http\Request $request   Incoming request object.
265
     * @param \App\Models\JobPoster    $jobPoster Optional Job Poster object.
266
     *
267
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view
268
     */
269 2
    public function populateCreateView(Request $request, JobPoster $jobPoster = null)
270
    {
271 2
        $manager = $request->user() ? $request->user()->manager : null;
272 2
        if (isset($jobPoster)) {
273 1
            $job = $jobPoster;
274 1
            $route = ['manager.jobs.store', $jobPoster];
275 1
            $jobHeading = 'manager/job_edit';
276
        } else {
277 1
            $job = [];
278 1
            $defaultQuestions = $this->populateDefaultQuestions();
279 1
            if (!empty($defaultQuestions)) {
280 1
                $job['job_poster_questions'] = $defaultQuestions;
281
            }
282 1
            $route = ['manager.jobs.store'];
283 1
            $jobHeading = 'manager/job_create';
284
        }
285
286 2
        $skillLangs = Lang::get('common/skills');
287
288 2
        $softSkills = Skill::whereHas(
289 2
            'skill_type',
290
            function ($query) : void {
291 2
                $query->where('name', '=', 'soft');
292 2
            }
293 2
        )->get()
294 2
            ->mapWithKeys(
295
                function ($skill) {
296
                    return [
297 2
                        $skill->id => $skill->name
298
                    ];
299 2
                }
300
            )
301 2
            ->all();
302
303 2
        $hardSkills = Skill::whereHas(
304 2
            'skill_type',
305
            function ($query) : void {
306 2
                $query->where('name', '=', 'hard');
307 2
            }
308 2
        )->get()
309 2
            ->mapWithKeys(
310
                function ($skill) {
311
                    return [
312 2
                        $skill->id => $skill->name
313
                    ];
314 2
                }
315
            )
316 2
            ->all();
317
318 2
        asort($softSkills, SORT_LOCALE_STRING);
319 2
        asort($hardSkills, SORT_LOCALE_STRING);
320
321
        $skills = [
322
            'essential' => [
323 2
                'hard' => $hardSkills,
324 2
                'soft' => $softSkills
325
            ],
326
            'asset' => [
327 2
                'hard' => $hardSkills,
328 2
                'soft' => $softSkills
329
            ]
330
        ];
331
332 2
        $skillLevelCollection = SkillLevel::all();
333
334 2
        $skillLevels = array();
335
336 2
        $skillLevels['hard'] = $skillLevelCollection->mapWithKeys(
337
            function ($skillLevel) use ($skillLangs) {
338 2
                return [$skillLevel->id => $skillLangs['skill_levels']['hard'][$skillLevel->name]];
339 2
            }
340 2
        )->all();
341
342 2
        $skillLevels['soft'] = $skillLevelCollection->mapWithKeys(
343
            function ($skillLevel) use ($skillLangs) {
344 2
                return [$skillLevel->id => $skillLangs['skill_levels']['soft'][$skillLevel->name]];
345 2
            }
346 2
        )->all();
347
348 2
        return view(
349 2
            'manager/job_create',
350
            [
351
                /*Localization Strings*/
352 2
                'job_l10n' => Lang::get('manager/job_create'),
353
354
                /* Data */
355 2
                'job' => Lang::get($jobHeading),
356 2
                'manager' => $manager,
357 2
                'provinces' => Province::all(),
358 2
                'departments' => Department::all(),
359 2
                'language_requirments' => LanguageRequirement::all(),
360 2
                'security_clearances' => SecurityClearance::all(),
361 2
                'job' => $job,
362 2
                'form_action_url' => route(/** @scrutinizer ignore-type */ ...$route), // phpcs:ignore
0 ignored issues
show
Bug introduced by
$route is expanded, but the parameter $name of route() does not expect variable arguments. ( Ignorable by Annotation )

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

362
                'form_action_url' => route(/** @scrutinizer ignore-type */ /** @scrutinizer ignore-type */ ...$route), // phpcs:ignore
Loading history...
363 2
                'skills' => $skills,
364 2
                'skill_levels' => $skillLevels,
365 2
                'skill_template' => $skillLangs,
366
            ]
367
        );
368
    }
369
370
    /**
371
     * Create a new resource in storage
372
     *
373
     * @param \Illuminate\Http\Request $request   Incoming request object.
374
     * @param \App\Models\JobPoster    $jobPoster Optional Job Poster object.
375
     *
376
     * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse A redirect to the Job Index
377
     */
378 1
    public function store(Request $request, JobPoster $jobPoster = null)
379
    {
380
        // Don't allow edits for published Job Posters
381
        // Also check auth while we're at it
382 1
        if (isset($jobPoster)) {
383
            $this->authorize('update', $jobPoster);
384
            JobPosterValidator::validateUnpublished($jobPoster);
385
        } else {
386 1
            $this->authorize('create', JobPoster::class);
387
        }
388
389 1
        $input = $request->input();
390
391 1
        $job = (isset($jobPoster) ? $jobPoster : new JobPoster());
392
393 1
        $job->manager_id = $request->user()->manager->id;
394
395 1
        $this->fillAndSaveJobPoster($input, $job);
396
397 1
        $this->fillAndSaveJobPosterTasks($input, $job, isset($jobPoster));
398
399 1
        $this->fillAndSaveJobPosterQuestions($input, $job, isset($jobPoster));
400
401 1
        $this->fillAndSaveJobPosterCriteria($input, $job, isset($jobPoster));
402
403 1
        return redirect(route('manager.jobs.show', $job->id));
404
    }
405
406
    /**
407
     * Fill Job Poster model's properties and save
408
     *
409
     * @param mixed[]               $input     Field values.
410
     * @param \App\Models\JobPoster $jobPoster Job Poster object.
411
     *
412
     * @return void
413
     */
414 1
    protected function fillAndSaveJobPoster(array $input, JobPoster $jobPoster) : void
415
    {
416 1
        $jobPoster->fill(
417
            [
418 1
                'job_term_id' => JobTerm::where('name', 'month')->firstOrFail()->id,
419 1
                'term_qty' => $input['term_qty'],
420 1
                'open_date_time' => new Date($input['open_date'] . $input['open_time']),
421 1
                'close_date_time' => new Date($input['close_date'] . $input['close_time']),
422 1
                'start_date_time' => new Date($input['start_date_time']),
423 1
                'department_id' => $input['department'],
424 1
                'province_id' => $input['province'],
425 1
                'salary_min' => $input['salary_min'],
426 1
                'salary_max' => $input['salary_max'],
427 1
                'noc' => $input['noc'],
428 1
                'classification' => $input['classification'],
429 1
                'security_clearance_id' => $input['security_clearance'],
430 1
                'language_requirement_id' => $input['language_requirement'],
431 1
                'remote_work_allowed' => (isset($input['remote_work_allowed']) ? $input['remote_work_allowed'] : false),
432
                'en' => [
433 1
                    'city' => $input['city'],
434 1
                    'title' => $input['title']['en'],
435 1
                    'impact' => $input['impact']['en'],
436 1
                    'branch' => $input['branch']['en'],
437 1
                    'division' => $input['division']['en'],
438 1
                    'education' => $input['education']['en'],
439
                ],
440
                'fr' => [
441 1
                    'city' => $input['city'],
442 1
                    'title' => $input['title']['fr'],
443 1
                    'impact' => $input['impact']['fr'],
444 1
                    'branch' => $input['branch']['fr'],
445 1
                    'division' => $input['division']['fr'],
446 1
                    'education' => $input['education']['fr'],
447
                ],
448
            ]
449
        );
450 1
        $jobPoster->save();
451 1
    }
452
453
    /**
454
     * Fill Job Poster's tasks and save
455
     *
456
     * @param mixed[]               $input     Field values.
457
     * @param \App\Models\JobPoster $jobPoster Job Poster object.
458
     * @param boolean               $replace   Remove existing relationships.
459
     *
460
     * @return void
461
     */
462 1
    protected function fillAndSaveJobPosterTasks(array $input, JobPoster $jobPoster, bool $replace) : void
463
    {
464 1
        if ($replace) {
465
            $jobPoster->job_poster_key_tasks()->delete();
466
        }
467
468 1
        if (!array_key_exists('task', $input) || !is_array($input['task'])) {
469 1
            return;
470
        }
471
472
        foreach ($input['task'] as $task) {
473
            $jobPosterTask = new JobPosterKeyTask();
474
            $jobPosterTask->job_poster_id = $jobPoster->id;
475
            $jobPosterTask->fill(
476
                [
477
                    'en' => [
478
                        'description' => $task['en']
479
                    ],
480
                    'fr' => [
481
                        'description' => $task['fr']
482
                    ]
483
                ]
484
            );
485
            $jobPosterTask->save();
486
        }
487
    }
488
489
    /**
490
     * Fill Job Poster's questions and save
491
     *
492
     * @param mixed[]               $input     Field values.
493
     * @param \App\Models\JobPoster $jobPoster Job Poster object.
494
     * @param boolean               $replace   Remove existing relationships.
495
     *
496
     * @return void
497
     */
498 1
    protected function fillAndSaveJobPosterQuestions(array $input, JobPoster $jobPoster, bool $replace) : void
499
    {
500 1
        if ($replace) {
501
            $jobPoster->job_poster_questions()->delete();
502
        }
503
504 1
        if (!array_key_exists('question', $input) || !is_array($input['question'])) {
505 1
            return;
506
        }
507
508
        foreach ($input['question'] as $question) {
509
            $jobQuestion = new JobPosterQuestion();
510
            $jobQuestion->job_poster_id = $jobPoster->id;
511
            $jobQuestion->fill(
512
                [
513
                    'en' => [
514
                        'question' => $question['question']['en'],
515
                        'description' => $question['description']['en']
516
                    ],
517
                    'fr' => [
518
                        'question' => $question['question']['fr'],
519
                        'description' => $question['description']['fr']
520
                    ]
521
                ]
522
            );
523
            $jobQuestion->save();
524
        }
525
    }
526
527
    /**
528
     * Fill Job Poster's criteria and save
529
     *
530
     * @param mixed[]               $input     Field values.
531
     * @param \App\Models\JobPoster $jobPoster Job Poster object.
532
     * @param boolean               $replace   Remove existing relationships.
533
     *
534
     * @return void
535
     */
536 1
    protected function fillAndSaveJobPosterCriteria(array $input, JobPoster $jobPoster, bool $replace) : void
537
    {
538 1
        if ($replace) {
539
            $jobPoster->criteria()->delete();
540
        }
541
542 1
        if (!array_key_exists('criteria', $input) || !is_array($input['criteria'])) {
543 1
            return;
544
        }
545
546
        $criteria = $input['criteria'];
547
548
        $combinedCriteria = [];
549
        if (isset($criteria['old'])) {
550
            $combinedCriteria = array_replace_recursive($combinedCriteria, $criteria['old']);
551
        }
552
        if (isset($criteria['new'])) {
553
            $combinedCriteria = array_replace_recursive($combinedCriteria, $criteria['new']);
554
        }
555
556
        if (! empty($combinedCriteria)) {
557
            foreach ($combinedCriteria as $criteriaType => $criteriaTypeInput) {
558
                foreach ($criteriaTypeInput as $skillType => $skillTypeInput) {
559
                    foreach ($skillTypeInput as $criteriaInput) {
560
                        $criteria = new Criteria();
561
                        $criteria->job_poster_id = $jobPoster->id;
562
                        $criteria->fill(
563
                            [
564
                                'criteria_type_id' => CriteriaType::where('name', $criteriaType)->firstOrFail()->id,
565
                                'skill_id' => $criteriaInput['skill_id'],
566
                                'skill_level_id' => $criteriaInput['skill_level_id'],
567
                                'en' => [
568
                                    'description' => $criteriaInput['description']['en'],
569
                                ],
570
                                'fr' => [
571
                                    'description' => $criteriaInput['description']['fr'],
572
                                ],
573
                            ]
574
                        );
575
                        $criteria->save();
576
                    }
577
                }
578
            }
579
        }
580
    }
581
582
    /**
583
     * Get the localized default questions and add them to an array.
584
     *
585
     * @return mixed[]|void
586
     */
587 1
    protected function populateDefaultQuestions()
588
    {
589
        $defaultQuestions = [
590 1
            'en' => array_values(Lang::get('manager/job_create', [], 'en')['questions']),
591 1
            'fr' => array_values(Lang::get('manager/job_create', [], 'fr')['questions']),
592
        ];
593
594 1
        if (count($defaultQuestions['en']) !== count($defaultQuestions['fr'])) {
595
            Log::warning('There must be the same number of French and English default questions for a Job Poster.');
596
            return;
597
        }
598
599 1
        $jobQuestions = [];
600
601 1
        for ($i = 0; $i < count($defaultQuestions['en']); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
602 1
            $jobQuestion = new JobPosterQuestion();
603 1
            $jobQuestion->fill(
604
                [
605
                    'en' => [
606 1
                        'question' => $defaultQuestions['en'][$i],
607
                    ],
608
                    'fr' => [
609 1
                        'question' => $defaultQuestions['fr'][$i],
610
                    ]
611
                ]
612
            );
613
            // Workaround for Default Questions with empty descriptions
614
            // throwing an error during save.
615
            // The id isn't actually used during the fillAndSaveJobPosterQuestions
616
            // method call.
617 1
            $jobQuestion->id = $i + 1;
618 1
            $jobQuestions[] = $jobQuestion;
619
        }
620
621 1
        return $jobQuestions;
622
    }
623
}
624