Conditions | 8 |
Paths | 10 |
Total Lines | 72 |
Code Lines | 43 |
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 |
||
88 | public function prepare(INotification $notification, string $languageCode): INotification { |
||
89 | if ($notification->getApp() !== 'files_sharing') { |
||
90 | // Not my app => throw |
||
91 | throw new \InvalidArgumentException(); |
||
92 | } |
||
93 | |||
94 | // Read the language from the notification |
||
95 | $l = $this->factory->get('files_sharing', $languageCode); |
||
96 | |||
97 | switch ($notification->getSubject()) { |
||
98 | // Deal with known subjects |
||
99 | case 'remote_share': |
||
100 | $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); |
||
101 | |||
102 | $params = $notification->getSubjectParameters(); |
||
103 | if ($params[0] !== $params[1] && $params[1] !== null) { |
||
104 | $notification->setParsedSubject( |
||
105 | $l->t('You received "%3$s" as a remote share from %1$s (on behalf of %2$s)', $params) |
||
106 | ); |
||
107 | $notification->setRichSubject( |
||
108 | $l->t('You received {share} as a remote share from {user} (on behalf of {behalf})'), |
||
109 | [ |
||
110 | 'share' => [ |
||
111 | 'type' => 'pending-federated-share', |
||
112 | 'id' => $notification->getObjectId(), |
||
113 | 'name' => $params[2], |
||
114 | ], |
||
115 | 'user' => $this->createRemoteUser($params[0]), |
||
116 | 'behalf' => $this->createRemoteUser($params[1]), |
||
117 | ] |
||
118 | ); |
||
119 | } else { |
||
120 | $notification->setParsedSubject( |
||
121 | $l->t('You received "%3$s" as a remote share from %1$s', $params) |
||
122 | ); |
||
123 | $notification->setRichSubject( |
||
124 | $l->t('You received {share} as a remote share from {user}'), |
||
125 | [ |
||
126 | 'share' => [ |
||
127 | 'type' => 'pending-federated-share', |
||
128 | 'id' => $notification->getObjectId(), |
||
129 | 'name' => $params[2], |
||
130 | ], |
||
131 | 'user' => $this->createRemoteUser($params[0]), |
||
132 | ] |
||
133 | ); |
||
134 | } |
||
135 | |||
136 | // Deal with the actions for a known subject |
||
137 | foreach ($notification->getActions() as $action) { |
||
138 | switch ($action->getLabel()) { |
||
139 | case 'accept': |
||
140 | $action->setParsedLabel( |
||
141 | (string) $l->t('Accept') |
||
142 | ) |
||
143 | ->setPrimary(true); |
||
144 | break; |
||
145 | |||
146 | case 'decline': |
||
147 | $action->setParsedLabel( |
||
148 | (string) $l->t('Decline') |
||
149 | ); |
||
150 | break; |
||
151 | } |
||
152 | |||
153 | $notification->addParsedAction($action); |
||
154 | } |
||
155 | return $notification; |
||
156 | |||
157 | default: |
||
158 | // Unknown subject => Unknown notification => throw |
||
159 | throw new \InvalidArgumentException(); |
||
160 | } |
||
252 |