SubstitutionFixtures::load()   F
last analyzed

Complexity

Conditions 13
Paths 580

Size

Total Lines 87
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
eloc 54
c 0
b 0
f 0
dl 0
loc 87
rs 3.0333
ccs 0
cts 57
cp 0
cc 13
nc 580
nop 1
crap 182

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
3
namespace App\DataFixtures;
4
5
use App\Entity\Room;
6
use App\Entity\StudyGroup;
7
use App\Entity\Substitution;
8
use App\Entity\Teacher;
9
use Doctrine\Bundle\FixturesBundle\Fixture;
10
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
11
use Doctrine\Persistence\ObjectManager;
12
use Faker\Generator;
13
use SchulIT\CommonBundle\Helper\DateHelper;
14
15
class SubstitutionFixtures extends Fixture implements DependentFixtureInterface {
16
17
    public function __construct(private RoomGenerator $roomGenerator, private Generator $generator, private DateHelper $dateHelper)
18
    {
19
    }
20
21
    public function load(ObjectManager $manager) {
22
        $subjects = [ 'M', 'D', 'E', 'IF', 'MU', 'F0', 'PH' ];
23
        $rooms = $manager->getRepository(Room::class)->findAll();
24
        $types = [ 'Raumvertretung', 'Entfall', 'Vertretung', null ];
25
        $studyGroups = $manager->getRepository(StudyGroup::class)->findAll();
26
        $teachers = $manager->getRepository(Teacher::class)->findAll();
27
        $dates = $this->dateHelper->getListOfNextDays(7);
28
        $id = 1;
29
30
        foreach($studyGroups as $studyGroup) {
31
            if($this->generator->boolean) {
32
                $start = $this->generator->numberBetween(1, 8);
33
                $end = $this->generator->numberBetween($start, 8);
34
35
                $substitution = (new Substitution())
36
                    ->setLessonStart($start)
37
                    ->setLessonEnd($end)
38
                    ->setDate($this->generator->randomElement($dates))
0 ignored issues
show
Bug introduced by
The call to Faker\Generator::randomElement() has too few arguments starting with 'b'. ( Ignorable by Annotation )

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

38
                    ->setDate($this->generator->/** @scrutinizer ignore-call */ randomElement($dates))

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
39
                    ->setSubject($this->generator->randomElement($subjects))
40
                    ->setType($this->generator->randomElement($types))
41
                    ->setExternalId(sprintf('substitution-%d', $id));
42
43
                $substitution->addTeacher($this->generator->randomElement($teachers));
44
45
                if($this->generator->boolean) {
46
                    $teacher = $this->generator->randomElement($teachers);
47
48
                    if(!$substitution->getTeachers()->contains($teacher)) {
49
                        $substitution->addTeacher($teacher);
50
                    }
51
                }
52
53
                if($this->generator->boolean) {
54
                    $substitution->setRoom($this->generator->randomElement($rooms));
0 ignored issues
show
Bug introduced by
The method setRoom() does not exist on App\Entity\Substitution. Did you maybe mean setRoomName()? ( Ignorable by Annotation )

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

54
                    $substitution->/** @scrutinizer ignore-call */ 
55
                                   setRoom($this->generator->randomElement($rooms));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
                }
56
57
                if($this->generator->boolean) {
58
                    $substitution->setReplacementRoom($this->generator->randomElement($rooms));
0 ignored issues
show
Bug introduced by
The method setReplacementRoom() does not exist on App\Entity\Substitution. Did you maybe mean setReplacementRoomName()? ( Ignorable by Annotation )

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

58
                    $substitution->/** @scrutinizer ignore-call */ 
59
                                   setReplacementRoom($this->generator->randomElement($rooms));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
                }
60
61
                if($this->generator->boolean) {
62
                    $substitution->setReplacementSubject($this->generator->randomElement($subjects));
63
                }
64
65
                if($this->generator->boolean(70)) {
66
                    $substitution->addReplacementTeacher($this->generator->randomElement($teachers));
67
                }
68
69
                if($this->generator->boolean) {
70
                    $substitution->setRemark($this->generator->text(100));
71
                }
72
73
                $substitution->addStudyGroup($studyGroup);
74
75
                if($this->generator->boolean) {
76
                    $additionalStudyGroup = $this->generator->randomElement($studyGroups);
77
78
                    if($additionalStudyGroup->getId() !== $studyGroup->getId()) {
79
                        $substitution->addStudyGroup($additionalStudyGroup);
80
                    }
81
                    $substitution->addReplacementStudyGroup($this->generator->randomElement($studyGroups));
82
                }
83
84
                $manager->persist($substitution);
85
            }
86
87
            $id++;
88
        }
89
90
        for($i = 0; $i < 100; $i++) {
91
            $start = $this->generator->numberBetween(1, 8);
92
93
            $substitution = (new Substitution())
94
                ->setStartsBefore(true)
95
                ->setLessonStart($start)
96
                ->setLessonEnd($start)
97
                ->setDate($this->generator->randomElement($dates))
98
                ->setType("Aufsicht")
99
                ->setExternalId(sprintf('substitution-%d', $id));
100
101
            $substitution->addTeacher($this->generator->randomElement($teachers));
102
103
            $manager->persist($substitution);
104
            $id++;
105
        }
106
107
        $manager->flush();
108
    }
109
110
    public function getDependencies() {
111
        return [
112
            StudyGroupFixtures::class,
113
            TeacherFixtures::class,
114
            RoomFixtures::class
115
        ];
116
    }
117
}