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