| Conditions | 8 |
| Paths | 17 |
| Total Lines | 68 |
| Code Lines | 38 |
| 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 |
||
| 25 | public function up(Schema $schema): void |
||
| 26 | { |
||
| 27 | /** @var ToolChain $toolChain */ |
||
| 28 | $toolChain = $this->container->get(ToolChain::class); |
||
|
|
|||
| 29 | /** @var SettingsManager $settings */ |
||
| 30 | $settings = $this->container->get(SettingsManager::class); |
||
| 31 | |||
| 32 | // Ensure global catalog is seeded (tool + resource_types) |
||
| 33 | $toolChain->createTools(); |
||
| 34 | |||
| 35 | $targetTitle = 'dropbox'; |
||
| 36 | |||
| 37 | $toolRepo = $this->entityManager->getRepository(Tool::class); |
||
| 38 | $courseRepo = $this->entityManager->getRepository(Course::class); |
||
| 39 | |||
| 40 | /** @var Tool|null $tool */ |
||
| 41 | $tool = $toolRepo->findOneBy(['title' => $targetTitle]); |
||
| 42 | if (!$tool) { |
||
| 43 | $this->write('Tool "'.$targetTitle.'" does not exist; aborting migration.'); |
||
| 44 | return; |
||
| 45 | } |
||
| 46 | |||
| 47 | $activeOnCreate = $settings->getSetting('course.active_tools_on_create') ?? []; |
||
| 48 | $batchSize = self::BATCH_SIZE; |
||
| 49 | $i = 0; |
||
| 50 | |||
| 51 | // Iterate over all courses using a memory-efficient cursor |
||
| 52 | $q = $this->entityManager->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c'); |
||
| 53 | |||
| 54 | /** @var Course $course */ |
||
| 55 | foreach ($q->toIterable() as $course) { |
||
| 56 | // Skip if the course already has a CTool with the same title |
||
| 57 | $already = false; |
||
| 58 | foreach ($course->getTools() as $ct) { |
||
| 59 | if (0 === strcasecmp($ct->getTitle(), $targetTitle)) { |
||
| 60 | $already = true; |
||
| 61 | break; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | if ($already) { |
||
| 65 | continue; |
||
| 66 | } |
||
| 67 | |||
| 68 | // Initial visibility and link visibility (same rule as addToolsInCourse) |
||
| 69 | $visible = \in_array($targetTitle, $activeOnCreate, true); |
||
| 70 | $linkVisibility = $visible ? ResourceLink::VISIBILITY_PUBLISHED : ResourceLink::VISIBILITY_DRAFT; |
||
| 71 | |||
| 72 | // Create CTool and its ResourceLink |
||
| 73 | $ctool = (new CTool()) |
||
| 74 | ->setTool($tool) |
||
| 75 | ->setTitle($targetTitle) |
||
| 76 | ->setVisibility($visible) |
||
| 77 | ->setParent($course) |
||
| 78 | ->setCreator($course->getCreator()) |
||
| 79 | ->addCourseLink($course, null, null, $linkVisibility); |
||
| 80 | |||
| 81 | $course->addTool($ctool); |
||
| 82 | $this->entityManager->persist($ctool); |
||
| 83 | |||
| 84 | // Batch flush |
||
| 85 | if ((++$i % $batchSize) === 0) { |
||
| 86 | $this->entityManager->flush(); |
||
| 87 | $this->entityManager->clear(); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | $this->entityManager->flush(); |
||
| 92 | $this->entityManager->clear(); |
||
| 93 | } |
||
| 97 |
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.