| Conditions | 10 |
| Paths | 36 |
| Total Lines | 66 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 55 | public function ProcessZip($foldercontent, $folder, $maxsize) { |
||
| 56 | |||
| 57 | $split = array(); |
||
| 58 | |||
| 59 | $splits = 1; |
||
| 60 | $t = 0; |
||
| 61 | |||
| 62 | // Determine how many zip files to create |
||
| 63 | if ( isset( $foldercontent ) ) { |
||
| 64 | foreach ($foldercontent as $entry) { |
||
| 65 | |||
| 66 | $t = $t + $entry['size']; |
||
| 67 | |||
| 68 | if ($entry['type'] == 'dir') { |
||
| 69 | $lastdir = $entry; |
||
| 70 | } |
||
| 71 | |||
| 72 | if ($t >= $maxsize) { |
||
| 73 | $splits++; |
||
| 74 | $t = 0; |
||
| 75 | // create lastdir in next archive, in case files still exist |
||
| 76 | // even if the next file is not in this archive it doesn't hurt |
||
| 77 | if ($lastdir !== '') { |
||
| 78 | $split[$splits][] = $lastdir; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | $split[$splits][] = $entry; |
||
| 83 | } |
||
| 84 | |||
| 85 | // delete the $foldercontent array |
||
| 86 | unset($foldercontent); |
||
| 87 | |||
| 88 | // Create the folder to put the zip files in |
||
| 89 | $date = new DateTime(); |
||
| 90 | $tS = $date->format('YmdHis'); |
||
| 91 | |||
| 92 | // Process the splits |
||
| 93 | foreach ($split as $idx => $sp) { |
||
| 94 | |||
| 95 | // create the zip file |
||
| 96 | |||
| 97 | $zip = new ZipArchive(); |
||
| 98 | |||
| 99 | $destination = $folder . '.zip'; |
||
| 100 | |||
| 101 | if (!$zip->open($destination, ZIPARCHIVE::CREATE)) { |
||
| 102 | return false; |
||
| 103 | } |
||
| 104 | |||
| 105 | $i = 1; |
||
| 106 | $dir = ""; |
||
| 107 | foreach ($sp as $entry) { |
||
| 108 | if ($entry['type'] === 'dir') { |
||
| 109 | $dir = explode('\\', $entry['file']); |
||
| 110 | $zip->addEmptyDir(end($dir)); |
||
| 111 | } else { |
||
| 112 | $zip->addFromString(end($dir).'/'.$i.'.jpg', file_get_contents($entry['file'])); |
||
| 113 | $i++; |
||
| 114 | } |
||
| 115 | } |
||
| 116 | $zip->close(); |
||
| 117 | } |
||
| 118 | return array( |
||
| 119 | 'splits' => count($split), |
||
| 120 | 'foldername' => '' |
||
| 121 | ); |
||
| 200 | } |