| Total Complexity | 70 | 
| Total Lines | 346 | 
| 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() | ||
| 47 |     { | ||
| 48 | $notInstalled = []; | ||
| 49 | |||
| 50 | /** @var BasicDriver $driverClass */ | ||
| 51 | $i = 1; | ||
| 52 |         foreach (Formats::$drivers as $driverClass) { | ||
| 53 | $description = $driverClass::getDescription(); | ||
| 54 | $install = $driverClass::getInstallationInstruction(); | ||
| 55 |             if (!empty($install)) { | ||
| 56 | $notInstalled[] = [$driverClass, $description, $install]; | ||
| 57 |             } else { | ||
| 58 | echo ($i++) . '. ' . $driverClass . ' - ' . $description . PHP_EOL; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 |         if (!empty($notInstalled)) { | ||
| 63 | echo PHP_EOL.'Not installed:'.PHP_EOL; | ||
| 64 | $i = 1; | ||
| 65 |             foreach ($notInstalled as $data) { | ||
| 66 | echo ($i++) . '. ' . $data[0] . ' - ' . $data[1] . PHP_EOL | ||
| 67 | . '- ' . $data[2] . PHP_EOL.PHP_EOL; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | /** | ||
| 73 | * @param $args | ||
| 74 | * @throws Exception | ||
| 75 | * @throws \Archive7z\Exception | ||
| 76 | */ | ||
| 77 | public function listArray($args) | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | /** | ||
| 86 | * @param $args | ||
| 87 | * @throws Exception | ||
| 88 | * @throws \Archive7z\Exception | ||
| 89 | */ | ||
| 90 | public function table($args) | ||
| 91 |     { | ||
| 92 | $archive = $this->open($args['ARCHIVE']); | ||
| 93 | |||
| 94 |         echo sprintf('%51s | %4s | %-18s'.PHP_EOL, 'File name', 'Size', 'Date'); | ||
| 95 |         echo str_repeat('-', 80).PHP_EOL; | ||
| 96 |         foreach ($archive->getFileNames() as $file) { | ||
| 97 | $info = $archive->getFileData($file); | ||
| 98 | $size = $this->formatSize($info->uncompressedSize); | ||
| 99 | $file_name = strlen($file) > 51 ? substr($file, 0, 49).'..' : $file; | ||
| 100 |             echo sprintf('%-51s | %1.1f%s | %18s'.PHP_EOL, | ||
| 101 | $file_name, | ||
| 102 | $size[0], | ||
| 103 | $size[1], | ||
| 104 | $this->formatDate($info->modificationTime) | ||
| 105 | ); | ||
| 106 | } | ||
| 107 | $size = $this->formatSize($archive->countUncompressedFilesSize()); | ||
| 108 | $packed_size = $this->formatSize($archive->countCompressedFilesSize()); | ||
| 109 |         echo str_repeat('-', 80).PHP_EOL; | ||
| 110 |         echo sprintf('%51s | %1.1f%s | %1.1f%s'.PHP_EOL, 'Total '.$archive->countFiles().' file(s)', $size[0], $size[1], $packed_size[0], $packed_size[1]); | ||
| 111 | |||
| 112 | } | ||
| 113 | |||
| 114 | /** | ||
| 115 | * @param $bytes | ||
| 116 | * @param int $precision | ||
| 117 | * @return array | ||
| 118 | */ | ||
| 119 | public function formatSize($bytes, $precision = 1) | ||
| 120 |     { | ||
| 121 | $units = ['b', 'k', 'm', 'g', 't']; | ||
| 122 | |||
| 123 | $bytes = max($bytes, 0); | ||
| 124 | $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); | ||
| 125 | $pow = min($pow, count($units) - 1); | ||
| 126 | $bytes /= pow(1024, $pow); | ||
| 127 | $i = round($bytes, $precision); | ||
| 128 |         if ($precision == 1 && $i >= 10) { | ||
| 129 | $i = round($i / 1024, 1); | ||
| 130 | $pow++; | ||
| 131 | } | ||
| 132 | |||
| 133 | return [$i, $units[$pow]]; | ||
| 134 | } | ||
| 135 | |||
| 136 | /** | ||
| 137 | * @param $unixtime | ||
| 138 | * | ||
| 139 | * @return string | ||
| 140 | * @throws \Exception | ||
| 141 | */ | ||
| 142 | public function formatDate($unixtime) | ||
| 143 |     { | ||
| 144 |         if (strtotime('today') < $unixtime) | ||
| 145 |             return 'Today, '.date('G:m', $unixtime); | ||
| 146 |         else if (strtotime('yesterday') < $unixtime) | ||
| 147 |             return 'Yesterday, '.date('G:m', $unixtime); | ||
| 148 |         else { | ||
| 149 | $datetime = new \DateTime(); | ||
| 150 | $datetime->setTimestamp($unixtime); | ||
| 151 |             if ($datetime->format('Y') == date('Y')) | ||
| 152 |                 return $datetime->format('d M, G:m'); | ||
| 153 | else | ||
| 154 |                 return $datetime->format('d M Y, G:m'); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | /** | ||
| 159 | * @param $args | ||
| 160 | * @throws Exception | ||
| 161 | * @throws \Archive7z\Exception | ||
| 162 | */ | ||
| 163 | public function info($args) | ||
| 164 |     { | ||
| 165 | $archive = $this->open($args['ARCHIVE']); | ||
| 166 | echo 'Archive type: '.$archive->getFormat().PHP_EOL; | ||
| 167 | echo 'Archive changed: '.$this->formatDate(filemtime($args['ARCHIVE'])).PHP_EOL; | ||
| 168 | echo 'Archive contains: '.$archive->countFiles().' file'.($archive->countFiles() > 1 ? 's' : null).PHP_EOL; | ||
| 169 |         echo 'Archive   compressed size: '.implode(' ', $this->formatSize($archive->countCompressedFilesSize(), 2)).PHP_EOL; | ||
| 170 |         echo 'Archive uncompressed size: '.implode(' ', $this->formatSize($archive->countUncompressedFilesSize(), 2)).PHP_EOL; | ||
| 171 |         echo 'Archive compression ratio: '.round($archive->countUncompressedFilesSize() / $archive->countCompressedFilesSize(), 6).'/1 ('.floor($archive->countCompressedFilesSize() / $archive->countUncompressedFilesSize() * 100).'%)'.PHP_EOL; | ||
| 172 | } | ||
| 173 | |||
| 174 | /** | ||
| 175 | * @param $args | ||
| 176 | * @throws Exception | ||
| 177 | * @throws \Archive7z\Exception | ||
| 178 | */ | ||
| 179 | public function extract($args) | ||
| 180 |     { | ||
| 181 | $archive = $this->open($args['ARCHIVE'], isset($args['--password']) ? $args['--password'] : null); | ||
| 182 | $output = getcwd(); | ||
| 183 |         if (isset($args['--output'])) { | ||
| 184 | if (!is_dir($args['--output'])) | ||
| 185 | mkdir($args['--output']); | ||
| 186 | $output = realpath($args['--output']); | ||
| 187 | } | ||
| 188 | |||
| 189 |         if (empty($args['FILES_IN_ARCHIVE']) || $args['FILES_IN_ARCHIVE'] == array('/') || $args['FILES_IN_ARCHIVE'] == array('*')) { | ||
| 190 | $result = $archive->extractFiles($output); | ||
| 191 | if ($result === false) echo 'Error occured'.PHP_EOL; | ||
| 192 | else echo 'Extracted '.$result.' file(s) to '.$output.PHP_EOL; | ||
| 193 |         } else { | ||
| 194 | $extracted = 0; | ||
| 195 | $errored = []; | ||
| 196 |             foreach ($args['FILES_IN_ARCHIVE'] as $file) { | ||
| 197 | $result = $archive->extractFiles($output, $file); | ||
| 198 | if ($result === false) $errored[] = $file; | ||
| 199 | else $extracted += $result; | ||
| 200 | } | ||
| 201 |             if (!empty($errored)) echo 'Errored: '.implode(', ', $errored).PHP_EOL; | ||
| 202 | if ($extracted > 0) echo 'Exctracted '.$extracted.' file(s) to '.$output.PHP_EOL; | ||
| 203 | } | ||
| 204 | } | ||
| 205 | |||
| 206 | /** | ||
| 207 | * @param $args | ||
| 208 | * @throws Exception | ||
| 209 | * @throws \Archive7z\Exception | ||
| 210 | */ | ||
| 211 | public function printFile($args) | ||
| 212 |     { | ||
| 213 | $archive = $this->open($args['ARCHIVE'], isset($args['--password']) ? $args['--password'] : null); | ||
| 214 |         foreach ($args['FILES_IN_ARCHIVE'] as $file) { | ||
| 215 |             if (!$archive->hasFile($file)) { | ||
| 216 | echo 'File '.$file.' IS NOT PRESENT'.PHP_EOL; | ||
| 217 | exit(-1); | ||
| 218 | } | ||
| 219 | // $info = $archive->getFileData($file); | ||
| 220 | //            echo 'File content: '.$file.' (size is '.implode('', $this->formatSize($info->uncompressedSize, 1)).')'.PHP_EOL; | ||
| 221 | echo $archive->getFileContent($file); | ||
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | /** | ||
| 226 | * @param $args | ||
| 227 | * @throws Exception | ||
| 228 | * @throws \Archive7z\Exception | ||
| 229 | */ | ||
| 230 | public function details($args) | ||
| 231 |     { | ||
| 232 | $archive = $this->open($args['ARCHIVE']); | ||
| 233 |         foreach ($args['FILES_IN_ARCHIVE'] as $file) { | ||
| 234 | $info = $archive->getFileData($file); | ||
| 235 |             if ($info === false) { | ||
| 236 | echo 'File '.$file.' IS NOT PRESENT'.PHP_EOL; | ||
| 237 | continue; | ||
| 238 | } | ||
| 239 | echo 'File name : '.$file.PHP_EOL; | ||
| 240 |             echo 'Compressed size  : '.implode('', $this->formatSize($info->compressedSize, 2)).PHP_EOL; | ||
| 241 |             echo 'Uncompressed size: '.implode('', $this->formatSize($info->uncompressedSize, 2)).PHP_EOL; | ||
| 242 | echo 'Is compressed : '.($info->isCompressed ? 'yes' : 'no').PHP_EOL; | ||
| 243 | echo 'Date modification: '.$this->formatDate($info->modificationTime).PHP_EOL; | ||
| 244 | } | ||
| 245 | } | ||
| 246 | |||
| 247 | /** | ||
| 248 | * @param $args | ||
| 249 | * @throws Exception | ||
| 250 | * @throws \Archive7z\Exception | ||
| 251 | */ | ||
| 252 | public function delete($args) | ||
| 253 |     { | ||
| 254 | $archive = $this->open($args['ARCHIVE']); | ||
| 255 | $files = $archive->getFileNames(); | ||
| 256 |         foreach ($args['FILES_IN_ARCHIVE'] as $file) { | ||
| 257 |             if (!in_array($file, $files)) { | ||
| 258 | echo 'File '.$file.' is NOT in archive'.PHP_EOL; | ||
| 259 | continue; | ||
| 260 | } | ||
| 261 | if ($archive->deleteFiles($file) === false) | ||
| 262 | echo 'Error file '.$file.PHP_EOL; | ||
| 263 | } | ||
| 264 | } | ||
| 265 | |||
| 266 | /** | ||
| 267 | * @param $args | ||
| 268 | * @throws Exception | ||
| 269 | * @throws \Archive7z\Exception | ||
| 270 | */ | ||
| 271 | public function add($args) | ||
| 279 | } | ||
| 280 | |||
| 281 | /** | ||
| 282 | * @param $args | ||
| 283 | * @throws Exception | ||
| 284 | * @throws \Archive7z\Exception | ||
| 285 | */ | ||
| 286 | public function addFromStdin($args) | ||
| 287 |     { | ||
| 288 | $archive = $this->open($args['ARCHIVE']); | ||
| 289 | $content = null; | ||
| 290 |         while (!feof(STDIN)) { | ||
| 291 | $content .= fgets(STDIN); | ||
| 292 | } | ||
| 293 | $len = strlen($content); | ||
| 294 | |||
| 295 | $added_files = $archive->addFileFromString($args['FILE_IN_ARCHIVE'], $content); | ||
| 296 | if ($added_files === false) | ||
| 297 | echo 'Error'.PHP_EOL; | ||
| 298 |         else { | ||
| 299 | $size = $this->formatSize($len); | ||
| 300 |             echo sprintf('Added %s(%1.1f%s) file to %s', | ||
| 301 | $args['FILE_IN_ARCHIVE'], $size[0], $size[1], $args['ARCHIVE']) . PHP_EOL; | ||
| 302 | } | ||
| 303 | } | ||
| 304 | |||
| 305 | /** | ||
| 306 | * @param $args | ||
| 307 | * @throws Exception | ||
| 308 | */ | ||
| 309 | public function create($args) | ||
| 336 | } | ||
| 337 | } | ||
| 338 | |||
| 339 | public function createFake($args) | ||
| 354 | } | ||
| 355 | } | ||
| 356 |