Complex classes like XoopsMediaUploader 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 XoopsMediaUploader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
67 | class XoopsMediaUploader |
||
68 | { |
||
69 | public $mediaName; |
||
70 | public $mediaType; |
||
71 | public $mediaSize; |
||
72 | public $mediaTmpName; |
||
73 | public $mediaError; |
||
74 | public $uploadDir = ''; |
||
75 | public $allowedMimeTypes = array(); |
||
76 | public $maxFileSize = 0; |
||
77 | public $maxWidth; |
||
78 | public $maxHeight; |
||
79 | public $targetFileName; |
||
80 | public $prefix; |
||
81 | public $ext; |
||
82 | public $dimension; |
||
83 | public $errors = array(); |
||
84 | public $savedDestination; |
||
85 | public $savedFileName; |
||
86 | /** |
||
87 | * No admin check for uploads |
||
88 | */ |
||
89 | public $noadmin_sizecheck; |
||
90 | |||
91 | /** |
||
92 | * Constructor |
||
93 | * |
||
94 | * @param string $uploadDir |
||
95 | * @param array|int $allowedMimeTypes |
||
96 | * @param int $maxFileSize |
||
97 | * @param int $maxWidth |
||
98 | * @param int $maxHeight |
||
99 | * |
||
100 | * @internal param int $cmodvalue |
||
101 | */ |
||
102 | public function __construct($uploadDir, $allowedMimeTypes = 0, $maxFileSize, $maxWidth = 0, $maxHeight = 0) |
||
116 | |||
117 | /** |
||
118 | * @param $value |
||
119 | */ |
||
120 | public function noAdminSizeCheck($value) |
||
124 | |||
125 | /** |
||
126 | * Fetch the uploaded file |
||
127 | * |
||
128 | * @param string $media_name Name of the file field |
||
129 | * @param int $index Index of the file (if more than one uploaded under that name) |
||
130 | * |
||
131 | * @global $HTTP_POST_FILES |
||
132 | * @return bool |
||
133 | */ |
||
134 | public function fetchMedia($media_name, $index = null) |
||
210 | |||
211 | /** |
||
212 | * Set the target filename |
||
213 | * |
||
214 | * @param string $value |
||
215 | */ |
||
216 | public function setTargetFileName($value) |
||
220 | |||
221 | /** |
||
222 | * Set the prefix |
||
223 | * |
||
224 | * @param string $value |
||
225 | */ |
||
226 | public function setPrefix($value) |
||
230 | |||
231 | /** |
||
232 | * Get the uploaded filename |
||
233 | * |
||
234 | * @return string |
||
235 | */ |
||
236 | public function getMediaName() |
||
240 | |||
241 | /** |
||
242 | * Get the type of the uploaded file |
||
243 | * |
||
244 | * @return string |
||
245 | */ |
||
246 | public function getMediaType() |
||
250 | |||
251 | /** |
||
252 | * Get the size of the uploaded file |
||
253 | * |
||
254 | * @return int |
||
255 | */ |
||
256 | public function getMediaSize() |
||
260 | |||
261 | /** |
||
262 | * Get the temporary name that the uploaded file was stored under |
||
263 | * |
||
264 | * @return string |
||
265 | */ |
||
266 | public function getMediaTmpName() |
||
270 | |||
271 | /** |
||
272 | * Get the saved filename |
||
273 | * |
||
274 | * @return string |
||
275 | */ |
||
276 | public function getSavedFileName() |
||
280 | |||
281 | /** |
||
282 | * Get the destination the file is saved to |
||
283 | * |
||
284 | * @return string |
||
285 | */ |
||
286 | public function getSavedDestination() |
||
290 | |||
291 | /** |
||
292 | * Check the file and copy it to the destination |
||
293 | * |
||
294 | * @param int $chmod |
||
295 | * |
||
296 | * @return bool |
||
297 | */ |
||
298 | public function upload($chmod = 0644) |
||
337 | |||
338 | /** |
||
339 | * Copy the file to its destination |
||
340 | * |
||
341 | * @param $chmod |
||
342 | * |
||
343 | * @return bool |
||
344 | */ |
||
345 | public function _copyFile($chmod) |
||
372 | |||
373 | /** |
||
374 | * Is the file the right size? |
||
375 | * |
||
376 | * @return bool |
||
377 | */ |
||
378 | public function checkMaxFileSize() |
||
389 | |||
390 | /** |
||
391 | * Is the picture the right width? |
||
392 | * |
||
393 | * @param $dimension |
||
394 | * |
||
395 | * @return bool |
||
396 | */ |
||
397 | public function checkMaxWidth($dimension) |
||
408 | |||
409 | /** |
||
410 | * Is the picture the right height? |
||
411 | * |
||
412 | * @param $dimension |
||
413 | * |
||
414 | * @return bool |
||
415 | */ |
||
416 | public function checkMaxHeight($dimension) |
||
427 | |||
428 | /** |
||
429 | * Is the file the right Mime type |
||
430 | * |
||
431 | * (is there a right type of mime? ;-) |
||
432 | * |
||
433 | * @return bool |
||
434 | */ |
||
435 | public function checkMimeType() |
||
439 | |||
440 | /** |
||
441 | * Add an error |
||
442 | * |
||
443 | * @param string $error |
||
444 | */ |
||
445 | public function setErrors($error) |
||
449 | |||
450 | /** |
||
451 | * Get generated errors |
||
452 | * |
||
453 | * @param bool $ashtml Format using HTML? |
||
454 | * |
||
455 | * @return array |string Array of array messages OR HTML string |
||
456 | */ |
||
457 | public function getErrors($ashtml = true) |
||
473 | } |
||
474 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.