| Conditions | 7 |
| Paths | 12 |
| Total Lines | 51 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 52 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 53 | { |
||
| 54 | $kernelContainer = $this->getApplication()?->getKernel()?->getContainer(); |
||
| 55 | if ($kernelContainer) { |
||
| 56 | Container::setContainer($kernelContainer); |
||
| 57 | } |
||
| 58 | |||
| 59 | $io = new SymfonyStyle($input, $output); |
||
| 60 | $olderThan = (int) $input->getOption('older-than'); |
||
| 61 | $dryRun = (bool) $input->getOption('dry-run'); |
||
| 62 | $dir = $input->getOption('dir'); |
||
| 63 | |||
| 64 | if ($olderThan < 0) { |
||
| 65 | $io->error('Option --older-than must be >= 0.'); |
||
| 66 | return Command::INVALID; |
||
| 67 | } |
||
| 68 | |||
| 69 | // Select helper (allow --dir override) |
||
| 70 | $targetHelper = $this->helper; |
||
| 71 | if (!empty($dir)) { |
||
| 72 | // quick override instance (no service registration needed) |
||
| 73 | $targetHelper = new TempUploadHelper($dir); |
||
| 74 | if (!is_dir($targetHelper->getTempDir()) || !is_readable($targetHelper->getTempDir())) { |
||
| 75 | $io->error(sprintf('Directory not readable: %s', $targetHelper->getTempDir())); |
||
| 76 | return Command::FAILURE; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | $tempDir = $targetHelper->getTempDir(); |
||
| 81 | |||
| 82 | // Run purge |
||
| 83 | $stats = $targetHelper->purge($olderThan, $dryRun); |
||
| 84 | |||
| 85 | $mb = $stats['bytes'] / 1048576; |
||
| 86 | if ($dryRun) { |
||
| 87 | $io->note(sprintf( |
||
| 88 | 'DRY RUN: %d files (%.2f MB) would be removed in %s', |
||
| 89 | $stats['files'], |
||
| 90 | $mb, |
||
| 91 | $tempDir |
||
| 92 | )); |
||
| 93 | } else { |
||
| 94 | $io->success(sprintf( |
||
| 95 | 'CLEANED: %d files removed (%.2f MB) in %s', |
||
| 96 | $stats['files'], |
||
| 97 | $mb, |
||
| 98 | $tempDir |
||
| 99 | )); |
||
| 100 | } |
||
| 101 | |||
| 102 | return Command::SUCCESS; |
||
| 103 | } |
||
| 105 |