Complex classes like Upload 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 Upload, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | trait Upload { |
||
| 15 | |||
| 16 | /** |
||
| 17 | *@var string contains the name of the file to be uploaded. |
||
| 18 | */ |
||
| 19 | protected $FileName; |
||
| 20 | /** |
||
| 21 | *@var string contains the temporary name of the file to be uploaded. |
||
| 22 | */ |
||
| 23 | protected $TempFileName; |
||
| 24 | /** |
||
| 25 | *@var string contains directory where the files should be uploaded. |
||
| 26 | */ |
||
| 27 | protected $UploadDirectory; |
||
| 28 | /** |
||
| 29 | *@var string contains an array of valid extensions which are allowed to be uploaded. |
||
| 30 | */ |
||
| 31 | protected $ValidExtensions; |
||
| 32 | /** |
||
| 33 | *@var string contains a message which can be used for debugging. |
||
| 34 | */ |
||
| 35 | protected $Message; |
||
| 36 | /** |
||
| 37 | *@var integer contains maximum size of fiels to be uploaded in bytes. |
||
| 38 | */ |
||
| 39 | protected $MaximumFileSize; |
||
| 40 | /** |
||
| 41 | *@var bool contains whether or not the files being uploaded are images. |
||
| 42 | */ |
||
| 43 | protected $IsImage; |
||
| 44 | /** |
||
| 45 | *@var string contains the email address of the recipient of upload logs. |
||
| 46 | */ |
||
| 47 | protected $Email; |
||
| 48 | /** |
||
| 49 | *@var integer contains maximum width of images to be uploaded. |
||
| 50 | */ |
||
| 51 | protected $MaximumWidth; |
||
| 52 | /** |
||
| 53 | *@var integer contains maximum height of images to be uploaded. |
||
| 54 | */ |
||
| 55 | protected $MaximumHeight; |
||
| 56 | |||
| 57 | public function upload() |
||
| 61 | |||
| 62 | /** |
||
| 63 | *@method bool ValidateExtension() returns whether the extension of file to be uploaded |
||
| 64 | * is allowable or not. |
||
| 65 | *@return true the extension is valid. |
||
| 66 | *@return false the extension is invalid. |
||
| 67 | */ |
||
| 68 | public function ValidateExtension() |
||
| 95 | |||
| 96 | /** |
||
| 97 | *@method bool ValidateSize() returns whether the file size is acceptable. |
||
| 98 | *@return true the size is smaller than the alloted value. |
||
| 99 | *@return false the size is larger than the alloted value. |
||
| 100 | */ |
||
| 101 | public function ValidateSize() |
||
| 120 | |||
| 121 | /** |
||
| 122 | *@method bool ValidateExistance() determins whether the file already exists. If so, rename $FileName. |
||
| 123 | *@return true can never be returned as all file names must be unique. |
||
| 124 | *@return false the file name does not exist. |
||
| 125 | */ |
||
| 126 | public function ValidateExistance() |
||
| 142 | |||
| 143 | /** |
||
| 144 | *@method bool ValidateDirectory() |
||
| 145 | *@return true the UploadDirectory exists, writable, and has a traling slash. |
||
| 146 | *@return false the directory was never set, does not exist, or is not writable. |
||
| 147 | */ |
||
| 148 | public function ValidateDirectory() |
||
| 177 | |||
| 178 | /** |
||
| 179 | *@method bool ValidateImage() |
||
| 180 | *@return true the image is smaller than the alloted dimensions. |
||
| 181 | *@return false the width and/or height is larger then the alloted dimensions. |
||
| 182 | */ |
||
| 183 | public function ValidateImage() { |
||
| 206 | |||
| 207 | /** |
||
| 208 | *@method bool UploadFile() uploads the file to the server after passing all the validations. |
||
| 209 | *@return true the file was uploaded. |
||
| 210 | *@return false the upload failed. |
||
| 211 | */ |
||
| 212 | public function uploadFile() |
||
| 251 | |||
| 252 | #Accessors and Mutators beyond this point. |
||
| 253 | #Siginificant documentation is not needed. |
||
| 254 | public function SetFileName($argv) |
||
| 258 | |||
| 259 | public function SetUploadDirectory($argv) |
||
| 263 | |||
| 264 | public function SetTempName($argv) |
||
| 268 | |||
| 269 | public function SetValidExtensions($argv) |
||
| 273 | |||
| 274 | public function SetMessage($argv) |
||
| 278 | |||
| 279 | public function SetMaximumFileSize($argv) |
||
| 283 | |||
| 284 | public function SetEmail($argv) |
||
| 288 | |||
| 289 | public function SetIsImage($argv) |
||
| 293 | |||
| 294 | public function SetMaximumWidth($argv) |
||
| 298 | |||
| 299 | public function SetMaximumHeight($argv) |
||
| 307 | |||
| 308 | public function GetUploadDirectory() |
||
| 312 | |||
| 313 | public function GetTempName() |
||
| 317 | |||
| 318 | public function GetValidExtensions() |
||
| 322 | |||
| 323 | public function GetMessage() |
||
| 331 | |||
| 332 | public function GetMaximumFileSize() |
||
| 336 | |||
| 337 | public function GetEmail() |
||
| 341 | |||
| 342 | public function GetIsImage() |
||
| 346 | |||
| 347 | public function GetMaximumWidth() |
||
| 351 | |||
| 352 | public function GetMaximumHeight() |
||
| 356 | } |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: