| Conditions | 11 |
| Paths | 7 |
| Total Lines | 47 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 96 | public static function zip (string $path, string $destination, bool $self = true, bool $sub_folder = true): bool { |
||
| 97 | if (extension_loaded('zip')) { |
||
| 98 | if (file_exists($destination)) unlink($destination); |
||
| 99 | |||
| 100 | $path = realpath($path); |
||
| 101 | $zip = new ZipArchive(); |
||
| 102 | $zip->open($destination, ZipArchive::CREATE); |
||
| 103 | |||
| 104 | if (is_dir($path)){ |
||
| 105 | if ($self){ |
||
| 106 | $dirs = explode('\\',$path); |
||
| 107 | $dir_count = count($dirs); |
||
| 108 | $main_dir = $dirs[$dir_count-1]; |
||
| 109 | |||
| 110 | $path = ''; |
||
| 111 | for ($i=0; $i < $dir_count - 1; $i++) { |
||
| 112 | $path .= '\\' . $dirs[$i]; |
||
| 113 | } |
||
| 114 | $path = substr($path, 1); |
||
| 115 | $zip->addEmptyDir($main_dir); |
||
| 116 | } |
||
| 117 | |||
| 118 | $it = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS); |
||
| 119 | $files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::SELF_FIRST); |
||
| 120 | foreach ($files as $file) { |
||
| 121 | if ($file->isFile()){ |
||
| 122 | if ($sub_folder){ |
||
| 123 | $zip->addFile($file, str_replace($path . '\\', '', $file)); |
||
| 124 | } |
||
| 125 | else{ |
||
| 126 | $zip->addFile($file, basename($file)); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | elseif ($file->isDir() && $sub_folder) { |
||
| 130 | $zip->addEmptyDir(str_replace($path . '\\', '', $file . '\\')); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 | else{ |
||
| 135 | $zip->addFile($path, basename($path)); |
||
| 136 | } |
||
| 137 | |||
| 138 | return $zip->close(); |
||
| 139 | } |
||
| 140 | else { |
||
| 141 | logger::write("tools::zip function used\nzip extension is not found , It may not be installed or enabled",loggerTypes::ERROR); |
||
| 142 | throw new bptException('ZIP_EXTENSION_MISSING'); |
||
| 143 | } |
||
| 145 | } |