| Total Complexity | 82 |
| Total Lines | 381 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 0 |
Complex classes like CamApplication often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CamApplication, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class CamApplication { |
||
| 9 | /** |
||
| 10 | * @param $file |
||
| 11 | * @param null $password |
||
|
|
|||
| 12 | * @return UnifiedArchive |
||
| 13 | * @throws Exceptions\UnsupportedOperationException |
||
| 14 | * @throws Exception |
||
| 15 | */ |
||
| 16 | protected function open($file, $password = null) |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * |
||
| 30 | */ |
||
| 31 | public function checkFormats() |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | public function checkDrivers() |
||
| 68 | } |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param $args |
||
| 74 | * @throws Exception |
||
| 75 | * @throws \Archive7z\Exception |
||
| 76 | */ |
||
| 77 | public function listArray($args) |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param $args |
||
| 88 | * @throws Exception |
||
| 89 | * @throws \Archive7z\Exception |
||
| 90 | */ |
||
| 91 | public function table($args) |
||
| 92 | { |
||
| 93 | $archive = $this->open($args['ARCHIVE']); |
||
| 94 | $filter = isset($args['FILTER']) ? $args['FILTER'] : null; |
||
| 95 | |||
| 96 | $width = $this->getTerminalWidth(); |
||
| 97 | $name_width = $width - 44; |
||
| 98 | |||
| 99 | echo sprintf('%'.$name_width.'s | %8s | %8s | %-18s'.PHP_EOL, 'File name', '#Size', 'Size', 'Date'); |
||
| 100 | echo str_repeat('-', $width).PHP_EOL; |
||
| 101 | foreach ($archive->getFileNames($filter) as $file) { |
||
| 102 | $info = $archive->getFileData($file); |
||
| 103 | $file_name = strlen($file) > $name_width ? substr($file, 0, $name_width-2).'..' : $file; |
||
| 104 | echo sprintf('%-'.$name_width.'s | %8s | %8s | %18s'.PHP_EOL, |
||
| 105 | $file_name, |
||
| 106 | implode(null, $this->formatSize($info->compressedSize, 3)), |
||
| 107 | implode(null, $this->formatSize($info->uncompressedSize, 3)), |
||
| 108 | $this->formatDate($info->modificationTime) |
||
| 109 | ); |
||
| 110 | } |
||
| 111 | $size = $this->formatSize($archive->getOriginalSize()); |
||
| 112 | $packed_size = $this->formatSize($archive->getCompressedSize()); |
||
| 113 | echo str_repeat('-', $width).PHP_EOL; |
||
| 114 | echo sprintf('%'.$name_width.'s | %8s | %8s'.PHP_EOL, 'Total '.$archive->countFiles().' file(s)', $packed_size[0].$packed_size[1], $size[0].$size[1]); |
||
| 115 | |||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param $bytes |
||
| 120 | * @param int $precision |
||
| 121 | * @return array |
||
| 122 | */ |
||
| 123 | public function formatSize($bytes, $precision = 2) |
||
| 124 | { |
||
| 125 | $units = ['b', 'k', 'm', 'g', 't']; |
||
| 126 | |||
| 127 | $bytes = max($bytes, 0); |
||
| 128 | $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); |
||
| 129 | $pow = min($pow, count($units) - 1); |
||
| 130 | $bytes /= pow(1024, $pow); |
||
| 131 | $i = round($bytes, $precision); |
||
| 132 | if ($precision == 1 && $i >= 10) { |
||
| 133 | $i = round($i / 1024, 1); |
||
| 134 | $pow++; |
||
| 135 | } |
||
| 136 | |||
| 137 | return [$i, $units[$pow]]; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param $unixtime |
||
| 142 | * |
||
| 143 | * @return string |
||
| 144 | * @throws \Exception |
||
| 145 | */ |
||
| 146 | public function formatDate($unixtime) |
||
| 147 | { |
||
| 148 | if (strtotime('today') < $unixtime) |
||
| 149 | return 'Today, '.date('G:m', $unixtime); |
||
| 150 | else if (strtotime('yesterday') < $unixtime) |
||
| 151 | return 'Yesterday, '.date('G:m', $unixtime); |
||
| 152 | else { |
||
| 153 | $datetime = new \DateTime(); |
||
| 154 | $datetime->setTimestamp($unixtime); |
||
| 155 | if ($datetime->format('Y') == date('Y')) |
||
| 156 | return $datetime->format('d M, G:m'); |
||
| 157 | else |
||
| 158 | return $datetime->format('d M Y, G:m'); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param $args |
||
| 164 | * @throws Exception |
||
| 165 | * @throws \Archive7z\Exception |
||
| 166 | */ |
||
| 167 | public function info($args) |
||
| 168 | { |
||
| 169 | $archive = $this->open($args['ARCHIVE']); |
||
| 170 | echo 'Archive type: '.$archive->getFormat().PHP_EOL; |
||
| 171 | echo 'Archive changed: '.$this->formatDate(filemtime($args['ARCHIVE'])).PHP_EOL; |
||
| 172 | echo 'Archive contains: '.$archive->countFiles().' file'.($archive->countFiles() > 1 ? 's' : null).PHP_EOL; |
||
| 173 | echo 'Archive compressed size: '.implode(' ', $this->formatSize($archive->getCompressedSize(), 2)).PHP_EOL; |
||
| 174 | echo 'Archive uncompressed size: '.implode(' ', $this->formatSize($archive->getOriginalSize(), 2)).PHP_EOL; |
||
| 175 | echo 'Archive compression ratio: '.round($archive->getOriginalSize() / $archive->getCompressedSize(), 6).'/1 ('.floor($archive->getCompressedSize() / $archive->getOriginalSize() * 100).'%)'.PHP_EOL; |
||
| 176 | if (($comment = $archive->getComment()) !== null) |
||
| 177 | echo 'Archive comment: '.$comment.PHP_EOL; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param $args |
||
| 182 | * @throws Exception |
||
| 183 | * @throws \Archive7z\Exception |
||
| 184 | */ |
||
| 185 | public function extract($args) |
||
| 186 | { |
||
| 187 | $archive = $this->open($args['ARCHIVE'], isset($args['--password']) ? $args['--password'] : null); |
||
| 188 | $output = getcwd(); |
||
| 189 | if (isset($args['--output'])) { |
||
| 190 | if (!is_dir($args['--output'])) |
||
| 191 | mkdir($args['--output']); |
||
| 192 | $output = realpath($args['--output']); |
||
| 193 | } |
||
| 194 | |||
| 195 | if (empty($args['FILES_IN_ARCHIVE']) || $args['FILES_IN_ARCHIVE'] == array('/') || $args['FILES_IN_ARCHIVE'] == array('*')) { |
||
| 196 | $result = $archive->extractFiles($output); |
||
| 197 | if ($result === false) echo 'Error occured'.PHP_EOL; |
||
| 198 | else echo 'Extracted '.$result.' file(s) to '.$output.PHP_EOL; |
||
| 199 | } else { |
||
| 200 | $extracted = 0; |
||
| 201 | $errored = []; |
||
| 202 | foreach ($args['FILES_IN_ARCHIVE'] as $file) { |
||
| 203 | $result = $archive->extractFiles($output, $file); |
||
| 204 | if ($result === false) $errored[] = $file; |
||
| 205 | else $extracted += $result; |
||
| 206 | } |
||
| 207 | if (!empty($errored)) echo 'Errored: '.implode(', ', $errored).PHP_EOL; |
||
| 208 | if ($extracted > 0) echo 'Extracted '.$extracted.' file(s) to '.$output.PHP_EOL; |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param $args |
||
| 214 | * @throws Exception |
||
| 215 | * @throws \Archive7z\Exception |
||
| 216 | */ |
||
| 217 | public function printFile($args) |
||
| 218 | { |
||
| 219 | $archive = $this->open($args['ARCHIVE'], isset($args['--password']) ? $args['--password'] : null); |
||
| 220 | foreach ($args['FILES_IN_ARCHIVE'] as $file) { |
||
| 221 | if (!$archive->hasFile($file)) { |
||
| 222 | echo 'File '.$file.' IS NOT PRESENT'.PHP_EOL; |
||
| 223 | exit(-1); |
||
| 224 | } |
||
| 225 | // $info = $archive->getFileData($file); |
||
| 226 | // echo 'File content: '.$file.' (size is '.implode('', $this->formatSize($info->uncompressedSize, 1)).')'.PHP_EOL; |
||
| 227 | echo $archive->getFileContent($file); |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param $args |
||
| 233 | * @throws Exception |
||
| 234 | * @throws \Archive7z\Exception |
||
| 235 | */ |
||
| 236 | public function details($args) |
||
| 237 | { |
||
| 238 | $archive = $this->open($args['ARCHIVE']); |
||
| 239 | foreach ($args['FILES_IN_ARCHIVE'] as $file) { |
||
| 240 | $info = $archive->getFileData($file); |
||
| 241 | if ($info === false) { |
||
| 242 | echo 'File '.$file.' IS NOT PRESENT'.PHP_EOL; |
||
| 243 | continue; |
||
| 244 | } |
||
| 245 | echo 'File name : '.$file.PHP_EOL; |
||
| 246 | echo 'Compressed size : '.implode('', $this->formatSize($info->compressedSize, 2)).PHP_EOL; |
||
| 247 | echo 'Uncompressed size: '.implode('', $this->formatSize($info->uncompressedSize, 2)).PHP_EOL; |
||
| 248 | echo 'Is compressed : '.($info->isCompressed ? 'yes' : 'no').PHP_EOL; |
||
| 249 | echo 'Date modification: '.$this->formatDate($info->modificationTime).PHP_EOL; |
||
| 250 | $comment = $info->comment; |
||
| 251 | if ($comment !== null) |
||
| 252 | echo 'Comment: '.$comment.PHP_EOL; |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param $args |
||
| 258 | * @throws Exception |
||
| 259 | * @throws \Archive7z\Exception |
||
| 260 | */ |
||
| 261 | public function delete($args) |
||
| 262 | { |
||
| 263 | $archive = $this->open($args['ARCHIVE']); |
||
| 264 | $files = $archive->getFileNames(); |
||
| 265 | foreach ($args['FILES_IN_ARCHIVE'] as $file) { |
||
| 266 | if (!in_array($file, $files)) { |
||
| 267 | echo 'File '.$file.' is NOT in archive'.PHP_EOL; |
||
| 268 | continue; |
||
| 269 | } |
||
| 270 | if ($archive->deleteFiles($file) === false) |
||
| 271 | echo 'Error file '.$file.PHP_EOL; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param $args |
||
| 277 | * @throws Exception |
||
| 278 | * @throws \Archive7z\Exception |
||
| 279 | */ |
||
| 280 | public function add($args) |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param $args |
||
| 292 | * @throws Exception |
||
| 293 | * @throws \Archive7z\Exception |
||
| 294 | */ |
||
| 295 | public function addFromStdin($args) |
||
| 296 | { |
||
| 297 | $archive = $this->open($args['ARCHIVE']); |
||
| 298 | $content = null; |
||
| 299 | while (!feof(STDIN)) { |
||
| 300 | $content .= fgets(STDIN); |
||
| 301 | } |
||
| 302 | $len = strlen($content); |
||
| 303 | |||
| 304 | $added_files = $archive->addFileFromString($args['FILE_IN_ARCHIVE'], $content); |
||
| 305 | if ($added_files === false) |
||
| 306 | echo 'Error'.PHP_EOL; |
||
| 307 | else { |
||
| 308 | $size = $this->formatSize($len); |
||
| 309 | echo sprintf('Added %s(%1.1f%s) file to %s', |
||
| 310 | $args['FILE_IN_ARCHIVE'], $size[0], $size[1], $args['ARCHIVE']) . PHP_EOL; |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param $args |
||
| 316 | * @throws Exception |
||
| 317 | */ |
||
| 318 | public function create($args) |
||
| 351 | } |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | public function createFake($args) |
||
| 370 | } |
||
| 371 | |||
| 372 | protected function getTerminalWidth() |
||
| 373 | { |
||
| 374 | if (is_numeric($columns = trim(getenv('COLUMNS')))) { |
||
| 389 | } |
||
| 390 | } |
||
| 391 |