| Conditions | 11 |
| Paths | 65 |
| Total Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 45 | public function getUploadedFiles() |
||
| 46 | { |
||
| 47 | // Check validity of the uploaded files |
||
| 48 | $aTempFiles = []; |
||
| 49 | foreach($_FILES as $sVarName => $aFile) |
||
| 50 | { |
||
| 51 | // If there is only one file, transform each entry into an array, |
||
| 52 | // so the same processing for multiple files can be applied. |
||
| 53 | if(!is_array($aFile['name'])) |
||
| 54 | { |
||
| 55 | $aFile['name'] = [$aFile['name']]; |
||
| 56 | $aFile['type'] = [$aFile['type']]; |
||
| 57 | $aFile['tmp_name'] = [$aFile['tmp_name']]; |
||
| 58 | $aFile['error'] = [$aFile['error']]; |
||
| 59 | $aFile['size'] = [$aFile['size']]; |
||
| 60 | } |
||
| 61 | |||
| 62 | $nFileCount = count($aFile['name']); |
||
| 63 | for($i = 0; $i < $nFileCount; $i++) |
||
| 64 | { |
||
| 65 | if(!$aFile['name'][$i]) |
||
| 66 | { |
||
| 67 | continue; |
||
| 68 | } |
||
| 69 | if(!array_key_exists($sVarName, $aTempFiles)) |
||
| 70 | { |
||
| 71 | $aTempFiles[$sVarName] = []; |
||
| 72 | } |
||
| 73 | // Filename without the extension |
||
| 74 | $sFilename = pathinfo($aFile['name'][$i], PATHINFO_FILENAME); |
||
| 75 | if(($this->cNameSanitizer)) |
||
| 76 | { |
||
| 77 | $sFilename = (string)call_user_func_array($$this->cNameSanitizer, [$sFilename, $sVarName]); |
||
| 78 | } |
||
| 79 | // Copy the file data into the local array |
||
| 80 | $aTempFiles[$sVarName][] = [ |
||
| 81 | 'name' => $aFile['name'][$i], |
||
| 82 | 'type' => $aFile['type'][$i], |
||
| 83 | 'tmp_name' => $aFile['tmp_name'][$i], |
||
| 84 | 'error' => $aFile['error'][$i], |
||
| 85 | 'size' => $aFile['size'][$i], |
||
| 86 | 'filename' => $sFilename, |
||
| 87 | 'extension' => pathinfo($aFile['name'][$i], PATHINFO_EXTENSION), |
||
| 88 | ]; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | // Check uploaded files validity |
||
| 93 | foreach($aTempFiles as $sVarName => $aFiles) |
||
| 94 | { |
||
| 95 | foreach($aFiles as $aFile) |
||
| 96 | { |
||
| 97 | // Verify upload result |
||
| 98 | if($aFile['error'] != 0) |
||
| 99 | { |
||
| 100 | throw new \Jaxon\Exception\Error($this->trans('errors.upload.failed', $aFile)); |
||
| 101 | } |
||
| 102 | // Verify file validity (format, size) |
||
| 103 | if(!$this->validateUploadedFile($sVarName, $aFile)) |
||
| 104 | { |
||
| 105 | throw new \Jaxon\Exception\Error($this->getValidatorMessage()); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | return $aTempFiles; |
||
| 111 | } |
||
| 112 | } |
||
| 113 |