Conditions | 21 |
Paths | 34 |
Total Lines | 117 |
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 |
||
115 | function mentions_notification_handler($event, $event_type, $object) { |
||
116 | // excludes messages - otherwise an endless loop of notifications occur! |
||
117 | if (elgg_instanceof($object, 'object', 'messages') || elgg_instanceof($object, 'object', 'message')) { |
||
118 | return; |
||
119 | } |
||
120 | |||
121 | $type = $object->getType(); |
||
122 | $subtype = $object->getSubtype(); |
||
123 | $owner = $object->getOwnerEntity(); |
||
124 | |||
125 | $fields = array( |
||
126 | 'title', 'description', 'value' |
||
127 | ); |
||
128 | |||
129 | // store the guids of notified users so they only get one notification per creation event |
||
130 | $notified_guids = array(); |
||
131 | |||
132 | foreach ($fields as $field) { |
||
133 | $content = $object->$field; |
||
134 | // it's ok in this case if 0 matches == FALSE |
||
135 | if (preg_match_all(mentions_get_regex(), $content, $matches)) { |
||
136 | // match against the 2nd index since the first is everything |
||
137 | foreach ($matches[1] as $username) { |
||
138 | |||
139 | $user = get_user_by_username($username); |
||
140 | |||
141 | // check for trailing punctuation caught by the regex |
||
142 | if (!$user && substr($username, -1) == '.') { |
||
143 | $user = get_user_by_username(rtrim($username, '.')); |
||
144 | } |
||
145 | |||
146 | if (!$user) { |
||
147 | continue; |
||
148 | } |
||
149 | |||
150 | // user must have access to view object/annotation |
||
151 | if ($type == 'annotation') { |
||
152 | $annotated_entity = $object->getEntity(); |
||
153 | if (!$annotated_entity || !has_access_to_entity($annotated_entity, $user)) { |
||
154 | continue; |
||
155 | } |
||
156 | } else { |
||
157 | if (!has_access_to_entity($object, $user)) { |
||
158 | continue; |
||
159 | } |
||
160 | } |
||
161 | |||
162 | if (!in_array($user->getGUID(), $notified_guids)) { |
||
163 | $notified_guids[] = $user->getGUID(); |
||
164 | |||
165 | // if they haven't set the notification status default to sending. |
||
166 | // Private settings are stored as strings so we check against "0" |
||
167 | $notification_setting = elgg_get_plugin_user_setting('notify', $user->getGUID(), 'mentions'); |
||
168 | if ($notification_setting === "0") { |
||
169 | continue; |
||
170 | } |
||
171 | |||
172 | $link = $object->getURL(); |
||
173 | $type_key = "mentions:notification_types:$type:$subtype"; |
||
174 | |||
175 | $type_str = array(); |
||
176 | |||
177 | $type_str[0] = elgg_echo($type_key, array(), 'en'); |
||
178 | $type_str[1] = elgg_echo($type_key, array(), 'fr'); |
||
179 | if ($type_str == $type_key) { |
||
180 | // plugins can add to the list of mention objects by defining |
||
181 | // the language string 'mentions:notification_types:<type>:<subtype>' |
||
182 | continue; |
||
183 | } |
||
184 | $subject = elgg_echo('mentions:notification:subject', array($owner->name, $type_str[0]), 'en'); |
||
185 | |||
186 | $subject .= ' / ' . elgg_echo('mentions:notification:subject', array($owner->name, $type_str[1]), 'fr'); |
||
187 | |||
188 | $body = elgg_echo('mentions:notification:body', array( |
||
189 | $owner->name, |
||
190 | $type_str[0], |
||
191 | $link, |
||
192 | ), 'en'); |
||
193 | |||
194 | $body .= elgg_echo('mentions:notification:body', array( |
||
195 | $owner->name, |
||
196 | $type_str[1], |
||
197 | $link, |
||
198 | ), 'fr'); |
||
199 | |||
200 | $params = array( |
||
201 | 'object' => $object, |
||
202 | 'action' => 'mention', |
||
203 | ); |
||
204 | |||
205 | // cyu - trigger the hook (we are re-routing the mentions to cp_notifications if it is activated) |
||
206 | // please don't trigger if subtype is thewire |
||
207 | if (elgg_is_active_plugin('cp_notifications') && strcmp($object->getSubtype(),'thewire') != 0) { |
||
208 | |||
209 | if (strcmp($object->getSubtype(),'comment') == 0 || strcmp($object->getSubtype(),'thewire') == 0 || strcmp($object->getSubtype(),'discussion_reply') == 0) |
||
210 | $container = $object->getContainerEntity(); |
||
211 | else |
||
212 | $container = $object; |
||
213 | |||
214 | $message = array( |
||
215 | 'cp_author' => $owner->name, |
||
216 | 'cp_content' => $container->title, |
||
217 | 'cp_link' => $link, |
||
218 | 'cp_msg_type' => 'cp_content_mention', |
||
219 | 'cp_to_user' => $user, |
||
220 | 'cp_content_desc' => $object->description, |
||
221 | ); |
||
222 | elgg_trigger_plugin_hook('cp_overwrite_notification','all',$message); |
||
223 | } else { |
||
224 | |||
225 | notify_user($user->getGUID(), $owner->getGUID(), utf8_encode($subject), utf8_encode($body), $params); |
||
226 | } |
||
227 | } |
||
228 | } |
||
229 | } |
||
230 | } |
||
231 | } |
||
232 | |||
251 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.