Conditions | 10 |
Paths | 5 |
Total Lines | 42 |
Code Lines | 21 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
125 | public function Zip($source, $destination) |
||
126 | { |
||
127 | // check for php_zip extension and file existance |
||
128 | if (! extension_loaded('zip') || ! file_exists($source)) { |
||
129 | return false; |
||
130 | } |
||
131 | |||
132 | // create ZipArchive Object |
||
133 | $zip = new \ZipArchive(); |
||
134 | |||
135 | // Create a new ZIP |
||
136 | if (! $zip->open($destination, \ZIPARCHIVE::CREATE)) { |
||
137 | return false; |
||
138 | } |
||
139 | |||
140 | $source = str_replace('\\', '/', realpath($source)); |
||
141 | |||
142 | if (is_dir($source) === true) { |
||
143 | $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source), \RecursiveIteratorIterator::SELF_FIRST); |
||
144 | |||
145 | foreach ($files as $file) { |
||
146 | $file = str_replace('\\', '/', $file); |
||
147 | |||
148 | // Ignore "." and ".." folders |
||
149 | if (in_array(substr($file, strrpos($file, '/') + 1), ['.', '..'])) { |
||
150 | continue; |
||
151 | } |
||
152 | |||
153 | $file = realpath($file); |
||
154 | |||
155 | if (is_dir($file) === true) { |
||
156 | $zip->addEmptyDir(str_replace($source.'/', '', $file.'/')); |
||
157 | } elseif (is_file($file) === true) { |
||
158 | $zip->addFromString(str_replace($source.'/', '', $file), file_get_contents($file)); |
||
159 | } |
||
160 | } |
||
161 | } elseif (is_file($source) === true) { |
||
162 | $zip->addFromString(basename($source), file_get_contents($source)); |
||
163 | } |
||
164 | |||
165 | return $zip->close(); |
||
166 | } |
||
167 | } |
||
168 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.