| Conditions | 8 |
| Paths | 36 |
| Total Lines | 64 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 21 | public function up(Schema $schema): void |
||
| 22 | { |
||
| 23 | $container = $this->getContainer(); |
||
| 24 | $doctrine = $container->get('doctrine'); |
||
| 25 | $em = $doctrine->getManager(); |
||
| 26 | /** @var Connection $connection */ |
||
| 27 | $connection = $em->getConnection(); |
||
| 28 | |||
| 29 | // Install tools. |
||
| 30 | $toolChain = $container->get(ToolChain::class); |
||
| 31 | $toolChain->createTools($em); |
||
| 32 | |||
| 33 | //$urlRepo = $container->get(AccessUrlRepository::class); |
||
| 34 | $urlRepo = $em->getRepository(AccessUrl::class); |
||
| 35 | $userRepo = $container->get(UserRepository::class); |
||
| 36 | |||
| 37 | $userList = []; |
||
| 38 | $admin = $this->getAdmin(); |
||
| 39 | $adminId = $admin->getId(); |
||
| 40 | $userList[$adminId] = $admin; |
||
| 41 | |||
| 42 | if (false === $admin->hasResourceNode()) { |
||
| 43 | $resourceNode = $userRepo->addUserToResourceNode($adminId, $adminId); |
||
| 44 | $em->persist($resourceNode); |
||
| 45 | } |
||
| 46 | |||
| 47 | $urls = $urlRepo->findAll(); |
||
| 48 | /** @var AccessUrl $url */ |
||
| 49 | foreach ($urls as $url) { |
||
| 50 | if (false === $url->hasResourceNode()) { |
||
| 51 | $urlRepo->addResourceNode($url, $admin); |
||
| 52 | $em->persist($url); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | $em->flush(); |
||
| 57 | |||
| 58 | $sql = 'SELECT * FROM user'; |
||
| 59 | $result = $connection->executeQuery($sql); |
||
| 60 | $users = $result->fetchAllAssociative(); |
||
| 61 | |||
| 62 | foreach ($users as $user) { |
||
| 63 | /** @var User $userEntity */ |
||
| 64 | $userEntity = $userRepo->find($user['id']); |
||
| 65 | if ($userEntity->hasResourceNode()) { |
||
| 66 | continue; |
||
| 67 | } |
||
| 68 | $creatorId = $user['creator_id']; |
||
| 69 | $creator = null; |
||
| 70 | if (isset($userList[$adminId])) { |
||
| 71 | $creator = $userList[$adminId]; |
||
| 72 | } else { |
||
| 73 | $creator = $userRepo->find($creatorId); |
||
| 74 | $userList[$adminId] = $creator; |
||
| 75 | } |
||
| 76 | if (null === $creator) { |
||
| 77 | $creator = $admin; |
||
| 78 | } |
||
| 79 | |||
| 80 | $resourceNode = $userRepo->addUserToResourceNode($adminId, $creator->getId()); |
||
| 81 | $em->persist($resourceNode); |
||
| 82 | } |
||
| 83 | |||
| 84 | $em->flush(); |
||
| 85 | } |
||
| 87 |