Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Nip_File_System 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Nip_File_System, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class Nip_File_System |
||
|
|||
6 | { |
||
7 | |||
8 | protected $_uploadErrors = array( |
||
9 | 0 => "There is no error, the file uploaded with success", |
||
10 | 1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini", |
||
11 | 2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form", |
||
12 | 3 => "The uploaded file was only partially uploaded", |
||
13 | 4 => "No file was uploaded", |
||
14 | 6 => "Missing a temporary folder", |
||
15 | ); |
||
16 | |||
17 | /** |
||
18 | * Singleton |
||
19 | * |
||
20 | * @return self |
||
21 | */ |
||
22 | public static function instance() |
||
31 | |||
32 | /** |
||
33 | * Returns error message on upload, if any |
||
34 | * |
||
35 | * @param string $file |
||
36 | * @param array $extensions |
||
37 | * @return mixed |
||
38 | */ |
||
39 | public function getUploadError($file, $extensions = array()) |
||
62 | |||
63 | /** |
||
64 | * Returns error message on upload, if any |
||
65 | * |
||
66 | * @param string $file |
||
67 | * @param array $extensions |
||
68 | * @return mixed |
||
69 | */ |
||
70 | public function getUploadErrorNo($file, $extensions = array()) |
||
104 | |||
105 | /** |
||
106 | * Get file extension |
||
107 | * |
||
108 | * @param string $str |
||
109 | * @return string |
||
110 | */ |
||
111 | public function getExtension($str) |
||
115 | |||
116 | /** |
||
117 | * Gets list of all files within a directory |
||
118 | * |
||
119 | * @param string $dir |
||
120 | * @param boolean $recursive |
||
121 | * @return array |
||
122 | */ |
||
123 | public function scanDirectory($dir, $recursive = false, $fullPaths = false) |
||
147 | |||
148 | /** |
||
149 | * Recursively create a directory and set it's permissions |
||
150 | * |
||
151 | * @param string $dir |
||
152 | * @param int $mode |
||
153 | * @return boolean |
||
154 | */ |
||
155 | public function createDirectory($dir, $mode = 0777) |
||
159 | |||
160 | /** |
||
161 | * Builds array-tree of directory, with files as final nodes |
||
162 | * |
||
163 | * @param string $dir |
||
164 | * @param array $tree |
||
165 | * @return array |
||
166 | */ |
||
167 | public function directoryTree($dir, $tree = array()) |
||
187 | |||
188 | /** |
||
189 | * Recursively empties a directory |
||
190 | * @param string $dir |
||
191 | */ |
||
192 | public function emptyDirectory($dir) |
||
212 | |||
213 | /** |
||
214 | * Recursively removes a directory |
||
215 | * @param string $dir |
||
216 | */ |
||
217 | public function removeDirectory($dir) |
||
238 | |||
239 | public function deleteFile($path) |
||
247 | |||
248 | View Code Duplication | public function copyDirectory($source, $destination) |
|
249 | { |
||
250 | if (!is_dir($destination)) { |
||
251 | mkdir($destination, 0755, true); |
||
252 | } |
||
253 | $process = new Nip_Process("cp -R -f $source/* $destination"); |
||
254 | |||
255 | return $process->run(); |
||
256 | } |
||
257 | |||
258 | public function formatSize($bytes) |
||
269 | |||
270 | public function chmod($file, $mode) |
||
276 | |||
277 | } |
||
278 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.