| Conditions | 14 |
| Paths | 38 |
| Total Lines | 69 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 138 | public function uploadFile(ServerRequestInterface $request): string |
||
| 139 | { |
||
| 140 | $tree = $request->getAttribute('tree'); |
||
| 141 | assert($tree instanceof Tree); |
||
| 142 | |||
| 143 | $params = $request->getParsedBody(); |
||
| 144 | $file_location = $params['file_location']; |
||
| 145 | |||
| 146 | switch ($file_location) { |
||
| 147 | case 'url': |
||
| 148 | $remote = $params['remote']; |
||
| 149 | |||
| 150 | if (strpos($remote, '://') !== false) { |
||
| 151 | return $remote; |
||
| 152 | } |
||
| 153 | |||
| 154 | return ''; |
||
| 155 | |||
| 156 | case 'unused': |
||
| 157 | $unused = $params['unused']; |
||
| 158 | |||
| 159 | if ($tree->mediaFilesystem()->has($unused)) { |
||
| 160 | return $unused; |
||
| 161 | } |
||
| 162 | |||
| 163 | return ''; |
||
| 164 | |||
| 165 | case 'upload': |
||
| 166 | default: |
||
| 167 | $folder = $params['folder']; |
||
| 168 | $auto = $params['auto']; |
||
| 169 | $new_file = $params['new_file']; |
||
| 170 | |||
| 171 | /** @var UploadedFileInterface|null $uploaded_file */ |
||
| 172 | $uploaded_file = $request->getUploadedFiles()['file']; |
||
| 173 | if ($uploaded_file === null || $uploaded_file->getError() !== UPLOAD_ERR_OK) { |
||
| 174 | return ''; |
||
| 175 | } |
||
| 176 | |||
| 177 | // The filename |
||
| 178 | $new_file = str_replace('\\', '/', $new_file); |
||
| 179 | if ($new_file !== '' && strpos($new_file, '/') === false) { |
||
| 180 | $file = $new_file; |
||
| 181 | } else { |
||
| 182 | $file = $uploaded_file->getClientFilename(); |
||
| 183 | } |
||
| 184 | |||
| 185 | // The folder |
||
| 186 | $folder = str_replace('\\', '/', $folder); |
||
| 187 | $folder = trim($folder, '/'); |
||
| 188 | if ($folder !== '') { |
||
| 189 | $folder .= '/'; |
||
| 190 | } |
||
| 191 | |||
| 192 | // Generate a unique name for the file? |
||
| 193 | if ($auto === '1' || $tree->mediaFilesystem()->has($folder . $file)) { |
||
| 194 | $folder = ''; |
||
| 195 | $extension = pathinfo($uploaded_file->getClientFilename(), PATHINFO_EXTENSION); |
||
| 196 | $file = sha1((string) $uploaded_file->getStream()) . '.' . $extension; |
||
| 197 | } |
||
| 198 | |||
| 199 | try { |
||
| 200 | $tree->mediaFilesystem()->writeStream($folder . $file, $uploaded_file->getStream()->detach()); |
||
| 201 | |||
| 202 | return $folder . $file; |
||
| 203 | } catch (RuntimeException | InvalidArgumentException $ex) { |
||
| 204 | FlashMessages::addMessage(I18N::translate('There was an error uploading your file.')); |
||
| 205 | |||
| 206 | return ''; |
||
| 207 | } |
||
| 241 |