DepartmentFixture::load()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 53
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 43
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 53
rs 9.232

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\DataFixtures;
4
5
use App\Entity\Department;
6
use Doctrine\Bundle\FixturesBundle\Fixture;
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\Persistence\ObjectManager;
9
10
class DepartmentFixture extends Fixture
11
{
12
    public const DEPARTMENT1_REFERENCE = 'department1';
13
    public const DEPARTMENT2_REFERENCE = 'department2';
14
    public const DEPARTMENT3_REFERENCE = 'department3';
15
    public const DEPARTMENT4_REFERENCE = 'department4';
16
    public const DEPARTMENT5_REFERENCE = 'department5';
17
18
    protected $em;
19
20
    public function __construct(EntityManagerInterface $entityManager)
21
    {
22
        $this->em = $entityManager;
23
    }
24
25
    public function load(ObjectManager $manager)
26
    {
27
        //Reset autoincrement
28
        $this->em->getConnection()
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::exec() has been deprecated: Use {@link executeStatement()} instead. ( Ignorable by Annotation )

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

28
        /** @scrutinizer ignore-deprecated */ $this->em->getConnection()

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
29
            ->exec('ALTER TABLE `departments` AUTO_INCREMENT = 1;');
30
        //ALTER TABLE does an implicit commit and PHP 8 throws if commit is called later internally without active transactions
31
        $this->em->getConnection()->beginTransaction();
32
33
        $department = new Department();
34
        $department->setName('Department 1');
35
        $department->setType('fsr');
36
        $this->addReference(self::DEPARTMENT1_REFERENCE, $department);
37
        $manager->persist($department);
38
39
        $department = new Department();
40
        $department->setName('Department 2');
41
        $department->setType('misc');
42
        $department->setBlocked(true);
43
        $department->setSkipBlockedValidationTokens(['token1', 'token2']);
44
        $department->setContactEmails(['[email protected]', '[email protected]']);
45
        $department->setEmailHhv(['[email protected]']);
46
        $department->setEmailTreasurer(['[email protected]', '[email protected]']);
47
        $this->addReference(self::DEPARTMENT2_REFERENCE, $department);
48
        $manager->persist($department);
49
50
        $department = new Department();
51
        $department->setName('Department 3');
52
        $department->setType('fsr');
53
        $department->setBankAccount($this->getReference(BankAccountFixture::BANK_ACCOUNT1_REFERENCE));
54
        $department->setComment('Test');
55
        $department->setContactEmails(['[email protected]', '[email protected]']);
56
        $department->setEmailHhv(['[email protected]']);
57
        $department->setEmailTreasurer(['[email protected]', '[email protected]']);
58
        $this->addReference(self::DEPARTMENT3_REFERENCE, $department);
59
        $manager->persist($department);
60
61
        $department = new Department();
62
        $department->setName('Department 4');
63
        $department->setType('fsr');
64
        $department->setBankAccount($this->getReference(BankAccountFixture::BANK_ACCOUNT2_REFERENCE));
65
        $department->setContactEmails(['[email protected]']);
66
        $this->addReference(self::DEPARTMENT4_REFERENCE, $department);
67
        $manager->persist($department);
68
69
        $department = new Department();
70
        $department->setName('Department 5');
71
        $department->setType('section');
72
        $department->setBankAccount($this->getReference(BankAccountFixture::BANK_ACCOUNT3_REFERENCE));
73
        $department->setEmailHhv(['[email protected]']);
74
        $this->addReference(self::DEPARTMENT5_REFERENCE, $department);
75
        $manager->persist($department);
76
77
        $manager->flush();
78
    }
79
}
80