| Total Complexity | 61 |
| Total Lines | 338 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like TarByPear 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 TarByPear, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class TarByPear extends BasicDriver |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var string Full path to archive |
||
| 19 | */ |
||
| 20 | protected $archiveFileName; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var Archive_Tar |
||
| 24 | */ |
||
| 25 | protected $tar; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var float Overall compression ratio of Tar archive when Archive_Tar is used |
||
| 29 | */ |
||
| 30 | protected $pearCompressionRatio; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array<string, integer> List of files and their index in listContent() result |
||
| 34 | */ |
||
| 35 | protected $pearFilesIndex; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @return array |
||
| 39 | */ |
||
| 40 | public static function getSupportedFormats() |
||
| 41 | { |
||
| 42 | return [ |
||
| 43 | Formats::TAR, |
||
| 44 | Formats::TAR_GZIP, |
||
| 45 | Formats::TAR_BZIP, |
||
| 46 | Formats::TAR_LZMA, |
||
| 47 | Formats::TAR_LZW, |
||
| 48 | ]; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param $format |
||
| 53 | * @return bool |
||
| 54 | * @throws \Exception |
||
| 55 | */ |
||
| 56 | public static function checkFormatSupport($format) |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @inheritDoc |
||
| 79 | */ |
||
| 80 | public static function getDescription() |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @inheritDoc |
||
| 87 | */ |
||
| 88 | public static function getInstallationInstruction() |
||
| 89 | { |
||
| 90 | return 'install library `pear/archive_tar` and optionally php-extensions (zlib, bzip2)'; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param array $files |
||
| 95 | * @param string $archiveFileName |
||
| 96 | * @param int $compressionLevel |
||
| 97 | * @param null $password |
||
| 98 | * @return int |
||
| 99 | * @throws ArchiveCreationException |
||
| 100 | * @throws UnsupportedOperationException |
||
| 101 | */ |
||
| 102 | public static function createArchive(array $files, $archiveFileName, $compressionLevel = self::COMPRESSION_AVERAGE, $password = null) |
||
| 103 | { |
||
| 104 | if ($password !== null) { |
||
| 105 | throw new UnsupportedOperationException('One-file format ('.__CLASS__.') could not encrypt an archive'); |
||
| 106 | } |
||
| 107 | |||
| 108 | $compression = null; |
||
| 109 | switch (strtolower(pathinfo($archiveFileName, PATHINFO_EXTENSION))) { |
||
| 110 | case 'gz': |
||
| 111 | case 'tgz': |
||
| 112 | $compression = 'gz'; |
||
| 113 | break; |
||
| 114 | case 'bz2': |
||
| 115 | case 'tbz2': |
||
| 116 | $compression = 'bz2'; |
||
| 117 | break; |
||
| 118 | case 'xz': |
||
| 119 | $compression = 'lzma2'; |
||
| 120 | break; |
||
| 121 | case 'z': |
||
| 122 | $tar_aname = 'compress.lzw://' . $archiveFileName; |
||
| 123 | break; |
||
| 124 | } |
||
| 125 | |||
| 126 | if (isset($tar_aname)) |
||
| 127 | $tar = new Archive_Tar($tar_aname, $compression); |
||
| 128 | else |
||
| 129 | $tar = new Archive_Tar($archiveFileName, $compression); |
||
| 130 | |||
| 131 | foreach ($files as $localName => $filename) { |
||
| 132 | $remove_dir = dirname($filename); |
||
| 133 | $add_dir = dirname($localName); |
||
| 134 | |||
| 135 | if (is_null($filename)) { |
||
| 136 | if ($tar->addString($localName, '') === false) |
||
| 137 | throw new ArchiveCreationException('Error when adding directory '.$localName.' to archive'); |
||
| 138 | } else { |
||
| 139 | if ($tar->addModify($filename, $add_dir, $remove_dir) === false) |
||
| 140 | throw new ArchiveCreationException('Error when adding file '.$filename.' to archive'); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | $tar = null; |
||
| 144 | |||
| 145 | return count($files); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @inheritDoc |
||
| 150 | */ |
||
| 151 | public function __construct($archiveFileName, $format, $password = null) |
||
| 152 | { |
||
| 153 | $this->archiveFileName = realpath($archiveFileName); |
||
| 154 | $this->open($format); |
||
| 155 | } |
||
| 156 | |||
| 157 | protected function open($archiveType) |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @inheritDoc |
||
| 185 | */ |
||
| 186 | public function getArchiveInformation() |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @inheritDoc |
||
| 213 | */ |
||
| 214 | public function getFileNames() |
||
| 215 | { |
||
| 216 | $files = []; |
||
| 217 | |||
| 218 | $Content = $this->tar->listContent(); |
||
| 219 | foreach ($Content as $i => $file) { |
||
| 220 | // BUG workaround: http://pear.php.net/bugs/bug.php?id=20275 |
||
| 221 | if ($file['filename'] === 'pax_global_header') { |
||
| 222 | continue; |
||
| 223 | } |
||
| 224 | $files[] = $file['filename']; |
||
| 225 | } |
||
| 226 | |||
| 227 | return $files; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @inheritDoc |
||
| 232 | */ |
||
| 233 | public function isFileExists($fileName) |
||
| 234 | { |
||
| 235 | return isset($this->pearFilesIndex[$fileName]); |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @inheritDoc |
||
| 240 | */ |
||
| 241 | public function getFileData($fileName) |
||
| 242 | { |
||
| 243 | if (!isset($this->pearFilesIndex[$fileName])) |
||
| 244 | throw new NonExistentArchiveFileException('File '.$fileName.' is not found in archive files list'); |
||
| 245 | |||
| 246 | $index = $this->pearFilesIndex[$fileName]; |
||
| 247 | |||
| 248 | $files_list = $this->tar->listContent(); |
||
| 249 | if (!isset($files_list[$index])) |
||
| 250 | throw new NonExistentArchiveFileException('File '.$fileName.' is not found in Tar archive'); |
||
| 251 | |||
| 252 | $data = $files_list[$index]; |
||
| 253 | unset($files_list); |
||
| 254 | |||
| 255 | return new ArchiveEntry($fileName, $data['size'] / $this->pearCompressionRatio, |
||
| 256 | $data['size'], $data['mtime'], in_array(strtolower(pathinfo($this->archiveFileName, |
||
| 257 | PATHINFO_EXTENSION)), ['gz', 'bz2', 'xz', 'Z'])); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @inheritDoc |
||
| 262 | */ |
||
| 263 | public function getFileContent($fileName) |
||
| 264 | { |
||
| 265 | if (!isset($this->pearFilesIndex[$fileName])) |
||
| 266 | throw new NonExistentArchiveFileException('File '.$fileName.' is not found in archive files list'); |
||
| 267 | |||
| 268 | return $this->tar->extractInString($fileName); |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @inheritDoc |
||
| 273 | */ |
||
| 274 | public function getFileStream($fileName) |
||
| 275 | { |
||
| 276 | if (!isset($this->pearFilesIndex[$fileName])) |
||
| 277 | throw new NonExistentArchiveFileException('File '.$fileName.' is not found in archive files list'); |
||
| 278 | |||
| 279 | return self::wrapStringInStream($this->tar->extractInString($fileName)); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @inheritDoc |
||
| 284 | */ |
||
| 285 | public function extractFiles($outputFolder, array $files) |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @inheritDoc |
||
| 297 | */ |
||
| 298 | public function extractArchive($outputFolder) |
||
| 299 | { |
||
| 300 | $result = $this->tar->extract($outputFolder); |
||
| 301 | if ($result === false) { |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @inheritDoc |
||
| 310 | */ |
||
| 311 | public function addFiles(array $files) |
||
| 312 | { |
||
| 313 | $added = 0; |
||
| 314 | foreach ($files as $localName => $filename) { |
||
| 315 | $remove_dir = dirname($filename); |
||
| 316 | $add_dir = dirname($localName); |
||
| 317 | if (is_null($filename)) { |
||
| 318 | if ($this->tar->addString($localName, "") === false) { |
||
| 319 | throw new ArchiveModificationException('Could not add directory "'.$filename.'": '.$this->tar->error_object->message, $this->tar->error_object->code); |
||
| 320 | } |
||
| 321 | } else { |
||
| 322 | if ($this->tar->addModify($filename, $add_dir, $remove_dir) === false) { |
||
| 323 | throw new ArchiveModificationException('Could not add file "'.$filename.'": '.$this->tar->error_object->message, $this->tar->error_object->code); |
||
| 324 | } |
||
| 325 | $added++; |
||
| 326 | } |
||
| 327 | } |
||
| 328 | return $added; |
||
| 329 | } |
||
| 330 | |||
| 331 | public static function canCreateArchive($format) |
||
| 332 | { |
||
| 333 | return true; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param $format |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | public static function canAddFiles($format) |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param string $inArchiveName |
||
| 347 | * @param string $content |
||
| 348 | * @return bool|true |
||
| 349 | */ |
||
| 350 | public function addFileFromString($inArchiveName, $content) |
||
| 353 | } |
||
| 354 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths