| Conditions | 7 |
| Paths | 17 |
| Total Lines | 83 |
| Code Lines | 47 |
| 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 |
||
| 27 | public function up(Schema $schema): void |
||
| 28 | { |
||
| 29 | /** @var GradebookCertificateRepository $certRepo */ |
||
| 30 | $certRepo = $this->container->get(GradebookCertificateRepository::class); |
||
|
|
|||
| 31 | /** @var PersonalFileRepository $personalRepo */ |
||
| 32 | $personalRepo = $this->container->get(PersonalFileRepository::class); |
||
| 33 | /** @var ResourceNodeRepository $rnRepo */ |
||
| 34 | $rnRepo = $this->container->get(ResourceNodeRepository::class); |
||
| 35 | |||
| 36 | $em = $this->entityManager; |
||
| 37 | |||
| 38 | // missing resource node but having a legacy path. |
||
| 39 | $dql = 'SELECT gc |
||
| 40 | FROM Chamilo\CoreBundle\Entity\GradebookCertificate gc |
||
| 41 | WHERE gc.resourceNode IS NULL |
||
| 42 | AND gc.pathCertificate IS NOT NULL |
||
| 43 | AND gc.pathCertificate <> :empty |
||
| 44 | ORDER BY gc.id ASC'; |
||
| 45 | |||
| 46 | $q = $em->createQuery($dql)->setParameter('empty', ''); |
||
| 47 | |||
| 48 | $migrated = 0; |
||
| 49 | $skipped = 0; |
||
| 50 | $errors = 0; |
||
| 51 | |||
| 52 | foreach ($q->toIterable() as $gc) { |
||
| 53 | \assert($gc instanceof GradebookCertificate); |
||
| 54 | |||
| 55 | $user = $gc->getUser(); |
||
| 56 | $userId = (int) $user->getId(); |
||
| 57 | $catId = $gc->getCategory() ? (int) $gc->getCategory()->getId() : 0; |
||
| 58 | $score = (float) $gc->getScoreCertificate(); |
||
| 59 | $path = (string) ($gc->getPathCertificate() ?? ''); |
||
| 60 | |||
| 61 | // Find the legacy PersonalFile (robust title search + optional creator-scope search). |
||
| 62 | $pf = $this->findLegacyPersonalFile($personalRepo, $gc); |
||
| 63 | if (!$pf) { |
||
| 64 | $this->dbg(sprintf( |
||
| 65 | '[skip] gc#%d user=%d cat=%d -> legacy PersonalFile not found for "%s"', |
||
| 66 | (int) $gc->getId(), $userId, $catId, $path |
||
| 67 | )); |
||
| 68 | $skipped++; |
||
| 69 | continue; |
||
| 70 | } |
||
| 71 | |||
| 72 | // Read the legacy HTML (robust strategy). |
||
| 73 | $html = $this->readLegacyHtml($personalRepo, $rnRepo, $pf); |
||
| 74 | if (!is_string($html) || $html === '') { |
||
| 75 | $this->dbg(sprintf( |
||
| 76 | '[error] gc#%d user=%d cat=%d -> failed to read legacy HTML content from PF (title="%s")', |
||
| 77 | (int) $gc->getId(), $userId, $catId, (string) $pf->getTitle() |
||
| 78 | )); |
||
| 79 | $errors++; |
||
| 80 | continue; |
||
| 81 | } |
||
| 82 | |||
| 83 | try { |
||
| 84 | // Move to Resource (resource type "files") |
||
| 85 | $cert = $certRepo->upsertCertificateResource($catId, $userId, $score, $html, null); |
||
| 86 | |||
| 87 | // Remove legacy PF only after a successful migration |
||
| 88 | $em->remove($pf); |
||
| 89 | $em->flush(); |
||
| 90 | $em->clear(); |
||
| 91 | |||
| 92 | $migrated++; |
||
| 93 | $this->dbg(sprintf( |
||
| 94 | '[ok] gc#%d user=%d cat=%d -> migrated to resource (cert id=%d) and removed PF "%s"', |
||
| 95 | (int) $gc->getId(), $userId, $catId, (int) $cert->getId(), (string) $pf->getTitle() |
||
| 96 | )); |
||
| 97 | } catch (\Throwable $e) { |
||
| 98 | $this->dbg(sprintf( |
||
| 99 | '[error] gc#%d user=%d cat=%d -> upsert failed: %s', |
||
| 100 | (int) $gc->getId(), $userId, $catId, $e->getMessage() |
||
| 101 | )); |
||
| 102 | $errors++; |
||
| 103 | // Do not remove PF on failure |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | $summary = sprintf('Summary: migrated=%d skipped=%d errors=%d', $migrated, $skipped, $errors); |
||
| 108 | $this->write("\n".$summary."\n"); |
||
| 109 | $this->dbg($summary); |
||
| 110 | } |
||
| 272 |
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.