| Conditions | 19 |
| Paths | 58 |
| Total Lines | 112 |
| Code Lines | 82 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 42 | public function execute(InputInterface $input, OutputInterface $output) |
||
| 43 | { |
||
| 44 | $archive_file = $input->getArgument('archive'); |
||
| 45 | $files_to_pack = $input->getArgument('file'); |
||
| 46 | |||
| 47 | $password = $input->getOption('password'); |
||
| 48 | $compression = $input->getOption('compression'); |
||
| 49 | if (!isset(static::$compressionLevels[$compression])) { |
||
| 50 | throw new \InvalidArgumentException('Compression "' . $compression . '" is not valid'); |
||
| 51 | } |
||
| 52 | $compression = static::$compressionLevels[$compression]; |
||
| 53 | $path = $input->getOption('path'); |
||
| 54 | if (!in_array($path, ['full', 'root', 'relative', 'basename'], true)) { |
||
| 55 | throw new \InvalidArgumentException('Path can not have this value'); |
||
| 56 | } |
||
| 57 | $dry_run = $input->getOption('dry-run'); |
||
| 58 | $comment = $input->getOption('comment'); |
||
| 59 | |||
| 60 | if (file_exists($archive_file)) { |
||
|
|
|||
| 61 | if (is_dir($archive_file)) |
||
| 62 | throw new \InvalidArgumentException($archive_file . ' is a directory!'); |
||
| 63 | else { |
||
| 64 | throw new \InvalidArgumentException('File "' . $archive_file . '" already exists!'); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | $files_list = []; |
||
| 69 | |||
| 70 | foreach ($files_to_pack as $i => $file_to_pack) { |
||
| 71 | $file_to_pack = realpath($file_to_pack); |
||
| 72 | switch ($path) { |
||
| 73 | case 'full': |
||
| 74 | $destination = ltrim($file_to_pack, '/'); |
||
| 75 | $files_list[$destination] = $file_to_pack; |
||
| 76 | $output->writeln('<comment>' . $file_to_pack . ' => ' . $destination . '</comment>', OutputInterface::VERBOSITY_VERBOSE); |
||
| 77 | break; |
||
| 78 | case 'root': |
||
| 79 | if (is_dir($file_to_pack)) { |
||
| 80 | $output->writeln('<comment>' . $file_to_pack . ' => root</comment>', OutputInterface::VERBOSITY_VERBOSE); |
||
| 81 | if (!isset($files_list[''])) { |
||
| 82 | $files_list[''] = $file_to_pack; |
||
| 83 | } elseif (is_string($files_list[''])) { |
||
| 84 | $files_list[''] = [$files_list[''], $file_to_pack]; |
||
| 85 | } else { |
||
| 86 | $files_list[''][] = $file_to_pack; |
||
| 87 | } |
||
| 88 | } else { |
||
| 89 | $output->writeln('<comment>' . $file_to_pack . ' => ' . basename($file_to_pack) . '</comment>', OutputInterface::VERBOSITY_VERBOSE); |
||
| 90 | $files_list[basename($file_to_pack)] = $file_to_pack; |
||
| 91 | } |
||
| 92 | break; |
||
| 93 | case 'relative': |
||
| 94 | $destination = ltrim($file_to_pack, '/.'); |
||
| 95 | $files_list[$destination] = $file_to_pack; |
||
| 96 | $output->writeln('<comment>' . $file_to_pack . ' => ' . $destination . '</comment>', OutputInterface::VERBOSITY_VERBOSE); |
||
| 97 | break; |
||
| 98 | case 'basename': |
||
| 99 | $destination = basename($file_to_pack); |
||
| 100 | $files_list[$destination] = $file_to_pack; |
||
| 101 | $output->writeln('<comment>' . $file_to_pack . ' => ' . $destination . '</comment>', OutputInterface::VERBOSITY_VERBOSE); |
||
| 102 | break; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | $information = UnifiedArchive::prepareForArchiving($files_list, $archive_file); |
||
| 107 | if ($dry_run) { |
||
| 108 | $output->writeln('Format: <info>' . $information['type'] . '</info>'); |
||
| 109 | $output->writeln('Original size: <info>' . implode($this->formatSize($information['totalSize'])) . '</info>'); |
||
| 110 | $output->writeln('Files: <info>' . $information['numberOfFiles'] . '</info>'); |
||
| 111 | foreach ($information['files'] as $destination => $source) { |
||
| 112 | // is folder |
||
| 113 | if ($source === null) { |
||
| 114 | continue; |
||
| 115 | } |
||
| 116 | |||
| 117 | $output->writeln($source . ' => <comment>' . $destination . '</comment>'); |
||
| 118 | } |
||
| 119 | return 0; |
||
| 120 | } |
||
| 121 | |||
| 122 | ProgressBar::setFormatDefinition('archiving', ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%: %message%'); |
||
| 123 | |||
| 124 | $progressBar = new ProgressBar($output, $information['numberOfFiles']); |
||
| 125 | $progressBar->setFormat('archiving'); |
||
| 126 | $progressBar->start(); |
||
| 127 | $archived_files = UnifiedArchive::archiveFiles($files_list, $archive_file, $compression, $password, function ($currentFile, $totalFiles, $fsFilename, $archiveFilename) |
||
| 128 | use ($progressBar) { |
||
| 129 | if ($fsFilename === null) { |
||
| 130 | $progressBar->setMessage('Creating ' . $archiveFilename); |
||
| 131 | } else { |
||
| 132 | $progressBar->setMessage($fsFilename); |
||
| 133 | } |
||
| 134 | $progressBar->advance(); |
||
| 135 | }); |
||
| 136 | $progressBar->finish(); |
||
| 137 | $progressBar->clear(); |
||
| 138 | $output->writeln(''); |
||
| 139 | |||
| 140 | if (!$archived_files) { |
||
| 141 | throw new \RuntimeException('archiveFiles result is false'); |
||
| 142 | } |
||
| 143 | |||
| 144 | $archive = $this->open($archive_file); |
||
| 145 | if (!empty($comment)) { |
||
| 146 | $archive->setComment($comment); |
||
| 147 | } |
||
| 148 | |||
| 149 | $output->writeln('Created <info>' . $archive_file . '</info> with <comment>' . $archived_files . '</comment> file(s) (' |
||
| 150 | . implode($this->formatSize($archive->getOriginalSize())) . ') of total size ' |
||
| 151 | . implode($this->formatSize(filesize($archive_file)))); |
||
| 152 | |||
| 153 | return 0; |
||
| 154 | } |
||
| 156 |