Conditions | 8 |
Paths | 28 |
Total Lines | 55 |
Code Lines | 34 |
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 |
||
36 | function get_blogpost($user, $guid, $lang) |
||
37 | { |
||
38 | $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user)); |
||
39 | if (!$user_entity) { |
||
40 | return "User was not found. Please try a different GUID, username, or email address"; |
||
41 | } |
||
42 | if (!$user_entity instanceof ElggUser) { |
||
43 | return "Invalid user. Please try a different GUID, username, or email address"; |
||
44 | } |
||
45 | |||
46 | $entity = get_entity($guid); |
||
47 | if (!isset($entity)) { |
||
48 | return "Blog was not found. Please try a different GUID"; |
||
49 | } |
||
50 | |||
51 | if (!elgg_is_logged_in()) { |
||
52 | login($user_entity); |
||
53 | } |
||
54 | |||
55 | $blog_posts = elgg_list_entities(array( |
||
56 | 'type' => 'object', |
||
57 | 'subtype' => 'blog', |
||
58 | 'guid' => $guid |
||
59 | )); |
||
60 | $blog_post = json_decode($blog_posts)[0]; |
||
61 | |||
62 | $blog_post->title = gc_explode_translation($blog_post->title, $lang); |
||
63 | $blog_post->description = gc_explode_translation($blog_post->description, $lang); |
||
64 | |||
65 | $likes = elgg_get_annotations(array( |
||
66 | 'guid' => $blog_post->guid, |
||
67 | 'annotation_name' => 'likes' |
||
68 | )); |
||
69 | $blog_post->likes = count($likes); |
||
70 | |||
71 | $liked = elgg_get_annotations(array( |
||
72 | 'guid' => $blog_post->guid, |
||
73 | 'annotation_owner_guid' => $user_entity->guid, |
||
74 | 'annotation_name' => 'likes' |
||
75 | )); |
||
76 | $blog_post->liked = count($liked) > 0; |
||
77 | |||
78 | $blog_post->comments = get_entity_comments($blog_post->guid); |
||
79 | |||
80 | $blog_post->userDetails = get_user_block($blog_post->owner_guid, $lang); |
||
81 | |||
82 | $group = get_entity($blog_post->container_guid); |
||
83 | $blog_post->group = gc_explode_translation($group->name, $lang); |
||
84 | |||
85 | if (is_callable(array($group, 'getURL'))) { |
||
86 | $blog_post->groupURL = $group->getURL(); |
||
87 | } |
||
88 | |||
89 | return $blog_post; |
||
90 | } |
||
91 | |||
164 |