| Conditions | 10 | 
| Paths | 5 | 
| Total Lines | 68 | 
| Code Lines | 37 | 
| 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  | 
            ||
| 69 | public function handle(ServerRequestInterface $request): ResponseInterface  | 
            ||
| 70 |     { | 
            ||
| 71 | $data_filesystem = Registry::filesystem()->data();  | 
            ||
| 72 | |||
| 73 | $params = (array) $request->getParsedBody();  | 
            ||
| 74 | |||
| 75 | $all_folders = $this->media_file_service->allMediaFolders($data_filesystem);  | 
            ||
| 76 | |||
| 77 |         foreach ($request->getUploadedFiles() as $key => $uploaded_file) { | 
            ||
| 78 | assert($uploaded_file instanceof UploadedFileInterface);  | 
            ||
| 79 |             if ($uploaded_file->getClientFilename() === '') { | 
            ||
| 80 | continue;  | 
            ||
| 81 | }  | 
            ||
| 82 |             if ($uploaded_file->getError() !== UPLOAD_ERR_OK) { | 
            ||
| 83 | FlashMessages::addMessage(Functions::fileUploadErrorText($uploaded_file->getError()), 'danger');  | 
            ||
| 84 | continue;  | 
            ||
| 85 | }  | 
            ||
| 86 | $key = substr($key, 9);  | 
            ||
| 87 | |||
| 88 | $folder = $params['folder' . $key];  | 
            ||
| 89 | $filename = $params['filename' . $key];  | 
            ||
| 90 | |||
| 91 | // If no filename specified, use the original filename.  | 
            ||
| 92 |             if ($filename === '') { | 
            ||
| 93 | $filename = $uploaded_file->getClientFilename();  | 
            ||
| 94 | }  | 
            ||
| 95 | |||
| 96 | // Validate the folder  | 
            ||
| 97 |             if (!$all_folders->contains($folder)) { | 
            ||
| 98 | break;  | 
            ||
| 99 | }  | 
            ||
| 100 | |||
| 101 | // Validate the filename.  | 
            ||
| 102 |             $filename = str_replace('\\', '/', $filename); | 
            ||
| 103 | $filename = trim($filename, '/');  | 
            ||
| 104 | |||
| 105 |             if (preg_match('/([:])/', $filename, $match)) { | 
            ||
| 106 | // Local media files cannot contain certain special characters, especially on MS Windows  | 
            ||
| 107 |                 FlashMessages::addMessage(I18N::translate('Filenames are not allowed to contain the character “%s”.', $match[1])); | 
            ||
| 108 | continue;  | 
            ||
| 109 | }  | 
            ||
| 110 | |||
| 111 |             if (preg_match('/(\.(php|pl|cgi|bash|sh|bat|exe|com|htm|html|shtml))$/i', $filename, $match)) { | 
            ||
| 112 | // Do not allow obvious script files.  | 
            ||
| 113 |                 FlashMessages::addMessage(I18N::translate('Filenames are not allowed to have the extension “%s”.', $match[1])); | 
            ||
| 114 | continue;  | 
            ||
| 115 | }  | 
            ||
| 116 | |||
| 117 | $path = $folder . $filename;  | 
            ||
| 118 | |||
| 119 |             if ($data_filesystem->has($path)) { | 
            ||
| 120 |                 FlashMessages::addMessage(I18N::translate('The file %s already exists. Use another filename.', $path, 'error')); | 
            ||
| 121 | continue;  | 
            ||
| 122 | }  | 
            ||
| 123 | |||
| 124 | // Now copy the file to the correct location.  | 
            ||
| 125 |             try { | 
            ||
| 126 | $data_filesystem->writeStream($path, $uploaded_file->getStream()->detach());  | 
            ||
| 127 |                 FlashMessages::addMessage(I18N::translate('The file %s has been uploaded.', Html::filename($path)), 'success'); | 
            ||
| 128 |                 Log::addMediaLog('Media file ' . $path . ' uploaded'); | 
            ||
| 129 |             } catch (Throwable $ex) { | 
            ||
| 130 |                 FlashMessages::addMessage(I18N::translate('There was an error uploading your file.') . '<br>' . e($ex->getMessage()), 'danger'); | 
            ||
| 131 | }  | 
            ||
| 132 | }  | 
            ||
| 133 | |||
| 134 | $url = route(UploadMediaPage::class);  | 
            ||
| 135 | |||
| 136 | return redirect($url);  | 
            ||
| 137 | }  | 
            ||
| 139 |