| Total Complexity | 45 |
| Total Lines | 327 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ZipTask 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 ZipTask, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class ZipTask extends MatchingTask |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @var File |
||
| 45 | */ |
||
| 46 | private $zipFile; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var File |
||
| 50 | */ |
||
| 51 | private $baseDir; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Whether to include empty dirs in the archive. |
||
| 55 | */ |
||
| 56 | private $includeEmpty = true; |
||
| 57 | |||
| 58 | private $filesets = []; |
||
| 59 | |||
| 60 | private $ignoreLinks = false; |
||
| 61 | |||
| 62 | private $saveFileAttributes = false; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * File path prefix in zip archive |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | private $prefix = null; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Comment for zip archive. |
||
| 73 | * |
||
| 74 | * @var string $comment |
||
| 75 | */ |
||
| 76 | private $comment = ''; |
||
| 77 | private string $mtimeDummy; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Removes all external attributes from the Zip archive. |
||
| 81 | * |
||
| 82 | * @param \ZipArchive $zip The Zip archive |
||
| 83 | * |
||
| 84 | * @return void |
||
| 85 | */ |
||
| 86 | private static function clearExternalAttributes(\ZipArchive $zip) |
||
| 87 | { |
||
| 88 | for ($i = 0, $count = $zip->count(); $i < $count; ++$i) { |
||
| 89 | $zip->setExternalAttributesIndex($i, \ZipArchive::OPSYS_DOS, 0); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Add a new fileset. |
||
| 95 | * |
||
| 96 | * @return ZipFileSet |
||
| 97 | */ |
||
| 98 | public function createFileSet() |
||
| 99 | { |
||
| 100 | $this->fileset = new ZipFileSet(); |
||
| 101 | $this->filesets[] = $this->fileset; |
||
| 102 | |||
| 103 | return $this->fileset; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Add a new fileset. |
||
| 108 | * |
||
| 109 | * @param ZipFileSet $fileset |
||
| 110 | */ |
||
| 111 | public function addZipFileSet(ZipFileSet $fileset) |
||
| 112 | { |
||
| 113 | $this->filesets[] = $fileset; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Set is the name/location of where to create the zip file. |
||
| 118 | * |
||
| 119 | * @param File $destFile The output of the zip |
||
| 120 | */ |
||
| 121 | public function setDestFile(File $destFile) |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * This is the base directory to look in for things to zip. |
||
| 128 | * |
||
| 129 | * @param File $baseDir |
||
| 130 | */ |
||
| 131 | public function setBasedir(File $baseDir) |
||
| 132 | { |
||
| 133 | $this->baseDir = $baseDir; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Sets the file path prefix for file in the zip file. |
||
| 138 | * |
||
| 139 | * @param string $prefix Prefix |
||
| 140 | * |
||
| 141 | * @return void |
||
| 142 | */ |
||
| 143 | public function setPrefix($prefix) |
||
| 144 | { |
||
| 145 | $this->prefix = $prefix; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Set the include empty dirs flag. |
||
| 150 | * |
||
| 151 | * @param boolean Flag if empty dirs should be tarred too |
||
|
|
|||
| 152 | * @return void |
||
| 153 | */ |
||
| 154 | public function setIncludeEmptyDirs($bool) |
||
| 155 | { |
||
| 156 | $this->includeEmpty = (bool) $bool; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Set the ignore symlinks flag. |
||
| 161 | * |
||
| 162 | * @param boolean $bool Flag if symlinks should be ignored |
||
| 163 | * @return void |
||
| 164 | */ |
||
| 165 | public function setIgnoreLinks($bool) |
||
| 166 | { |
||
| 167 | $this->ignoreLinks = (bool) $bool; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Set the save file attributes flag. |
||
| 172 | * |
||
| 173 | * @param bool $bool Flag if file attributes should be saved |
||
| 174 | * @return void |
||
| 175 | */ |
||
| 176 | public function setSaveFileAttributes($bool) |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Add a comment to the zip archive. |
||
| 183 | * |
||
| 184 | * @param string $text |
||
| 185 | * |
||
| 186 | * @return void |
||
| 187 | */ |
||
| 188 | public function setComment($text) |
||
| 189 | { |
||
| 190 | $this->comment = $text; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * do the work |
||
| 195 | * |
||
| 196 | * @throws BuildException |
||
| 197 | */ |
||
| 198 | public function main() |
||
| 199 | { |
||
| 200 | if (!extension_loaded('zip')) { |
||
| 201 | throw new BuildException("Zip extension is required"); |
||
| 202 | } |
||
| 203 | |||
| 204 | if ($this->zipFile === null) { |
||
| 205 | throw new BuildException("zipfile attribute must be set!", $this->getLocation()); |
||
| 206 | } |
||
| 207 | |||
| 208 | if ($this->zipFile->exists() && $this->zipFile->isDirectory()) { |
||
| 209 | throw new BuildException("zipfile is a directory!", $this->getLocation()); |
||
| 210 | } |
||
| 211 | |||
| 212 | if ($this->zipFile->exists() && !$this->zipFile->canWrite()) { |
||
| 213 | throw new BuildException("Can not write to the specified zipfile!", $this->getLocation()); |
||
| 214 | } |
||
| 215 | |||
| 216 | try { |
||
| 217 | if ($this->baseDir !== null) { |
||
| 218 | if (!$this->baseDir->exists()) { |
||
| 219 | throw new BuildException( |
||
| 220 | "basedir '" . (string) $this->baseDir . "' does not exist!", |
||
| 221 | $this->getLocation() |
||
| 222 | ); |
||
| 223 | } |
||
| 224 | |||
| 225 | if (empty($this->filesets)) { |
||
| 226 | // add the main fileset to the list of filesets to process. |
||
| 227 | $mainFileSet = new ZipFileSet($this->fileset); |
||
| 228 | $mainFileSet->setDir($this->baseDir); |
||
| 229 | $this->filesets[] = $mainFileSet; |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | if (empty($this->filesets)) { |
||
| 234 | throw new BuildException( |
||
| 235 | "You must supply either a basedir " |
||
| 236 | . "attribute or some nested filesets.", |
||
| 237 | $this->getLocation() |
||
| 238 | ); |
||
| 239 | } |
||
| 240 | |||
| 241 | // check if zip is out of date with respect to each |
||
| 242 | // fileset |
||
| 243 | if ($this->areFilesetsUpToDate()) { |
||
| 244 | $this->log("Nothing to do: " . $this->zipFile->__toString() . " is up to date.", Project::MSG_INFO); |
||
| 245 | |||
| 246 | return; |
||
| 247 | } |
||
| 248 | |||
| 249 | $this->log("Building zip: " . $this->zipFile->__toString(), Project::MSG_INFO); |
||
| 250 | |||
| 251 | if (false === $this->mtimeDummy = tempnam(sys_get_temp_dir(), 'mtimeDummy')) { |
||
| 252 | throw new Exception('Could not create temp file'); |
||
| 253 | } |
||
| 254 | |||
| 255 | $zip = new ZipArchive(); |
||
| 256 | $res = $zip->open($this->zipFile->getAbsolutePath(), ZipArchive::CREATE); |
||
| 257 | |||
| 258 | if ($res !== true) { |
||
| 259 | throw new Exception("ZipArchive::open() failed with code " . $res); |
||
| 260 | } |
||
| 261 | |||
| 262 | if ($this->comment !== '') { |
||
| 263 | $isCommented = $zip->setArchiveComment($this->comment); |
||
| 264 | if ($isCommented === false) { |
||
| 265 | $this->log("Could not add a comment for the Archive.", Project::MSG_INFO); |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | $this->addFilesetsToArchive($zip); |
||
| 270 | |||
| 271 | if (!$this->saveFileAttributes) { |
||
| 272 | self::clearExternalAttributes($zip); |
||
| 273 | } |
||
| 274 | |||
| 275 | $zip->close(); |
||
| 276 | unlink($this->mtimeDummy); |
||
| 277 | } catch (IOException $ioe) { |
||
| 278 | $msg = "Problem creating ZIP: " . $ioe->getMessage(); |
||
| 279 | throw new BuildException($msg, $ioe, $this->getLocation()); |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param array $files array of filenames |
||
| 285 | * @param File $dir |
||
| 286 | * @return boolean |
||
| 287 | */ |
||
| 288 | private function archiveIsUpToDate($files, $dir) |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @return array |
||
| 299 | * @throws BuildException |
||
| 300 | */ |
||
| 301 | public function areFilesetsUpToDate() |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param $zip |
||
| 322 | */ |
||
| 323 | private function addFilesetsToArchive($zip) |
||
| 351 | } |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param \ZipArchive $zip |
||
| 357 | * @param string $dirPath |
||
| 358 | * @param string $entryName |
||
| 359 | * |
||
| 360 | * @return void |
||
| 361 | */ |
||
| 362 | private function addDirToZip(\ZipArchive $zip, string $dirPath, string $entryName) |
||
| 368 | } |
||
| 369 | } |
||
| 371 |
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