Conditions | 13 |
Paths | 816 |
Total Lines | 96 |
Code Lines | 60 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
24 | public function up(Schema $schema): void |
||
25 | { |
||
26 | $urlRepo = $this->container->get(AccessUrlRepository::class); |
||
|
|||
27 | $userRepo = $this->container->get(UserRepository::class); |
||
28 | |||
29 | $userList = []; |
||
30 | // Adding first admin as main creator also adding to the resource node tree. |
||
31 | $admin = $this->getAdmin(); |
||
32 | $admin->addRole('ROLE_ADMIN'); |
||
33 | |||
34 | $adminId = $admin->getId(); |
||
35 | $userList[$adminId] = $admin; |
||
36 | |||
37 | $this->write('Adding admin user'); |
||
38 | if (false === $admin->hasResourceNode()) { |
||
39 | $resourceNode = $userRepo->addUserToResourceNode($adminId, $adminId); |
||
40 | $this->entityManager->persist($resourceNode); |
||
41 | } |
||
42 | |||
43 | // Adding portals (AccessUrl) to the resource node tree. |
||
44 | $urls = $urlRepo->findAll(); |
||
45 | |||
46 | /** @var AccessUrl $url */ |
||
47 | foreach ($urls as $url) { |
||
48 | if (false === $url->hasResourceNode()) { |
||
49 | $urlRepo->createNodeForResourceWithNoParent($url, $admin); |
||
50 | $this->entityManager->persist($url); |
||
51 | } |
||
52 | } |
||
53 | $this->entityManager->flush(); |
||
54 | |||
55 | $sql = 'SELECT DISTINCT(user_id) FROM admin'; |
||
56 | $result = $this->entityManager->getConnection()->executeQuery($sql); |
||
57 | $results = $result->fetchAllAssociative(); |
||
58 | $adminList = []; |
||
59 | if (!empty($results)) { |
||
60 | $adminList = array_map('intval', array_column($results, 'user_id')); |
||
61 | } |
||
62 | |||
63 | // Adding users to the resource node tree. |
||
64 | $batchSize = self::BATCH_SIZE; |
||
65 | $counter = 1; |
||
66 | $q = $this->entityManager->createQuery('SELECT u FROM Chamilo\CoreBundle\Entity\User u'); |
||
67 | |||
68 | $this->write('Migrating users'); |
||
69 | |||
70 | /** @var User $userEntity */ |
||
71 | foreach ($q->toIterable() as $userEntity) { |
||
72 | if ($userEntity->hasResourceNode()) { |
||
73 | continue; |
||
74 | } |
||
75 | |||
76 | $userId = $userEntity->getId(); |
||
77 | $this->write("Migrating user: #$userId"); |
||
78 | |||
79 | $userEntity |
||
80 | ->setUuid(Uuid::v4()) |
||
81 | ->setRoles([]) |
||
82 | ->setRoleFromStatus($userEntity->getStatus()) |
||
83 | ; |
||
84 | |||
85 | if (\in_array($userId, $adminList, true)) { |
||
86 | $userEntity->addRole('ROLE_ADMIN'); |
||
87 | } |
||
88 | |||
89 | if ($userEntity::ANONYMOUS === $userEntity->getStatus()) { |
||
90 | $userEntity->addRole('ROLE_ANONYMOUS'); |
||
91 | } |
||
92 | |||
93 | $creatorId = $userEntity->getCreatorId(); |
||
94 | $creator = null; |
||
95 | if (isset($userList[$adminId])) { |
||
96 | $creator = $userList[$adminId]; |
||
97 | } else { |
||
98 | $creator = $userRepo->find($creatorId); |
||
99 | $userList[$adminId] = $creator; |
||
100 | } |
||
101 | if (null === $creator) { |
||
102 | $creator = $admin; |
||
103 | } |
||
104 | |||
105 | $resourceNode = $userRepo->addUserToResourceNode($userId, $creator->getId()); |
||
106 | $this->entityManager->persist($resourceNode); |
||
107 | |||
108 | if (($counter % $batchSize) === 0) { |
||
109 | $this->entityManager->flush(); |
||
110 | $this->entityManager->clear(); // Detaches all objects from Doctrine! |
||
111 | } |
||
112 | $counter++; |
||
113 | } |
||
114 | $this->entityManager->flush(); |
||
115 | $this->entityManager->clear(); |
||
116 | |||
117 | $table = $schema->getTable('user'); |
||
118 | if (false === $table->hasIndex('UNIQ_8D93D649D17F50A6')) { |
||
119 | $this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D649D17F50A6 ON user (uuid);'); |
||
120 | } |
||
123 |
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.