Complex classes like FileUploadValidation 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 FileUploadValidation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class FileUploadValidation |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | private static $byte = [ |
||
| 26 | 'K' => 1000, |
||
| 27 | 'KB' => 1000, |
||
| 28 | 'M' => 1000000, |
||
| 29 | 'MB' => 1000000, |
||
| 30 | 'G' => 1000000000, |
||
| 31 | 'GB' => 1000000000, |
||
| 32 | 'T' => 1000000000000, |
||
| 33 | 'TB' => 1000000000000, |
||
| 34 | ]; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Validates if the given data is a file that was uploaded |
||
| 38 | * |
||
| 39 | * @param string $uploadName |
||
| 40 | * |
||
| 41 | * @return bool |
||
| 42 | */ |
||
| 43 | public static function isUploaded($uploadName) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @return int |
||
| 50 | */ |
||
| 51 | private static function getMaxServerFileSize() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param string $uploadName |
||
| 68 | * @param integer $minSize |
||
| 69 | * @param integer $maxSize |
||
| 70 | * @param string $format |
||
| 71 | * @param bool $inclusive |
||
| 72 | * |
||
| 73 | * @return bool |
||
| 74 | * @throws FileUploadException |
||
| 75 | */ |
||
| 76 | public static function isBetween($uploadName, $minSize, $maxSize, $format = 'B', $inclusive = false) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param string $uploadName |
||
| 108 | * @param $size |
||
| 109 | * @param $maxSize |
||
| 110 | * |
||
| 111 | * @throws FileUploadException |
||
| 112 | */ |
||
| 113 | private static function checkIfMaximumUploadFileSizeHasBeenExceeded($uploadName, $size, $maxSize) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param $filePath |
||
| 122 | * |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | private static function getMimeType($filePath) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param string $uploadName |
||
| 143 | * @param string[] $allowedTypes |
||
| 144 | * |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | public static function isMimeType($uploadName, array $allowedTypes) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param string $uploadName |
||
| 169 | * @param AbstractValidator $validator |
||
| 170 | * |
||
| 171 | * @return bool |
||
| 172 | */ |
||
| 173 | public static function hasFileNameFormat($uploadName, AbstractValidator $validator) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param string $uploadName |
||
| 189 | * @param string $uploadDir |
||
| 190 | * |
||
| 191 | * @return bool |
||
| 192 | */ |
||
| 193 | public static function hasValidUploadDirectory($uploadName, $uploadDir) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $uploadName |
||
| 206 | * @param string $uploadDir |
||
| 207 | * |
||
| 208 | * @return bool |
||
| 209 | */ |
||
| 210 | public static function notOverwritingExistingFile($uploadName, $uploadDir) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param string $uploadName |
||
| 230 | * @param integer $size |
||
| 231 | * |
||
| 232 | * @return bool |
||
| 233 | */ |
||
| 234 | public static function hasLength($uploadName, $size) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param string $uploadName |
||
| 247 | * |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | public static function isImage($uploadName) |
||
| 254 | } |
||
| 255 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: