Conditions | 29 |
Paths | > 20000 |
Total Lines | 146 |
Code Lines | 99 |
Lines | 30 |
Ratio | 20.55 % |
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 |
||
50 | function get_opportunity($user, $guid, $lang) |
||
51 | { |
||
52 | $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user)); |
||
53 | if (!$user_entity) { |
||
54 | return "User was not found. Please try a different GUID, username, or email address"; |
||
55 | } |
||
56 | if (!$user_entity instanceof ElggUser) { |
||
57 | return "Invalid user. Please try a different GUID, username, or email address"; |
||
58 | } |
||
59 | |||
60 | if (!elgg_is_logged_in()) { |
||
61 | login($user_entity); |
||
62 | } |
||
63 | |||
64 | $entity = get_entity($guid); |
||
65 | if (!$entity) { |
||
66 | return "Opportunity was not found. Please try a different GUID"; |
||
67 | } |
||
68 | if (!elgg_instanceof($entity, 'object', 'mission')) { |
||
69 | return "Invalid opportunity. Please try a different GUID"; |
||
70 | } |
||
71 | |||
72 | |||
73 | |||
74 | $opportunities = elgg_list_entities(array( |
||
75 | 'type' => 'object', |
||
76 | 'subtype' => 'mission', |
||
77 | 'guid' => $guid |
||
78 | )); |
||
79 | $opportunity = json_decode($opportunities)[0]; |
||
80 | |||
81 | $opportunity->title = gc_explode_translation($opportunity->title, $lang); |
||
82 | |||
83 | $likes = elgg_get_annotations(array( |
||
84 | 'guid' => $opportunity->guid, |
||
85 | 'annotation_name' => 'likes' |
||
86 | )); |
||
87 | $opportunity->likes = count($likes); |
||
88 | |||
89 | $liked = elgg_get_annotations(array( |
||
90 | 'guid' => $opportunity->guid, |
||
91 | 'annotation_owner_guid' => $user_entity->guid, |
||
92 | 'annotation_name' => 'likes' |
||
93 | )); |
||
94 | $opportunity->liked = count($liked) > 0; |
||
95 | |||
96 | $opportunity->comments = get_entity_comments($opportunity->guid); |
||
97 | |||
98 | $opportunity->userDetails = get_user_block($opportunity->owner_guid, $lang); |
||
99 | $opportunity->description = gc_explode_translation($opportunity->description, $lang); |
||
100 | |||
101 | $opportunityObj = get_entity($opportunity->guid); |
||
102 | $opportunity->jobtype = elgg_echo($opportunityObj->job_type); |
||
103 | $opportunity->roletype = elgg_echo($opportunityObj->role_type); |
||
104 | //$opportunity->programArea = elgg_echo('missions:program_area') . ": " . elgg_echo($opportunityObj->program_area); //This should work and translate to user lang but doesnt |
||
105 | $opportunity->programArea = elgg_echo($opportunityObj->program_area); |
||
106 | $opportunity->numOpportunities = $opportunityObj->number; |
||
107 | $opportunity->idealStart = $opportunityObj->start_date; |
||
108 | $opportunity->idealComplete = $opportunityObj->complete_date; |
||
109 | $opportunity->deadline = $opportunityObj->deadline; |
||
110 | $opportunity->oppVirtual = $opportunityObj->remotely; |
||
111 | $opportunity->oppOnlyIn = $opportunityObj->openess; |
||
112 | $opportunity->location = elgg_echo($opportunityObj->location); |
||
113 | $opportunity->security = elgg_echo($opportunityObj->security); |
||
114 | $opportunity->skills = $opportunityObj->key_skills; |
||
115 | //$opportunity->participants = $opportunityObj->; |
||
116 | //$opportunity->applicants = $opportunityObj->; |
||
117 | $opportunity->timezone = elgg_echo($opportunityObj->timezone); |
||
118 | $opportunity->timecommitment = $opportunityObj->time_commitment; |
||
119 | $opportunity->department = $opportunityObj->department; |
||
120 | |||
121 | //Language metadata |
||
122 | $unpacked_array = mm_unpack_mission($opportunityObj); |
||
123 | $unpacked_language = ''; |
||
124 | View Code Duplication | if (! empty($unpacked_array['lwc_english']) || ! empty($unpacked_array['lwc_french'])) { |
|
125 | $unpacked_language .= '<b>' . elgg_echo('missions:written_comprehension') . ': </b>'; |
||
126 | if (! empty($unpacked_array['lwc_english'])) { |
||
127 | $unpacked_language .= '<span name="mission-lwc-english">' . elgg_echo('missions:formatted:english', array($unpacked_array['lwc_english'])) . '</span> '; |
||
128 | } |
||
129 | if (! empty($unpacked_array['lwc_french'])) { |
||
130 | $unpacked_language .= '<span name="mission-lwc-french">' . elgg_echo('missions:formatted:french', array($unpacked_array['lwc_french'])) . '</span>'; |
||
131 | } |
||
132 | $unpacked_language .= '<br>'; |
||
133 | } |
||
134 | View Code Duplication | if (! empty($unpacked_array['lwe_english']) || ! empty($unpacked_array['lwe_french'])) { |
|
135 | $unpacked_language .= '<b>' . elgg_echo('missions:written_expression') . ': </b>'; |
||
136 | if (! empty($unpacked_array['lwe_english'])) { |
||
137 | $unpacked_language .= '<span name="mission-lwe-english">' . elgg_echo('missions:formatted:english', array($unpacked_array['lwe_english'])) . '</span> '; |
||
138 | } |
||
139 | if (! empty($unpacked_array['lwe_french'])) { |
||
140 | $unpacked_language .= '<span name="mission-lwe-french">' . elgg_echo('missions:formatted:french', array($unpacked_array['lwe_french'])) . '</span>'; |
||
141 | } |
||
142 | $unpacked_language .= '<br>'; |
||
143 | } |
||
144 | View Code Duplication | if (! empty($unpacked_array['lop_english']) || ! empty($unpacked_array['lop_french'])) { |
|
145 | $unpacked_language .= '<b>' . elgg_echo('missions:oral_proficiency') . ': </b>'; |
||
146 | if (! empty($unpacked_array['lop_english'])) { |
||
147 | $unpacked_language .= '<span name="mission-lop-english">' . elgg_echo('missions:formatted:english', array($unpacked_array['lop_english'])) . '</span> '; |
||
148 | } |
||
149 | if (! empty($unpacked_array['lop_french'])) { |
||
150 | $unpacked_language .= '<span name="mission-lop-french">' . elgg_echo('missions:formatted:french', array($unpacked_array['lop_french'])) . '</span>'; |
||
151 | } |
||
152 | $unpacked_language .= '<br>'; |
||
153 | } |
||
154 | if (empty($unpacked_language)) { |
||
155 | $unpacked_language = '<span name="no-languages">' . elgg_echo('missions:none_required') . '</span>'; |
||
156 | } |
||
157 | $opportunity->languageRequirements = $unpacked_language; |
||
158 | |||
159 | //scheduling metadata |
||
160 | $unpacked_time = ''; |
||
161 | if ($opportunityObj->mon_start) { |
||
162 | $unpacked_time .= '<b>' . elgg_echo('missions:mon') . ': </b>'; |
||
163 | $unpacked_time .= $opportunityObj->mon_start . elgg_echo('missions:to') . $opportunityObj->mon_duration . '<br>'; |
||
164 | } |
||
165 | if ($opportunityObj->tue_start) { |
||
166 | $unpacked_time .= '<b>' . elgg_echo('missions:tue') . ': </b>'; |
||
167 | $unpacked_time .= '<span name="mission-tue-start">' . $opportunityObj->tue_start . '</span>' . elgg_echo('missions:to') . '<span name="mission-tue-duration">' . $opportunityObj->tue_duration . '</span><br>'; |
||
168 | } |
||
169 | if ($opportunityObj->wed_start) { |
||
170 | $unpacked_time .= '<b>' . elgg_echo('missions:wed') . ': </b>'; |
||
171 | $unpacked_time .= $opportunityObj->wed_start . elgg_echo('missions:to') . $opportunityObj->wed_duration . '<br>'; |
||
172 | } |
||
173 | if ($opportunityObj->thu_start) { |
||
174 | $unpacked_time .= '<b>' . elgg_echo('missions:thu') . ': </b>'; |
||
175 | $unpacked_time .= '<span name="mission-thu-start">' . $opportunityObj->thu_start . '</span>' . elgg_echo('missions:to') . '<span name="mission-thu-duration">' . $opportunityObj->thu_duration . '</span><br>'; |
||
176 | } |
||
177 | if ($opportunityObj->fri_start) { |
||
178 | $unpacked_time .= '<b>' . elgg_echo('missions:fri') . ': </b>'; |
||
179 | $unpacked_time .= '<span name="mission-fri-start">' . $opportunityObj->fri_start . '</span>' . elgg_echo('missions:to') . '<span name="mission-fri-duration">' . $opportunityObj->fri_duration . '</span><br>'; |
||
180 | } |
||
181 | if ($opportunityObj->sat_start) { |
||
182 | $unpacked_time .= '<b>' . elgg_echo('missions:sat') . ': </b>'; |
||
183 | $unpacked_time .= '<span name="mission-sat-start">' . $opportunityObj->sat_start . '</span>' . elgg_echo('missions:to') . '<span name="mission-sat-duration">' . $opportunityObj->sat_duration . '</span><br>'; |
||
184 | } |
||
185 | if ($opportunityObj->sun_start) { |
||
186 | $unpacked_time .= '<b>' . elgg_echo('missions:sun') . ': </b>'; |
||
187 | $unpacked_time .= '<span name="mission-sun-start">' . $opportunityObj->sun_start . '</span>' . elgg_echo('missions:to') . '<span name="mission-sun-duration">' . $opportunityObj->sun_duration . '</span><br>'; |
||
188 | } |
||
189 | if (empty($unpacked_time)) { |
||
190 | $unpacked_time = '<span name="no-times">' . elgg_echo('missions:none_required') . '</span>'; |
||
191 | } |
||
192 | $opportunity->schedulingRequirements = $unpacked_time; |
||
193 | |||
194 | return $opportunity; |
||
195 | } |
||
196 | |||
316 | } |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: