| Conditions | 10 |
| Paths | 25 |
| Total Lines | 121 |
| Code Lines | 78 |
| 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 |
||
| 30 | public function up(Schema $schema): void |
||
| 31 | { |
||
| 32 | $container = $this->getContainer(); |
||
| 33 | $doctrine = $container->get('doctrine'); |
||
| 34 | $em = $doctrine->getManager(); |
||
| 35 | /** @var Connection $connection */ |
||
| 36 | $connection = $em->getConnection(); |
||
| 37 | |||
| 38 | $lpCategoryRepo = $container->get(CLpCategoryRepository::class); |
||
| 39 | $lpRepo = $container->get(CLpRepository::class); |
||
| 40 | |||
| 41 | $courseRepo = $container->get(CourseRepository::class); |
||
| 42 | $sessionRepo = $container->get(SessionRepository::class); |
||
| 43 | $groupRepo = $container->get(CGroupRepository::class); |
||
| 44 | $userRepo = $container->get(UserRepository::class); |
||
| 45 | |||
| 46 | $lpItemRepo = $container->get(CLpItem::class); |
||
| 47 | |||
| 48 | $admin = $this->getAdmin(); |
||
| 49 | |||
| 50 | $q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c'); |
||
| 51 | /** @var Course $course */ |
||
| 52 | foreach ($q->toIterable() as $course) { |
||
| 53 | $courseId = $course->getId(); |
||
| 54 | $course = $courseRepo->find($courseId); |
||
| 55 | |||
| 56 | // c_lp_category. |
||
| 57 | $sql = "SELECT * FROM c_lp_category WHERE c_id = {$courseId} |
||
| 58 | ORDER BY iid"; |
||
| 59 | $result = $connection->executeQuery($sql); |
||
| 60 | $items = $result->fetchAllAssociative(); |
||
| 61 | foreach ($items as $itemData) { |
||
| 62 | $id = $itemData['iid']; |
||
| 63 | /** @var CLpCategory $resource */ |
||
| 64 | $resource = $lpCategoryRepo->find($id); |
||
| 65 | if ($resource->hasResourceNode()) { |
||
| 66 | continue; |
||
| 67 | } |
||
| 68 | |||
| 69 | $result = $this->fixItemProperty( |
||
| 70 | 'learnpath_category', |
||
| 71 | $lpCategoryRepo, |
||
| 72 | $course, |
||
| 73 | $admin, |
||
| 74 | $resource, |
||
| 75 | $course |
||
| 76 | ); |
||
| 77 | |||
| 78 | if (false === $result) { |
||
| 79 | continue; |
||
| 80 | } |
||
| 81 | |||
| 82 | $em->persist($resource); |
||
| 83 | $em->flush(); |
||
| 84 | } |
||
| 85 | |||
| 86 | $em->flush(); |
||
| 87 | $em->clear(); |
||
| 88 | |||
| 89 | // c_lp. |
||
| 90 | $sql = "SELECT * FROM c_lp WHERE c_id = {$courseId} |
||
| 91 | ORDER BY iid"; |
||
| 92 | $result = $connection->executeQuery($sql); |
||
| 93 | $lps = $result->fetchAllAssociative(); |
||
| 94 | foreach ($lps as $lp) { |
||
| 95 | $lpId = $lp['iid']; |
||
| 96 | |||
| 97 | /** @var CLp $resource */ |
||
| 98 | $resource = $lpRepo->find($lpId); |
||
| 99 | if ($resource->hasResourceNode()) { |
||
| 100 | continue; |
||
| 101 | } |
||
| 102 | |||
| 103 | $course = $courseRepo->find($courseId); |
||
| 104 | |||
| 105 | $result = $this->fixItemProperty( |
||
| 106 | 'learnpath', |
||
| 107 | $lpRepo, |
||
| 108 | $course, |
||
| 109 | $admin, |
||
| 110 | $resource, |
||
| 111 | $course |
||
| 112 | ); |
||
| 113 | |||
| 114 | if (false === $result) { |
||
| 115 | continue; |
||
| 116 | } |
||
| 117 | |||
| 118 | $em->persist($resource); |
||
| 119 | $em->flush(); |
||
| 120 | |||
| 121 | $itemRoot = $lpItemRepo->getItemRoot($lpId); |
||
| 122 | |||
| 123 | if (!empty($itemRoot)) { |
||
| 124 | continue; |
||
| 125 | } |
||
| 126 | |||
| 127 | $lpItem = new CLpItem(); |
||
| 128 | $lpItem |
||
| 129 | ->setTitle('root') |
||
| 130 | ->setPath('root') |
||
| 131 | ->setLp($resource) |
||
| 132 | ->setItemType('root'); |
||
| 133 | $em->persist($lpItem); |
||
| 134 | $em->flush(); |
||
| 135 | |||
| 136 | // Migrate c_lp_item |
||
| 137 | $sql = "SELECT * FROM c_lp_item WHERE lp_id = $lpId |
||
| 138 | ORDER BY display_order"; |
||
| 139 | |||
| 140 | $resultItems = $connection->executeQuery($sql); |
||
| 141 | $lpItems = $resultItems->fetchAllAssociative(); |
||
| 142 | $orderList = []; |
||
| 143 | foreach ($lpItems as $item) { |
||
| 144 | $object = new \stdClass(); |
||
| 145 | $object->id = $item['iid']; |
||
| 146 | $object->parent_id = $item['parentId']; |
||
| 147 | $orderList[] = $object; |
||
| 148 | } |
||
| 149 | |||
| 150 | \learnpath::sortItemByOrderList($lpId, $orderList); |
||
| 151 | } |
||
| 155 |