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