| Conditions | 15 |
| Paths | 60 |
| Total Lines | 101 |
| Code Lines | 51 |
| 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 |
||
| 97 | protected function readFromHttpData() |
||
| 98 | { |
||
| 99 | // Check validity of the uploaded files |
||
| 100 | $aTempFiles = []; |
||
| 101 | foreach($_FILES as $sVarName => $aFile) |
||
| 102 | { |
||
| 103 | if(is_array($aFile['name'])) |
||
| 104 | { |
||
| 105 | for($i = 0; $i < count($aFile['name']); $i++) |
||
| 106 | { |
||
| 107 | if(!$aFile['name'][$i]) |
||
| 108 | { |
||
| 109 | continue; |
||
| 110 | } |
||
| 111 | if(!array_key_exists($sVarName, $aTempFiles)) |
||
| 112 | { |
||
| 113 | $aTempFiles[$sVarName] = []; |
||
| 114 | } |
||
| 115 | // Filename without the extension |
||
| 116 | $sFilename = $this->filterFilename(pathinfo($aFile['name'][$i], PATHINFO_FILENAME), $sVarName); |
||
| 117 | // Copy the file data into the local array |
||
| 118 | $aTempFiles[$sVarName][] = [ |
||
| 119 | 'name' => $aFile['name'][$i], |
||
| 120 | 'type' => $aFile['type'][$i], |
||
| 121 | 'tmp_name' => $aFile['tmp_name'][$i], |
||
| 122 | 'error' => $aFile['error'][$i], |
||
| 123 | 'size' => $aFile['size'][$i], |
||
| 124 | 'filename' => $sFilename, |
||
| 125 | 'extension' => pathinfo($aFile['name'][$i], PATHINFO_EXTENSION), |
||
| 126 | ]; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | else |
||
| 130 | { |
||
| 131 | if(!$aFile['name']) |
||
| 132 | { |
||
| 133 | continue; |
||
| 134 | } |
||
| 135 | if(!array_key_exists($sVarName, $aTempFiles)) |
||
| 136 | { |
||
| 137 | $aTempFiles[$sVarName] = []; |
||
| 138 | } |
||
| 139 | // Filename without the extension |
||
| 140 | $sFilename = $this->filterFilename(pathinfo($aFile['name'], PATHINFO_FILENAME), $sVarName); |
||
| 141 | // Copy the file data into the local array |
||
| 142 | $aTempFiles[$sVarName][] = [ |
||
| 143 | 'name' => $aFile['name'], |
||
| 144 | 'type' => $aFile['type'], |
||
| 145 | 'tmp_name' => $aFile['tmp_name'], |
||
| 146 | 'error' => $aFile['error'], |
||
| 147 | 'size' => $aFile['size'], |
||
| 148 | 'filename' => $sFilename, |
||
| 149 | 'extension' => pathinfo($aFile['name'], PATHINFO_EXTENSION), |
||
| 150 | ]; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | // Default upload dir |
||
| 155 | $sDefaultUploadDir = $this->getOption('upload.default.dir'); |
||
| 156 | |||
| 157 | // Check uploaded files validity |
||
| 158 | foreach($aTempFiles as $sVarName => $aFiles) |
||
| 159 | { |
||
| 160 | foreach($aFiles as $aFile) |
||
| 161 | { |
||
| 162 | // Verify upload result |
||
| 163 | if($aFile['error'] != 0) |
||
| 164 | { |
||
| 165 | throw new \Jaxon\Exception\Error($this->trans('errors.upload.failed', $aFile)); |
||
| 166 | } |
||
| 167 | // Verify file validity (format, size) |
||
| 168 | if(!$this->validateUploadedFile($sVarName, $aFile)) |
||
| 169 | { |
||
| 170 | throw new \Jaxon\Exception\Error($this->getValidatorMessage()); |
||
| 171 | } |
||
| 172 | // Verify that the upload dir exists and is writable |
||
| 173 | $sUploadDir = $this->getOption('upload.files.' . $sVarName . '.dir', $sDefaultUploadDir); |
||
| 174 | $sUploadDir = rtrim(trim($sUploadDir), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
||
| 175 | if(!is_writable($sUploadDir)) |
||
| 176 | { |
||
| 177 | throw new \Jaxon\Exception\Error($this->trans('errors.upload.access')); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | // Copy the uploaded files from the temp dir to the user dir |
||
| 183 | foreach($aTempFiles as $sVarName => $aTempFiles) |
||
| 184 | { |
||
| 185 | $this->aUserFiles[$sVarName] = []; |
||
| 186 | foreach($aTempFiles as $aFile) |
||
| 187 | { |
||
| 188 | // Set the user file data |
||
| 189 | $sUploadDir = $this->getOption('upload.files.' . $sVarName . '.dir', $sDefaultUploadDir); |
||
| 190 | $sUploadDir = rtrim(trim($sUploadDir), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
||
| 191 | $xUploadedFile = UploadedFile::fromHttpData($sUploadDir, $aFile); |
||
| 192 | // All's right, move the file to the user dir. |
||
| 193 | move_uploaded_file($aFile["tmp_name"], $xUploadedFile->path()); |
||
| 194 | $this->aUserFiles[$sVarName][] = $xUploadedFile; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 359 |
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: