| Conditions | 11 |
| Paths | 34 |
| Total Lines | 46 |
| Code Lines | 27 |
| 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 |
||
| 27 | public function updateFileRequest(Request $request) |
||
| 28 | { |
||
| 29 | $input = $request->getContent(); |
||
| 30 | $server = $request->server; |
||
| 31 | $contentType = $server->get('CONTENT_TYPE', $server->get('HTTP_CONTENT_TYPE')); |
||
| 32 | $data = []; |
||
| 33 | |||
| 34 | // grab multipart boundary from content type header |
||
| 35 | preg_match('/boundary=(.*)$/', $contentType, $matches); |
||
| 36 | |||
| 37 | // content type is probably regular form-encoded |
||
| 38 | if (!count($matches)) { |
||
| 39 | // Check if content is binary, convert to file upload |
||
| 40 | if ($request->files->count() == 0 && preg_match('~[^\x20-\x7E\t\r\n]~', $input) > 0) { |
||
| 41 | $file = $this->extractFileFromString($input); |
||
|
|
|||
| 42 | $request->files->add([$file]); |
||
| 43 | } |
||
| 44 | return $request; |
||
| 45 | } |
||
| 46 | |||
| 47 | $contentBlocks = preg_split("/-+$matches[1]/", $input); |
||
| 48 | |||
| 49 | // determine content blocks usage |
||
| 50 | foreach ($contentBlocks as $contentBlock) { |
||
| 51 | if (empty($contentBlock)) { |
||
| 52 | continue; |
||
| 53 | } |
||
| 54 | preg_match('/name=\"(.*?)\"[^"]/i', $contentBlock, $matches); |
||
| 55 | $name = isset($matches[1]) ? $matches[1] : ''; |
||
| 56 | if ('upload' !== $name) { |
||
| 57 | preg_match('/name=\"([^\"]*)\"[\n|\r]+([^\n\r].*)?\r$/s', $contentBlock, $matches); |
||
| 58 | $contentBlock = array_key_exists(2, $matches) ? $matches[2]: $contentBlock; |
||
| 59 | } |
||
| 60 | $data[$name] = $contentBlock; |
||
| 61 | } |
||
| 62 | |||
| 63 | if (array_key_exists('metadata', $data)) { |
||
| 64 | $request->request->set('metadata', $data['metadata']); |
||
| 65 | } |
||
| 66 | if (array_key_exists('upload', $data)) { |
||
| 67 | $file = $this->extractFileFromString($data['upload']); |
||
| 68 | $request->files->add([$file]); |
||
| 69 | } |
||
| 70 | |||
| 71 | return $request; |
||
| 72 | } |
||
| 73 | |||
| 120 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.