| Conditions | 12 |
| Paths | 34 |
| Total Lines | 109 |
| Code Lines | 69 |
| 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 |
||
| 22 | public function up(Schema $schema): void |
||
| 23 | { |
||
| 24 | $accessUrlRepo = $this->entityManager->getRepository(AccessUrl::class); |
||
|
|
|||
| 25 | $pageCategoryRepo = $this->entityManager->getRepository(PageCategory::class); |
||
| 26 | $pageRepo = $this->entityManager->getRepository(Page::class); |
||
| 27 | $adminUser = $this->getAdmin(); |
||
| 28 | |||
| 29 | // Source directory where Chamilo 1 custom pages are located |
||
| 30 | $sourcePath = $this->getUpdateRootPath().'/app/home'; |
||
| 31 | error_log("[MIGRATION] Looking for registration HTML files in: $sourcePath"); |
||
| 32 | |||
| 33 | // Get or create the "introduction" category |
||
| 34 | $category = $pageCategoryRepo->findOneBy(['title' => 'introduction']); |
||
| 35 | if (!$category) { |
||
| 36 | $category = new PageCategory(); |
||
| 37 | $category |
||
| 38 | ->setTitle('introduction') |
||
| 39 | ->setType('cms') |
||
| 40 | ->setCreator($adminUser); |
||
| 41 | $this->entityManager->persist($category); |
||
| 42 | $this->entityManager->flush(); |
||
| 43 | error_log('[MIGRATION] Created "introduction" category.'); |
||
| 44 | } |
||
| 45 | |||
| 46 | // Loop through directories like /app/home/localhost/ |
||
| 47 | $accessUrls = scandir($sourcePath); |
||
| 48 | foreach ($accessUrls as $dirName) { |
||
| 49 | if (in_array($dirName, ['.', '..'])) { |
||
| 50 | continue; |
||
| 51 | } |
||
| 52 | |||
| 53 | $dirPath = $sourcePath.'/'.$dirName; |
||
| 54 | if (!is_dir($dirPath)) { |
||
| 55 | continue; |
||
| 56 | } |
||
| 57 | |||
| 58 | error_log("[MIGRATION] Checking directory: $dirName"); |
||
| 59 | |||
| 60 | // Look for files like register_top_spanish.html |
||
| 61 | foreach (glob($dirPath.'/register_top_*.html') as $filePath) { |
||
| 62 | $matches = []; |
||
| 63 | if (!preg_match('/register_top_(.+)\.html$/', basename($filePath), $matches)) { |
||
| 64 | error_log("[MIGRATION] File name does not match expected pattern: $filePath"); |
||
| 65 | continue; |
||
| 66 | } |
||
| 67 | |||
| 68 | $locale = $matches[1]; |
||
| 69 | |||
| 70 | // Try to find AccessUrl with both http/https and with/without trailing slash |
||
| 71 | $normalizedUrls = [ |
||
| 72 | 'http://' . $dirName . '/', |
||
| 73 | 'https://' . $dirName . '/', |
||
| 74 | 'http://' . $dirName, |
||
| 75 | 'https://' . $dirName, |
||
| 76 | ]; |
||
| 77 | |||
| 78 | $accessUrl = null; |
||
| 79 | foreach ($normalizedUrls as $url) { |
||
| 80 | $accessUrl = $accessUrlRepo->findOneBy(['url' => $url]); |
||
| 81 | if ($accessUrl) { |
||
| 82 | break; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | if (!$accessUrl) { |
||
| 87 | error_log("[MIGRATION] AccessUrl not found for http(s)://$dirName with or without trailing slash"); |
||
| 88 | continue; |
||
| 89 | } |
||
| 90 | |||
| 91 | // Skip if page already exists |
||
| 92 | $existingPage = $pageRepo->findOneBy([ |
||
| 93 | 'category' => $category, |
||
| 94 | 'url' => $accessUrl, |
||
| 95 | 'locale' => $locale, |
||
| 96 | ]); |
||
| 97 | |||
| 98 | if ($existingPage) { |
||
| 99 | error_log("[MIGRATION] Page already exists for URL=$accessUrl->getUrl(), locale=$locale. Skipped."); |
||
| 100 | continue; |
||
| 101 | } |
||
| 102 | |||
| 103 | // Read file content |
||
| 104 | $content = file_get_contents($filePath); |
||
| 105 | if (empty($content)) { |
||
| 106 | error_log("[MIGRATION] File is empty: $filePath"); |
||
| 107 | continue; |
||
| 108 | } |
||
| 109 | |||
| 110 | // Create new Page entity |
||
| 111 | $page = new Page(); |
||
| 112 | $page |
||
| 113 | ->setTitle('Intro inscription') |
||
| 114 | ->setSlug('intro-inscription') |
||
| 115 | ->setContent($content) |
||
| 116 | ->setLocale($locale) |
||
| 117 | ->setCategory($category) |
||
| 118 | ->setEnabled(true) |
||
| 119 | ->setCreator($adminUser) |
||
| 120 | ->setUrl($accessUrl) |
||
| 121 | ->setPosition(1); |
||
| 122 | |||
| 123 | $this->entityManager->persist($page); |
||
| 124 | |||
| 125 | error_log("[MIGRATION] Page created for: URL={$accessUrl->getUrl()}, locale=$locale"); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | $this->entityManager->flush(); |
||
| 130 | error_log('[MIGRATION] Migration completed successfully.'); |
||
| 131 | } |
||
| 135 |
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.