| Conditions | 8 |
| Paths | 13 |
| Total Lines | 54 |
| Code Lines | 34 |
| 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 handle(): int |
||
| 28 | { |
||
| 29 | $animeCoverPath = storage_path('covers/anime/'); |
||
| 30 | $dryRun = $this->option('dry-run'); |
||
| 31 | |||
| 32 | if (!is_dir($animeCoverPath)) { |
||
| 33 | $this->error("Anime covers directory does not exist: {$animeCoverPath}"); |
||
| 34 | return 1; |
||
| 35 | } |
||
| 36 | |||
| 37 | $this->info("Scanning anime covers directory: {$animeCoverPath}"); |
||
| 38 | |||
| 39 | $files = File::files($animeCoverPath); |
||
| 40 | $renamed = 0; |
||
| 41 | $skipped = 0; |
||
| 42 | |||
| 43 | foreach ($files as $file) { |
||
| 44 | $filename = $file->getFilename(); |
||
| 45 | |||
| 46 | // Check if file matches old format: {id}.jpg (where id is numeric) |
||
| 47 | if (preg_match('/^(\d+)\.jpg$/', $filename, $matches)) { |
||
| 48 | $anidbid = $matches[1]; |
||
| 49 | $newFilename = "{$anidbid}-cover.jpg"; |
||
| 50 | $newPath = $animeCoverPath . $newFilename; |
||
| 51 | |||
| 52 | // Skip if new format already exists |
||
| 53 | if (file_exists($newPath)) { |
||
| 54 | $this->warn("Skipping {$filename} - {$newFilename} already exists"); |
||
| 55 | $skipped++; |
||
| 56 | continue; |
||
| 57 | } |
||
| 58 | |||
| 59 | if ($dryRun) { |
||
| 60 | $this->line("Would rename: {$filename} -> {$newFilename}"); |
||
| 61 | $renamed++; |
||
| 62 | } else { |
||
| 63 | if (rename($file->getPathname(), $newPath)) { |
||
| 64 | $this->info("Renamed: {$filename} -> {$newFilename}"); |
||
| 65 | $renamed++; |
||
| 66 | } else { |
||
| 67 | $this->error("Failed to rename: {$filename}"); |
||
| 68 | } |
||
| 69 | } |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | if ($dryRun) { |
||
| 74 | $this->info("\nDry run complete. Would rename {$renamed} file(s), skipped {$skipped}."); |
||
| 75 | $this->info("Run without --dry-run to perform the migration."); |
||
| 76 | } else { |
||
| 77 | $this->info("\nMigration complete. Renamed {$renamed} file(s), skipped {$skipped}."); |
||
| 78 | } |
||
| 79 | |||
| 80 | return 0; |
||
| 81 | } |
||
| 84 |