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 Uploader 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 Uploader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Uploader { |
||
10 | |||
11 | /** |
||
12 | * File to upload. |
||
13 | */ |
||
14 | var $file = array(); |
||
15 | |||
16 | /** |
||
17 | * Global options |
||
18 | * fileTypes to allow to upload |
||
19 | */ |
||
20 | var $options = array(); |
||
21 | |||
22 | /** |
||
23 | * errors holds any errors that occur as string values. |
||
24 | * this can be access to debug the FileUploadComponent |
||
25 | * |
||
26 | * @var array |
||
27 | * @access public |
||
28 | */ |
||
29 | var $errors = array(); |
||
30 | |||
31 | /** |
||
32 | * Definitions of errors that could occur during upload |
||
33 | * |
||
34 | * @author Jon Langevin |
||
35 | * @var array |
||
36 | */ |
||
37 | var $uploadErrors = array( |
||
38 | UPLOAD_ERR_OK => 'There is no error, the file uploaded with success.', |
||
39 | UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.', |
||
40 | UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.', |
||
41 | UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.', |
||
42 | UPLOAD_ERR_NO_FILE => 'No file was uploaded.', |
||
43 | UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.', //Introduced in PHP 4.3.10 and PHP 5.0.3. |
||
44 | UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.', //Introduced in PHP 5.1.0. |
||
45 | UPLOAD_ERR_EXTENSION => 'File upload stopped by extension.' //Introduced in PHP 5.2.0. |
||
46 | ); |
||
47 | |||
48 | /** |
||
49 | * Final file is set on move_uploaded_file success. |
||
50 | * This is the file name of the final file that was uploaded |
||
51 | * to the uploadDir directory |
||
52 | * |
||
53 | * @var string of final file name uploaded |
||
54 | * @access public |
||
55 | */ |
||
56 | var $finalFile = null; |
||
57 | |||
58 | function __construct($options = array()){ |
||
61 | |||
62 | /** |
||
63 | * Preform requested callbacks on the filename. |
||
64 | * |
||
65 | * @var string chosen filename |
||
66 | * @return string of resulting filename |
||
67 | * @access private |
||
68 | */ |
||
69 | function __handleFileNameCallback($fileName){ |
||
92 | |||
93 | /** |
||
94 | * Preform requested target patch checks depending on the unique setting |
||
95 | * |
||
96 | * @var string chosen filename target_path |
||
97 | * @return string of resulting target_path |
||
98 | * @access private |
||
99 | */ |
||
100 | function __handleUnique($target_path){ |
||
111 | |||
112 | /** |
||
113 | * processFile will take a file, or use the current file given to it |
||
114 | * and attempt to save the file to the file system. |
||
115 | * processFile will check to make sure the file is there, and its type is allowed to be saved. |
||
116 | * |
||
117 | * @param file array of uploaded file (optional) |
||
118 | * @return String | false String of finalFile name saved to the file system or false if unable to save to file system. |
||
119 | * @access public |
||
120 | */ |
||
121 | function processFile($file = null){ |
||
149 | |||
150 | /** |
||
151 | * setFile will set a this->file if given one. |
||
152 | * |
||
153 | * @param file array of uploaded file. (optional) |
||
154 | * @return void |
||
155 | */ |
||
156 | function setFile($file = null){ |
||
159 | |||
160 | /** |
||
161 | * Returns the extension of the uploaded filename. |
||
162 | * |
||
163 | * @return string $extension A filename extension |
||
164 | * @param file array of uploaded file (optional) |
||
165 | * @access protected |
||
166 | */ |
||
167 | function _ext($file = null){ |
||
171 | |||
172 | /** |
||
173 | * Adds error messages to the component |
||
174 | * |
||
175 | * @param string $text String of error message to save |
||
176 | * @return void |
||
177 | * @access protected |
||
178 | */ |
||
179 | function _error($text){ |
||
182 | |||
183 | /** |
||
184 | * Checks if the uploaded type is allowed defined in the allowedTypes |
||
185 | * |
||
186 | * @return boolean if type is accepted |
||
187 | * @param file array of uploaded file (optional) |
||
188 | * @access public |
||
189 | */ |
||
190 | function checkType($file = null){ |
||
216 | |||
217 | /** |
||
218 | * Checks if there is a file uploaded |
||
219 | * |
||
220 | * @return void |
||
221 | * @access public |
||
222 | * @param file array of uploaded file (optional) |
||
223 | */ |
||
224 | function checkFile($file = null){ |
||
236 | |||
237 | /** |
||
238 | * Checks if the file uploaded exceeds the maxFileSize setting (if there is onw) |
||
239 | * |
||
240 | * @return boolean |
||
241 | * @access public |
||
242 | * @param file array of uploaded file (optional) |
||
243 | */ |
||
244 | function checkSize($file = null){ |
||
260 | |||
261 | /** |
||
262 | * removeFile removes a specific file from the uploaded directory |
||
263 | * |
||
264 | * @param string $name A reference to the filename to delete from the uploadDirectory |
||
265 | * @return boolean |
||
266 | * @access public |
||
267 | */ |
||
268 | function removeFile($name = null){ |
||
283 | |||
284 | /** |
||
285 | * hasUpload |
||
286 | * |
||
287 | * @return boolean true | false depending if a file was actually uploaded. |
||
288 | * @param file array of uploaded file (optional) |
||
289 | */ |
||
290 | function hasUpload($file = null){ |
||
294 | |||
295 | /** |
||
296 | * @return boolean true if errors were detected. |
||
297 | */ |
||
298 | function hasErrors(){ |
||
301 | |||
302 | /** |
||
303 | * showErrors itterates through the errors array |
||
304 | * and returns a concatinated string of errors sepearated by |
||
305 | * the $sep |
||
306 | * |
||
307 | * @param string $sep A seperated defaults to <br /> |
||
308 | * @return string |
||
309 | * @access public |
||
310 | */ |
||
311 | function showErrors($sep = " "){ |
||
318 | |||
319 | /** |
||
320 | * Searches through the $haystack for a $key. |
||
321 | * |
||
322 | * @param string $needle String of key to search for in $haystack |
||
323 | * @param array $haystack Array of which to search for $needle |
||
324 | * @return boolean true if given key is in an array |
||
325 | * @access protected |
||
326 | */ |
||
327 | function _multiArrayKeyExists($needle, $haystack) { |
||
342 | |||
343 | } |
||
344 | ?> |
||
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.