| Conditions | 9 |
| Paths | 14 |
| Total Lines | 100 |
| Code Lines | 66 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 25 | public function up(Schema $schema): void |
||
| 26 | { |
||
| 27 | $container = $this->getContainer(); |
||
| 28 | $em = $this->getEntityManager(); |
||
| 29 | $connection = $em->getConnection(); |
||
| 30 | |||
| 31 | $introRepo = $container->get(CToolIntroRepository::class); |
||
| 32 | $cToolRepo = $container->get(CToolRepository::class); |
||
| 33 | $toolRepo = $container->get(ToolRepository::class); |
||
| 34 | |||
| 35 | $sessionRepo = $container->get(SessionRepository::class); |
||
| 36 | $courseRepo = $container->get(CourseRepository::class); |
||
| 37 | |||
| 38 | $q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c'); |
||
| 39 | |||
| 40 | /** @var Course $course */ |
||
| 41 | foreach ($q->toIterable() as $course) { |
||
| 42 | $courseId = $course->getId(); |
||
| 43 | $sql = "SELECT * FROM c_tool_intro WHERE c_id = {$courseId} |
||
| 44 | ORDER BY iid"; |
||
| 45 | $result = $connection->executeQuery($sql); |
||
| 46 | $items = $result->fetchAllAssociative(); |
||
| 47 | |||
| 48 | if (empty($items)) { |
||
| 49 | $admin = $this->getAdmin(); |
||
| 50 | $tool = $toolRepo->findOneBy(['name' => 'course_homepage']); |
||
| 51 | $cTool = (new CTool()) |
||
| 52 | ->setName('course_homepage') |
||
| 53 | ->setCourse($course) |
||
| 54 | ->setTool($tool) |
||
| 55 | ->setCreator($admin) |
||
| 56 | ->setParent($course) |
||
| 57 | ->addCourseLink($course) |
||
| 58 | ; |
||
| 59 | $em->persist($cTool); |
||
| 60 | $em->flush(); |
||
| 61 | |||
| 62 | continue; |
||
| 63 | } |
||
| 64 | |||
| 65 | foreach ($items as $itemData) { |
||
| 66 | $id = $itemData['iid']; |
||
| 67 | $sessionId = (int) $itemData['session_id']; |
||
| 68 | $toolName = $itemData['id']; |
||
| 69 | |||
| 70 | /** @var CToolIntro $intro */ |
||
| 71 | $intro = $introRepo->find($id); |
||
| 72 | |||
| 73 | if ($intro->hasResourceNode()) { |
||
| 74 | continue; |
||
| 75 | } |
||
| 76 | |||
| 77 | $session = null; |
||
| 78 | if (!empty($sessionId)) { |
||
| 79 | $session = $sessionRepo->find($sessionId); |
||
| 80 | } |
||
| 81 | |||
| 82 | $admin = $this->getAdmin(); |
||
| 83 | |||
| 84 | $cTool = null; |
||
| 85 | if ('course_homepage' === $toolName) { |
||
| 86 | $tool = $toolRepo->findOneBy(['name' => $toolName]); |
||
| 87 | |||
| 88 | if (null === $tool) { |
||
| 89 | continue; |
||
| 90 | } |
||
| 91 | |||
| 92 | $course = $courseRepo->find($courseId); |
||
| 93 | |||
| 94 | $cTool = (new CTool()) |
||
| 95 | ->setName('course_homepage') |
||
| 96 | ->setCourse($course) |
||
| 97 | ->setSession($session) |
||
| 98 | ->setTool($tool) |
||
| 99 | ->setCreator($admin) |
||
| 100 | ->setParent($course) |
||
| 101 | ->addCourseLink($course, $session) |
||
| 102 | ; |
||
| 103 | $em->persist($cTool); |
||
| 104 | } else { |
||
| 105 | $cTool = $cToolRepo->findCourseResourceByTitle($toolName, $course->getResourceNode(), $course, $session); |
||
| 106 | } |
||
| 107 | |||
| 108 | if (null === $cTool) { |
||
| 109 | continue; |
||
| 110 | } |
||
| 111 | |||
| 112 | $intro |
||
| 113 | ->setParent($course) |
||
| 114 | ->setCourseTool($cTool) |
||
| 115 | ; |
||
| 116 | $introRepo->addResourceNode($intro, $admin, $course); |
||
| 117 | $intro->addCourseLink($course, $session); |
||
| 118 | |||
| 119 | $em->persist($intro); |
||
| 120 | $em->flush(); |
||
| 121 | } |
||
| 122 | |||
| 123 | $em->flush(); |
||
| 124 | $em->clear(); |
||
| 125 | } |
||
| 132 |