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