| Conditions | 9 |
| Paths | 49 |
| Total Lines | 55 |
| Code Lines | 31 |
| 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 |
||
| 78 | public function getPreview( |
||
| 79 | int $fileId, |
||
| 80 | int $x = 44, |
||
| 81 | int $y = 44 |
||
| 82 | ) { |
||
| 83 | |||
| 84 | if ($x === 0 || $y === 0) { |
||
| 85 | return new DataResponse([], Http::STATUS_BAD_REQUEST); |
||
| 86 | } |
||
| 87 | |||
| 88 | try { |
||
| 89 | $userFolder = $this->rootFolder->getUserFolder($this->userId); |
||
| 90 | /** @var Folder $trash */ |
||
| 91 | $trash = $userFolder->getParent()->get('files_trashbin/files'); |
||
| 92 | $trashFiles = $trash->getById($fileId); |
||
| 93 | |||
| 94 | if (empty($trashFiles)) { |
||
| 95 | throw new NotFoundException(); |
||
| 96 | } |
||
| 97 | |||
| 98 | $trashFile = array_pop($trashFiles); |
||
| 99 | |||
| 100 | if ($trashFile instanceof Folder) { |
||
| 101 | return new DataResponse([], Http::STATUS_BAD_REQUEST); |
||
| 102 | } |
||
| 103 | |||
| 104 | /* |
||
| 105 | * Files in the root of the trashbin are timetamped. |
||
| 106 | * So we have to strip that in order to properly detect the mimetype of the file. |
||
| 107 | */ |
||
| 108 | if ($trashFile->getParent()->getPath() === $trash->getPath()) { |
||
| 109 | /** @var File $trashFile */ |
||
| 110 | $fileName = $trashFile->getName(); |
||
| 111 | $i = strrpos($fileName, '.'); |
||
| 112 | if ($i !== false) { |
||
| 113 | $fileName = substr($fileName, 0, $i); |
||
| 114 | } |
||
| 115 | |||
| 116 | $mimeType = $this->mimeTypeDetector->detectPath($fileName); |
||
| 117 | } else { |
||
| 118 | $mimeType = $this->mimeTypeDetector->detectPath($trashFile->getName()); |
||
| 119 | } |
||
| 120 | |||
| 121 | $f = $this->previewManager->getPreview($trashFile, $x, $y, true, IPreview::MODE_FILL, $mimeType); |
||
|
|
|||
| 122 | $response = new Http\FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]); |
||
| 123 | |||
| 124 | // Cache previews for 24H |
||
| 125 | $response->cacheFor(3600 * 24); |
||
| 126 | return $response; |
||
| 127 | } catch (NotFoundException $e) { |
||
| 128 | return new DataResponse([], Http::STATUS_NOT_FOUND); |
||
| 129 | } catch (\InvalidArgumentException $e) { |
||
| 130 | return new DataResponse([], Http::STATUS_BAD_REQUEST); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: