Conditions | 15 |
Paths | 315 |
Total Lines | 112 |
Code Lines | 72 |
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 |
||
30 | public function up(Schema $schema): void |
||
31 | { |
||
32 | $courseRepo = $this->container->get(CourseRepository::class); |
||
|
|||
33 | $sessionRepo = $this->container->get(SessionRepository::class); |
||
34 | $toolRepo = $this->container->get(CToolRepository::class); |
||
35 | $urlRepo = $this->container->get(AccessUrlRepository::class); |
||
36 | $userRepo = $this->container->get(UserRepository::class); |
||
37 | |||
38 | $batchSize = self::BATCH_SIZE; |
||
39 | $admin = $this->getAdmin(); |
||
40 | $adminId = $admin->getId(); |
||
41 | |||
42 | // Adding courses to the resource node tree. |
||
43 | $urls = $urlRepo->findAll(); |
||
44 | |||
45 | /** @var AccessUrl $url */ |
||
46 | foreach ($urls as $url) { |
||
47 | $counter = 1; |
||
48 | |||
49 | /** @var AccessUrl $urlEntity */ |
||
50 | $urlEntity = $urlRepo->find($url->getId()); |
||
51 | $accessUrlRelCourses = $urlEntity->getCourses(); |
||
52 | |||
53 | /** @var AccessUrlRelCourse $accessUrlRelCourse */ |
||
54 | foreach ($accessUrlRelCourses as $accessUrlRelCourse) { |
||
55 | $course = $accessUrlRelCourse->getCourse(); |
||
56 | $course = $courseRepo->find($course->getId()); |
||
57 | if ($course->hasResourceNode()) { |
||
58 | continue; |
||
59 | } |
||
60 | $urlEntity = $urlRepo->find($url->getId()); |
||
61 | $adminEntity = $userRepo->find($adminId); |
||
62 | $courseRepo->addResourceNode($course, $adminEntity, $urlEntity); |
||
63 | $this->entityManager->persist($course); |
||
64 | |||
65 | // Add groups. |
||
66 | // $course = $course->getGroups(); |
||
67 | if (($counter % $batchSize) === 0) { |
||
68 | $this->entityManager->flush(); |
||
69 | $this->entityManager->clear(); // Detaches all objects from Doctrine! |
||
70 | } |
||
71 | $counter++; |
||
72 | } |
||
73 | } |
||
74 | |||
75 | $this->entityManager->flush(); |
||
76 | $this->entityManager->clear(); |
||
77 | |||
78 | // Special course. |
||
79 | $extraFieldType = ExtraField::COURSE_FIELD_TYPE; |
||
80 | $sql = "SELECT id FROM extra_field |
||
81 | WHERE item_type = $extraFieldType AND variable = 'special_course'"; |
||
82 | $result = $this->connection->executeQuery($sql); |
||
83 | $extraFieldId = $result->fetchOne(); |
||
84 | |||
85 | $specialCourses = []; |
||
86 | if (!empty($extraFieldId)) { |
||
87 | $extraFieldId = (int) $extraFieldId; |
||
88 | $sql = "SELECT DISTINCT(item_id) |
||
89 | FROM extra_field_values |
||
90 | WHERE field_id = $extraFieldId AND field_value= 1 "; |
||
91 | $result = $this->connection->executeQuery($sql); |
||
92 | $specialCourses = $result->fetchAllAssociative(); |
||
93 | if (!empty($specialCourses)) { |
||
94 | $specialCourses = array_map('intval', array_column($specialCourses, 'item_id')); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | // Migrating c_tool. |
||
99 | $q = $this->entityManager->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c'); |
||
100 | |||
101 | /** @var Course $course */ |
||
102 | foreach ($q->toIterable() as $course) { |
||
103 | $counter = 1; |
||
104 | $courseId = $course->getId(); |
||
105 | if (!empty($specialCourses) && \in_array($courseId, $specialCourses, true)) { |
||
106 | $this->addSql("UPDATE course SET sticky = 1 WHERE id = $courseId "); |
||
107 | } |
||
108 | |||
109 | $sql = "SELECT * FROM c_tool |
||
110 | WHERE c_id = {$courseId} "; |
||
111 | $result = $this->connection->executeQuery($sql); |
||
112 | $tools = $result->fetchAllAssociative(); |
||
113 | |||
114 | foreach ($tools as $toolData) { |
||
115 | /** @var CTool $tool */ |
||
116 | $tool = $toolRepo->find($toolData['iid']); |
||
117 | if ($tool->hasResourceNode()) { |
||
118 | continue; |
||
119 | } |
||
120 | |||
121 | $course = $courseRepo->find($courseId); |
||
122 | $session = null; |
||
123 | if (!empty($toolData['session_id'])) { |
||
124 | $session = $sessionRepo->find($toolData['session_id']); |
||
125 | } |
||
126 | |||
127 | $admin = $this->getAdmin(); |
||
128 | $tool->setParent($course); |
||
129 | $toolRepo->addResourceNode($tool, $admin, $course); |
||
130 | $newVisibility = 1 === (int) $toolData['visibility'] ? ResourceLink::VISIBILITY_PUBLISHED : ResourceLink::VISIBILITY_DRAFT; |
||
131 | $tool->addCourseLink($course, $session, null, $newVisibility); |
||
132 | $this->entityManager->persist($tool); |
||
133 | if (($counter % $batchSize) === 0) { |
||
134 | $this->entityManager->flush(); |
||
135 | $this->entityManager->clear(); // Detaches all objects from Doctrine! |
||
136 | } |
||
137 | $counter++; |
||
138 | } |
||
139 | } |
||
140 | $this->entityManager->flush(); |
||
141 | $this->entityManager->clear(); |
||
142 | } |
||
144 |
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.