Conditions | 15 |
Paths | 116 |
Total Lines | 74 |
Lines | 28 |
Ratio | 37.84 % |
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 |
||
210 | public function generatePaperHiveDocument($dir, $bookID) { |
||
211 | // Try to get the document |
||
212 | $paperHiveObjectString = $this->fetchDocument($bookID); |
||
213 | View Code Duplication | if ($paperHiveObjectString === false) { |
|
214 | $message = (string)$this->l->t('Problem connecting to PaperHive.'); |
||
215 | return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
||
216 | } |
||
217 | $paperHiveObject = json_decode($paperHiveObjectString, true); |
||
218 | |||
219 | // Check if correct response has been returned |
||
220 | View Code Duplication | if (json_last_error() != JSON_ERROR_NONE) { |
|
221 | $message = (string)$this->l->t('Received wrong response from PaperHive.'); |
||
222 | return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
||
223 | } |
||
224 | |||
225 | // Check if document is found |
||
226 | if (!(isset($paperHiveObject['metadata']) && isset($paperHiveObject['metadata']['title']))) { |
||
227 | $message = (string)$this->l->t('Document with this BookID cannot be found'); |
||
228 | return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
||
229 | } |
||
230 | |||
231 | // Try fetching discussions |
||
232 | $paperHiveDiscussionsString = $this->fetchDiscussions($bookID); |
||
233 | View Code Duplication | if ($paperHiveDiscussionsString === false) { |
|
234 | $message = (string)$this->l->t('Problem connecting to PaperHive.'); |
||
235 | return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
||
236 | } |
||
237 | $paperHiveDiscussions = json_decode($paperHiveDiscussionsString, true); |
||
238 | $discussionCount = -1; |
||
239 | View Code Duplication | if (json_last_error() === JSON_ERROR_NONE && isset($paperHiveDiscussions['discussions'])) { |
|
240 | $discussionCount = count($paperHiveDiscussions['discussions']); |
||
241 | } |
||
242 | |||
243 | // Save the file |
||
244 | $title = $paperHiveObject['metadata']['title']; |
||
245 | $filename = $title . $this->paperhive_file_extension; |
||
246 | if($dir == '/') { |
||
247 | $path = $dir . $filename; |
||
248 | } else { |
||
249 | $path = $dir . '/' . $filename; |
||
250 | } |
||
251 | |||
252 | $exists = $this->view->file_exists($path); |
||
253 | View Code Duplication | if ($exists) { |
|
254 | $message = (string) $this->l->t('The file already exists.'); |
||
255 | return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
||
256 | } |
||
257 | |||
258 | try { |
||
259 | $created = $this->view->touch($path); |
||
260 | View Code Duplication | if(!$created) { |
|
261 | $message = (string) $this->l->t('Could not save document.'); |
||
262 | return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
||
263 | } |
||
264 | |||
265 | $fileInfo = $this->view->getFileInfo($path); |
||
266 | $inserted = $this->paperHiveMetadata->insertBookID($fileInfo['fileid'], $bookID); |
||
267 | View Code Duplication | if(!$inserted) { |
|
268 | $this->view->unlink($path); |
||
269 | $message = (string) $this->l->t('Could not save document metadata.'); |
||
270 | return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
||
271 | } |
||
272 | } catch (LockedException $e) { |
||
273 | $message = (string) $this->l->t('The file is locked.'); |
||
274 | return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
||
275 | } catch (ForbiddenException $e) { |
||
276 | return new DataResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST); |
||
277 | } catch (\Exception $e) { |
||
278 | $message = (string)$this->l->t('An internal server error occurred.'); |
||
279 | return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
||
280 | } |
||
281 | |||
282 | return new DataResponse(['path' => $path, 'filename' => $title, 'extension' => $this->paperhive_file_extension, 'discussionCount' => $discussionCount], Http::STATUS_OK); |
||
283 | } |
||
284 | |||
304 |
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.