Conditions | 39 |
Paths | 40 |
Total Lines | 167 |
Lines | 74 |
Ratio | 44.31 % |
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 |
||
194 | function collab_menu_block_handler($hook, $type, $menu, $params){ |
||
195 | //rearrange menu items |
||
196 | View Code Duplication | if(elgg_get_context() == 'groupSubPage' || elgg_in_context('group_profile')){ |
|
197 | |||
198 | elgg_unregister_menu_item('owner_block', 'activity'); |
||
199 | |||
200 | //turn owner_block menu into tabs |
||
201 | foreach ($menu as $key => $item){ |
||
202 | switch ($item->getName()) { |
||
203 | case 'discussion': |
||
204 | $item->setText(elgg_echo('gprofile:discussion')); |
||
205 | $item->setPriority('1'); |
||
206 | break; |
||
207 | case 'gcforums': |
||
208 | $item->setPriority('1'); |
||
209 | $item->setLinkClass('forums'); |
||
210 | break; |
||
211 | case 'file': |
||
212 | $item->setText(elgg_echo('gprofile:files')); |
||
213 | $item->setPriority('2'); |
||
214 | break; |
||
215 | case 'blog': |
||
216 | $item->setText(elgg_echo('gprofile:blogs')); |
||
217 | $item->setPriority('3'); |
||
218 | break; |
||
219 | case 'event_calendar': |
||
220 | $item->setText(elgg_echo('gprofile:events')); |
||
221 | $item->setPriority('4'); |
||
222 | break; |
||
223 | case 'etherpad': |
||
224 | $item->setPriority('5'); |
||
225 | break; |
||
226 | case 'pages': |
||
227 | $item->setText(elgg_echo('gprofile:pages')); |
||
228 | $item->setPriority('6'); |
||
229 | break; |
||
230 | case 'bookmarks': |
||
231 | $item->setText(elgg_echo('gprofile:bookmarks')); |
||
232 | $item->setPriority('7'); |
||
233 | break; |
||
234 | case 'activity': |
||
235 | $item->setText(elgg_echo('activity')); |
||
236 | $item->setPriority('8'); |
||
237 | $item->addItemClass('removeMe'); |
||
238 | break; |
||
239 | case 'questions': |
||
240 | $item->setText(elgg_echo('widget:questions:title')); |
||
241 | $item->setPriority('9'); |
||
242 | break; |
||
243 | case 'polls': |
||
244 | $item->setText(elgg_echo('gprofile:polls')); |
||
245 | $item->setPriority('10'); |
||
246 | break; |
||
247 | case 'tasks': |
||
248 | $item->setText(elgg_echo('gprofile:tasks')); |
||
249 | $item->setPriority('11'); |
||
250 | break; |
||
251 | case 'photos': |
||
252 | $item->setText(elgg_echo('gprofile:photos')); |
||
253 | $item->addItemClass('removeMe'); |
||
254 | $item->setPriority('12'); |
||
255 | break; |
||
256 | case 'photo_albums': |
||
257 | $item->setText(elgg_echo('gprofile:albumsCatch')); |
||
258 | $item->setPriority('13'); |
||
259 | break; |
||
260 | case 'ideas': |
||
261 | $item->setText(elgg_echo('gprofile:ideas')); |
||
262 | $item->setPriority('14'); |
||
263 | break; |
||
264 | case 'related_groups': |
||
265 | $item->setPriority('15'); |
||
266 | break; |
||
267 | } |
||
268 | } |
||
269 | } |
||
270 | |||
271 | if(elgg_get_context() == 'profile'){ |
||
272 | |||
273 | elgg_unregister_menu_item('owner_block', 'activity'); |
||
274 | |||
275 | //turn owner_block menu into tabs |
||
276 | foreach ($menu as $key => $item){ |
||
277 | switch ($item->getName()) { |
||
278 | case 'discussion': |
||
279 | $item->setText(elgg_echo('gprofile:discussion')); |
||
280 | $item->setPriority('1'); |
||
281 | break; |
||
282 | case 'file': |
||
283 | $item->setText(elgg_echo('gprofile:files')); |
||
284 | $item->setHref('#file'); |
||
285 | $item->setPriority('2'); |
||
286 | break; |
||
287 | case 'blog': |
||
288 | $item->setText(elgg_echo('gprofile:blogs')); |
||
289 | $item->setHref('#blog'); |
||
290 | $item->setPriority('3'); |
||
291 | break; |
||
292 | case 'event_calendar': |
||
293 | $item->setText(elgg_echo('gprofile:events')); |
||
294 | $item->setHref('#events'); |
||
295 | $item->setPriority('4'); |
||
296 | break; |
||
297 | case 'thewire': |
||
298 | //$item->setText(elgg_echo('The Wire')); |
||
299 | $item->setHref('#thewire'); |
||
300 | $item->setPriority('5'); |
||
301 | break; |
||
302 | case 'etherpad': |
||
303 | $item->setHref('#etherpad'); |
||
304 | $item->setPriority('6'); |
||
305 | break; |
||
306 | case 'pages': |
||
307 | $item->setText(elgg_echo('gprofile:pages')); |
||
308 | $item->setHref('#page_top'); |
||
309 | $item->setPriority('7'); |
||
310 | break; |
||
311 | case 'questions': |
||
312 | $item->setText(elgg_echo('widget:questions:title')); |
||
313 | $item->setHref('#question'); |
||
314 | $item->setPriority('8'); |
||
315 | break; |
||
316 | case 'bookmarks': |
||
317 | $item->setText(elgg_echo('gprofile:bookmarks')); |
||
318 | $item->setHref('#bookmarks'); |
||
319 | $item->setPriority('9'); |
||
320 | break; |
||
321 | case 'polls': |
||
322 | $item->setText(elgg_echo('gprofile:polls')); |
||
323 | $item->setHref('#poll'); |
||
324 | $item->setPriority('10'); |
||
325 | break; |
||
326 | case 'tasks': |
||
327 | $item->setText(elgg_echo('gprofile:tasks')); |
||
328 | $item->setHref('#task_top'); |
||
329 | $item->setPriority('11'); |
||
330 | break; |
||
331 | case 'photos': |
||
332 | $item->setText(elgg_echo('gprofile:photos')); |
||
333 | $item->addItemClass('removeMe'); |
||
334 | $item->setPriority('12'); |
||
335 | break; |
||
336 | case 'photo_albums': |
||
337 | $item->setText(elgg_echo('gprofile:albumsCatch')); |
||
338 | $item->setHref('#album'); |
||
339 | $item->setPriority('13'); |
||
340 | break; |
||
341 | case 'ideas': |
||
342 | $item->setText(elgg_echo('gprofile:ideas')); |
||
343 | $item->addItemClass('removeMe'); |
||
344 | $item->setPriority('14'); |
||
345 | break; |
||
346 | case 'orgs': |
||
347 | $item->setPriority('15'); |
||
348 | break; |
||
349 | case 'activity': |
||
350 | $item->setText(elgg_echo('activity')); |
||
351 | $item->setPriority('16'); |
||
352 | $item->addItemClass('removeMe'); |
||
353 | break; |
||
354 | case 'user_invite_from_profile': |
||
355 | $item->setPriority('17'); |
||
356 | break; |
||
357 | } |
||
358 | } |
||
359 | } |
||
360 | } |
||
361 | |||
417 |
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$x
would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.