| Conditions | 6 |
| Paths | 9 |
| Total Lines | 75 |
| Code Lines | 47 |
| 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 |
||
| 26 | public function up(Schema $schema): void |
||
| 27 | { |
||
| 28 | /** @var ToolChain $toolChain */ |
||
| 29 | $toolChain = $this->container->get(ToolChain::class); |
||
|
|
|||
| 30 | /** @var SettingsManager $settings */ |
||
| 31 | $settings = $this->container->get(SettingsManager::class); |
||
| 32 | |||
| 33 | // Ensure global catalog is seeded (tool + resource_types) |
||
| 34 | $toolChain->createTools(); |
||
| 35 | |||
| 36 | $targetTitle = 'blog'; |
||
| 37 | |||
| 38 | // Get Tool ID by title (fail fast if not found) |
||
| 39 | $toolId = $this->entityManager |
||
| 40 | ->createQuery('SELECT t.id FROM Chamilo\CoreBundle\Entity\Tool t WHERE t.title = :title') |
||
| 41 | ->setParameter('title', $targetTitle) |
||
| 42 | ->getOneOrNullResult(Query::HYDRATE_SINGLE_SCALAR); |
||
| 43 | |||
| 44 | if ($toolId === null) { |
||
| 45 | $this->write('Tool "'.$targetTitle.'" does not exist; aborting migration.'); |
||
| 46 | return; |
||
| 47 | } |
||
| 48 | $toolId = (int) $toolId; |
||
| 49 | |||
| 50 | $activeOnCreate = $settings->getSetting('course.active_tools_on_create') ?? []; |
||
| 51 | $visible = \in_array($targetTitle, $activeOnCreate, true); |
||
| 52 | $linkVisibility = $visible ? ResourceLink::VISIBILITY_PUBLISHED : ResourceLink::VISIBILITY_DRAFT; |
||
| 53 | |||
| 54 | $batchSize = self::BATCH_SIZE; |
||
| 55 | $i = 0; |
||
| 56 | |||
| 57 | // Iterate over course IDs only (no heavy hydration) |
||
| 58 | $q = $this->entityManager->createQuery('SELECT c.id FROM Chamilo\CoreBundle\Entity\Course c'); |
||
| 59 | |||
| 60 | foreach ($q->toIterable([], Query::HYDRATE_SCALAR) as $row) { |
||
| 61 | $courseId = (int) $row['id']; |
||
| 62 | |||
| 63 | // Does the course already have this tool? |
||
| 64 | $exists = (int) $this->entityManager |
||
| 65 | ->createQuery( |
||
| 66 | 'SELECT COUNT(ct.iid) |
||
| 67 | FROM Chamilo\CourseBundle\Entity\CTool ct |
||
| 68 | WHERE ct.title = :title |
||
| 69 | AND ct.course = :course' |
||
| 70 | ) |
||
| 71 | ->setParameter('title', $targetTitle) |
||
| 72 | ->setParameter('course', $this->entityManager->getReference(Course::class, $courseId)) |
||
| 73 | ->getSingleScalarResult(); |
||
| 74 | |||
| 75 | if ($exists > 0) { |
||
| 76 | continue; |
||
| 77 | } |
||
| 78 | |||
| 79 | $toolRef = $this->entityManager->getReference(Tool::class, $toolId); |
||
| 80 | $courseRef = $this->entityManager->getReference(Course::class, $courseId); |
||
| 81 | |||
| 82 | $ctool = (new CTool()) |
||
| 83 | ->setTool($toolRef) |
||
| 84 | ->setTitle($targetTitle) |
||
| 85 | ->setVisibility($visible) |
||
| 86 | ->setCourse($courseRef) |
||
| 87 | ->setParent($courseRef) |
||
| 88 | ->setCreator($courseRef->getCreator()) |
||
| 89 | ->addCourseLink($courseRef, null, null, $linkVisibility); |
||
| 90 | |||
| 91 | $this->entityManager->persist($ctool); |
||
| 92 | |||
| 93 | if ((++$i % $batchSize) === 0) { |
||
| 94 | $this->entityManager->flush(); |
||
| 95 | $this->entityManager->clear(); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | $this->entityManager->flush(); |
||
| 100 | $this->entityManager->clear(); |
||
| 101 | } |
||
| 105 |
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.