| 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 |
||
| 98 | public function prepare(INotification $notification, string $languageCode): INotification { |
||
| 99 | if($notification->getApp() !== 'comments') { |
||
| 100 | throw new \InvalidArgumentException(); |
||
| 101 | } |
||
| 102 | try { |
||
| 103 | $comment = $this->commentsManager->get($notification->getObjectId()); |
||
| 104 | } catch(NotFoundException $e) { |
||
| 105 | // needs to be converted to InvalidArgumentException, otherwise none Notifications will be shown at all |
||
| 106 | throw new \InvalidArgumentException('Comment not found', 0, $e); |
||
| 107 | } |
||
| 108 | $l = $this->l10nFactory->get('comments', $languageCode); |
||
| 109 | $displayName = $comment->getActorId(); |
||
| 110 | $isDeletedActor = $comment->getActorType() === ICommentsManager::DELETED_USER; |
||
| 111 | if ($comment->getActorType() === 'users') { |
||
| 112 | $commenter = $this->userManager->get($comment->getActorId()); |
||
| 113 | if ($commenter instanceof IUser) { |
||
| 114 | $displayName = $commenter->getDisplayName(); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | switch ($notification->getSubject()) { |
||
| 119 | case 'mention': |
||
| 120 | $parameters = $notification->getSubjectParameters(); |
||
| 121 | if($parameters[0] !== 'files') { |
||
| 122 | throw new \InvalidArgumentException('Unsupported comment object'); |
||
| 123 | } |
||
| 124 | $userFolder = $this->rootFolder->getUserFolder($notification->getUser()); |
||
| 125 | $nodes = $userFolder->getById((int)$parameters[1]); |
||
| 126 | if(empty($nodes)) { |
||
| 127 | throw new AlreadyProcessedException(); |
||
| 128 | } |
||
| 129 | $node = $nodes[0]; |
||
| 130 | |||
| 131 | $path = rtrim($node->getPath(), '/'); |
||
| 132 | if (strpos($path, '/' . $notification->getUser() . '/files/') === 0) { |
||
| 133 | // Remove /user/files/... |
||
| 134 | $fullPath = $path; |
||
| 135 | list(,,, $path) = explode('/', $fullPath, 4); |
||
| 136 | } |
||
| 137 | $subjectParameters = [ |
||
| 138 | 'file' => [ |
||
| 139 | 'type' => 'file', |
||
| 140 | 'id' => $comment->getObjectId(), |
||
| 141 | 'name' => $node->getName(), |
||
| 142 | 'path' => $path, |
||
| 143 | 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $comment->getObjectId()]), |
||
| 144 | ], |
||
| 145 | ]; |
||
| 146 | |||
| 147 | if ($isDeletedActor) { |
||
| 148 | $subject = $l->t('You were mentioned on “{file}”, in a comment by a user that has since been deleted'); |
||
| 149 | } else { |
||
| 150 | $subject = $l->t('{user} mentioned you in a comment on “{file}”'); |
||
| 151 | $subjectParameters['user'] = [ |
||
| 152 | 'type' => 'user', |
||
| 153 | 'id' => $comment->getActorId(), |
||
| 154 | 'name' => $displayName, |
||
| 155 | ]; |
||
| 156 | } |
||
| 157 | list($message, $messageParameters) = $this->commentToRichMessage($comment); |
||
| 158 | $notification->setRichSubject($subject, $subjectParameters) |
||
| 159 | ->setParsedSubject($this->richToParsed($subject, $subjectParameters)) |
||
| 160 | ->setRichMessage($message, $messageParameters) |
||
| 161 | ->setParsedMessage($this->richToParsed($message, $messageParameters)) |
||
| 162 | ->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg'))) |
||
| 163 | ->setLink($this->url->linkToRouteAbsolute( |
||
| 164 | 'comments.Notifications.view', |
||
| 165 | ['id' => $comment->getId()]) |
||
| 166 | ); |
||
| 167 | |||
| 168 | return $notification; |
||
| 169 | break; |
||
|
|
|||
| 170 | |||
| 171 | default: |
||
| 172 | throw new \InvalidArgumentException('Invalid subject'); |
||
| 173 | } |
||
| 229 |
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.