| Conditions | 10 | 
| Paths | 23 | 
| Total Lines | 75 | 
| Code Lines | 55 | 
| 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 | ||
| 75 | 	public function prepare(INotification $notification, $languageCode) { | ||
| 76 | 		if($notification->getApp() !== 'comments') { | ||
| 77 | throw new \InvalidArgumentException(); | ||
| 78 | } | ||
| 79 | 		try { | ||
| 80 | $comment = $this->commentsManager->get($notification->getObjectId()); | ||
| 81 | 		} catch(NotFoundException $e) { | ||
| 82 | // needs to be converted to InvalidArgumentException, otherwise none Notifications will be shown at all | ||
| 83 | 			throw new \InvalidArgumentException('Comment not found', 0, $e); | ||
| 84 | } | ||
| 85 | 		$l = $this->l10nFactory->get('comments', $languageCode); | ||
| 86 | $displayName = $comment->getActorId(); | ||
| 87 | $isDeletedActor = $comment->getActorType() === ICommentsManager::DELETED_USER; | ||
| 88 | 		if ($comment->getActorType() === 'users') { | ||
| 89 | $commenter = $this->userManager->get($comment->getActorId()); | ||
| 90 | 			if ($commenter instanceof IUser) { | ||
| 91 | $displayName = $commenter->getDisplayName(); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | 		switch ($notification->getSubject()) { | ||
| 96 | case 'mention': | ||
| 97 | $parameters = $notification->getSubjectParameters(); | ||
| 98 | 				if($parameters[0] !== 'files') { | ||
| 99 | 					throw new \InvalidArgumentException('Unsupported comment object'); | ||
| 100 | } | ||
| 101 | $userFolder = $this->rootFolder->getUserFolder($notification->getUser()); | ||
| 102 | $nodes = $userFolder->getById((int)$parameters[1]); | ||
| 103 | 				if(empty($nodes)) { | ||
| 104 | 					throw new \InvalidArgumentException('Cannot resolve file ID to node instance'); | ||
| 105 | } | ||
| 106 | $node = $nodes[0]; | ||
| 107 | |||
| 108 | $path = rtrim($node->getPath(), '/'); | ||
| 109 | 				if (strpos($path, '/' . $notification->getUser() . '/files/') === 0) { | ||
| 110 | // Remove /user/files/... | ||
| 111 | $fullPath = $path; | ||
| 112 | 					list(,,, $path) = explode('/', $fullPath, 4); | ||
| 113 | } | ||
| 114 | $subjectParameters = [ | ||
| 115 | 'file' => [ | ||
| 116 | 'type' => 'file', | ||
| 117 | 'id' => $comment->getObjectId(), | ||
| 118 | 'name' => $node->getName(), | ||
| 119 | 'path' => $path, | ||
| 120 | 						'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $comment->getObjectId()]), | ||
| 121 | ], | ||
| 122 | ]; | ||
| 123 | |||
| 124 | 				if ($isDeletedActor) { | ||
| 125 | 					$subject = $l->t('You were mentioned on “{file}”, in a comment by a user that has since been deleted'); | ||
| 126 | 				} else { | ||
| 127 | 					$subject = $l->t('{user} mentioned you in a comment on “{file}”'); | ||
| 128 | $subjectParameters['user'] = [ | ||
| 129 | 'type' => 'user', | ||
| 130 | 'id' => $comment->getActorId(), | ||
| 131 | 'name' => $displayName, | ||
| 132 | ]; | ||
| 133 | } | ||
| 134 | list($message, $messageParameters) = $this->commentToRichMessage($comment); | ||
| 135 | $notification->setRichSubject($subject, $subjectParameters) | ||
| 136 | ->setParsedSubject($this->richToParsed($subject, $subjectParameters)) | ||
| 137 | ->setRichMessage($message, $messageParameters) | ||
| 138 | ->setParsedMessage($this->richToParsed($message, $messageParameters)) | ||
| 139 | 					->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg'))) | ||
| 140 | ->setLink($this->url->linkToRouteAbsolute( | ||
| 141 | 'comments.Notifications.view', | ||
| 142 | ['id' => $comment->getId()]) | ||
| 143 | ); | ||
| 144 | |||
| 145 | return $notification; | ||
| 146 | break; | ||
|  | |||
| 147 | |||
| 148 | default: | ||
| 149 | 				throw new \InvalidArgumentException('Invalid subject'); | ||
| 150 | } | ||
| 206 | 
The
breakstatement is not necessary if it is preceded for example by areturnstatement:If you would like to keep this construct to be consistent with other
casestatements, you can safely mark this issue as a false-positive.