Conditions | 14 |
Paths | 58 |
Total Lines | 89 |
Lines | 39 |
Ratio | 43.82 % |
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 |
||
112 | public function postAvatar($path) { |
||
113 | $userId = $this->userSession->getUser()->getUID(); |
||
114 | $files = $this->request->getUploadedFile('files'); |
||
115 | |||
116 | $headers = []; |
||
117 | if ($this->request->isUserAgent([Request::USER_AGENT_IE_8])) { |
||
118 | // due to upload iframe workaround, need to set content-type to text/plain |
||
119 | $headers['Content-Type'] = 'text/plain'; |
||
120 | } |
||
121 | |||
122 | if (isset($path)) { |
||
123 | $path = \stripslashes($path); |
||
124 | $node = $this->rootFolder->getUserFolder($userId)->get($path); |
||
125 | View Code Duplication | if (!($node instanceof \OCP\Files\File)) { |
|
|
|||
126 | return new DataResponse(['data' => ['message' => $this->l->t('Please select a file.')]], Http::STATUS_OK, $headers); |
||
127 | } |
||
128 | View Code Duplication | if ($node->getSize() > 20*1024*1024) { |
|
129 | return new DataResponse( |
||
130 | ['data' => ['message' => $this->l->t('File is too big')]], |
||
131 | Http::STATUS_BAD_REQUEST, |
||
132 | $headers |
||
133 | ); |
||
134 | } |
||
135 | $content = $node->getContent(); |
||
136 | } elseif ($files !== null) { |
||
137 | if ( |
||
138 | $files['error'][0] === 0 && |
||
139 | $this->isUploadFile($files['tmp_name'][0]) && |
||
140 | !Filesystem::isForbiddenFileOrDir($files['tmp_name'][0]) |
||
141 | ) { |
||
142 | View Code Duplication | if ($files['size'][0] > 20*1024*1024) { |
|
143 | return new DataResponse( |
||
144 | ['data' => ['message' => $this->l->t('File is too big')]], |
||
145 | Http::STATUS_BAD_REQUEST, |
||
146 | $headers |
||
147 | ); |
||
148 | } |
||
149 | $this->cache->set('avatar_upload', \file_get_contents($files['tmp_name'][0]), 7200); |
||
150 | $content = $this->cache->get('avatar_upload'); |
||
151 | \unlink($files['tmp_name'][0]); |
||
152 | View Code Duplication | } else { |
|
153 | return new DataResponse( |
||
154 | ['data' => ['message' => $this->l->t('Invalid file provided')]], |
||
155 | Http::STATUS_BAD_REQUEST, |
||
156 | $headers |
||
157 | ); |
||
158 | } |
||
159 | View Code Duplication | } else { |
|
160 | //Add imgfile |
||
161 | return new DataResponse( |
||
162 | ['data' => ['message' => $this->l->t('No image or file provided')]], |
||
163 | Http::STATUS_BAD_REQUEST, |
||
164 | $headers |
||
165 | ); |
||
166 | } |
||
167 | |||
168 | try { |
||
169 | $image = new \OC_Image(); |
||
170 | $image->loadFromData($content); |
||
171 | $image->fixOrientation(); |
||
172 | |||
173 | if ($image->valid()) { |
||
174 | $mimeType = $image->mimeType(); |
||
175 | if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') { |
||
176 | return new DataResponse( |
||
177 | ['data' => ['message' => $this->l->t('Unknown filetype')]], |
||
178 | Http::STATUS_OK, |
||
179 | $headers |
||
180 | ); |
||
181 | } |
||
182 | |||
183 | $this->cache->set('tmpAvatar', $image->data(), 7200); |
||
184 | return new DataResponse( |
||
185 | ['data' => 'notsquare'], |
||
186 | Http::STATUS_OK, |
||
187 | $headers |
||
188 | ); |
||
189 | View Code Duplication | } else { |
|
190 | return new DataResponse( |
||
191 | ['data' => ['message' => $this->l->t('Invalid image')]], |
||
192 | Http::STATUS_OK, |
||
193 | $headers |
||
194 | ); |
||
195 | } |
||
196 | } catch (\Exception $e) { |
||
197 | $this->logger->logException($e, ['app' => 'core']); |
||
198 | return new DataResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK, $headers); |
||
199 | } |
||
200 | } |
||
201 | |||
298 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.