Conditions | 41 |
Paths | > 20000 |
Total Lines | 266 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
128 | function mm_api_export_entities($type, $subtype = false, $guid = null, |
||
129 | $since = null, $before = null, $limit = null, $resume = null, $sort = false, |
||
130 | $omit = null, $countRows = false) { |
||
131 | _elgg_services()->db->establishLink('api_exporter'); |
||
132 | $dbprefix = elgg_get_config('dbprefix'); |
||
133 | $dblink = _elgg_services()->db->getLink('read'); |
||
134 | function dismount($object) { |
||
135 | $reflectionClass = new ReflectionClass(get_class($object)); |
||
136 | $array = array(); |
||
137 | foreach ($reflectionClass->getProperties() as $property) { |
||
138 | $property->setAccessible(true); |
||
139 | $array[$property->getName()] = $property->getValue($object); |
||
140 | $property->setAccessible(false); |
||
141 | } |
||
142 | return $array; |
||
143 | } |
||
144 | global $in_array; |
||
145 | $in_array = false; |
||
146 | function outVal($val, $start_with_comma=true) { |
||
147 | global $in_array; |
||
148 | $value = ($val->meta_type === 'integer') ? |
||
149 | json_encode(intval($val->meta_value)) : json_encode($val->meta_value); |
||
150 | if (($in_array !== false) && ($in_array == $val->meta_name)) { |
||
151 | return (($start_with_comma) ? ',' : '').$value; |
||
152 | } |
||
153 | $ret = ''; |
||
154 | if (($in_array !== false) && (($in_array != $val->meta_name) || ($val->arr == 0))) { |
||
155 | $ret .= ']'; |
||
156 | $in_array = false; |
||
157 | } |
||
158 | if ($start_with_comma) $ret .= ','; |
||
159 | $ret .= '"' . $val->meta_name . '":'; |
||
160 | if ($val->arr != 0) { |
||
161 | $ret .= '['; |
||
162 | $in_array = $val->meta_name; |
||
163 | } |
||
164 | $ret .= $value; |
||
165 | return $ret; |
||
166 | } |
||
167 | function finalizeEntity($uguid) { |
||
168 | global $in_array; |
||
169 | $dbprefix = elgg_get_config('dbprefix'); |
||
170 | $dblink = _elgg_services()->db->getLink('read'); |
||
171 | |||
172 | if ($uguid > 0) { |
||
173 | if ($in_array) { |
||
174 | yield ']'; |
||
175 | $in_array = false; |
||
176 | } |
||
177 | |||
178 | $options = array('guid' => $uguid, 'limit' => 0); |
||
179 | $annotations = elgg_get_annotations($options); |
||
180 | |||
181 | if ($annotations) { |
||
182 | $data = [ 'annotations' => []]; |
||
183 | foreach ($annotations as $v) { |
||
184 | if (!isset($data['annotations'][$v->name])) { |
||
185 | $data['annotations'][$v->name] = []; |
||
186 | }; |
||
187 | $data['annotations'][$v->name][] = dismount($v); |
||
188 | } |
||
189 | yield ','.substr(json_encode($data), 1, -1); |
||
190 | } |
||
191 | yield ',"relationships":['; |
||
192 | |||
193 | $relstart = false; |
||
194 | $reltable = "{$dbprefix}entity_relationships"; |
||
195 | $relationships = mysql_unbuffered_query( |
||
196 | "SELECT * from $reltable where guid_one = " . |
||
197 | mysql_escape_string($uguid) . |
||
198 | ' OR guid_two = ' . mysql_escape_string($uguid), |
||
199 | $dblink |
||
200 | ); |
||
201 | while ($v = mysql_fetch_object($relationships)) { |
||
202 | yield (($relstart) ? ',' : '') . json_encode(array( |
||
203 | 'direction' => ($v->guid_one == $uguid) ? 'OUT' : 'IN', |
||
204 | 'time_created' => $v->time_created, |
||
205 | 'id' => $v->id, |
||
206 | 'relationship' => $v->relationship, |
||
207 | 'entity' => ($v->guid_one == $uguid) ? $v->guid_two : $v->guid_one, |
||
208 | )); |
||
209 | $relstart = true; |
||
210 | } |
||
211 | mysql_free_result($relationships); |
||
212 | yield ']}'; |
||
213 | } |
||
214 | } |
||
215 | |||
216 | // Get all subtypes |
||
217 | global $subtypes; |
||
218 | $subtypes = []; |
||
219 | $subtype_results = mysql_unbuffered_query( |
||
220 | "select id, subtype from {$dbprefix}entity_subtypes", |
||
221 | $dblink |
||
222 | ); |
||
223 | while ($row = mysql_fetch_object($subtype_results)) { |
||
224 | $subtypes["i_{$row->id}"] = $row->subtype; |
||
225 | $subtypes["s_{$row->subtype}"] = $row->id; |
||
226 | } |
||
227 | mysql_free_result($subtype_results); |
||
228 | |||
229 | $where = ['a.enabled = "yes"']; |
||
230 | if ($type !== 'export') { |
||
231 | $where[] = 'a.type = "' . mysql_escape_string($type) . '"'; |
||
232 | } |
||
233 | if ($subtype !== false) { |
||
234 | $where[] = 'a.subtype = ' . (($subtypes["s_$subtype"]) ? $subtypes["s_$subtype"] : -1); |
||
235 | } |
||
236 | |||
237 | if (!is_null($guid) && is_numeric($guid)) { |
||
238 | $where[] = 'a.guid = ' . mysql_escape_string(intval($guid)); |
||
239 | } |
||
240 | if (is_numeric($since)) { |
||
241 | $where[] = 'a.time_updated > ' . mysql_escape_string($since); |
||
242 | } |
||
243 | if (is_numeric($before)) { |
||
244 | $where[] = 'a.time_updated < ' . mysql_escape_string($before); |
||
245 | } |
||
246 | if (!is_null($omit)) { |
||
247 | $omitGuids = explode(',', $omit); |
||
248 | if (count($omitGuids) > 0) { |
||
249 | $ogs = []; |
||
250 | foreach ($omitGuids as $og) { |
||
251 | $ogs[] = mysql_escape_string(intval($og)); |
||
252 | } |
||
253 | $where[] = 'a.guid NOT IN ('.implode(',', $ogs).')'; |
||
254 | } |
||
255 | } |
||
256 | |||
257 | $where_sql = ''; |
||
258 | if (count($where) > 0) { |
||
259 | $where_sql = 'WHERE ' . implode(' AND ', $where); |
||
260 | } |
||
261 | if ($sort) { |
||
262 | $sort_sql = 'ORDER BY a.time_updated ASC'; |
||
263 | } |
||
264 | try { |
||
265 | $sql = " |
||
266 | SELECT |
||
267 | a.guid, |
||
268 | a.type, |
||
269 | a.subtype, |
||
270 | a.owner_guid, |
||
271 | a.site_guid, |
||
272 | a.container_guid, |
||
273 | a.time_created, |
||
274 | a.time_updated, |
||
275 | a.access_id, |
||
276 | a.enabled, |
||
277 | a.last_action, |
||
278 | o.title, |
||
279 | o.description, |
||
280 | u.name, |
||
281 | u.username, |
||
282 | u.admin, |
||
283 | u.banned, |
||
284 | u.language, |
||
285 | u.last_action AS user_last_action, |
||
286 | u.prev_last_action, |
||
287 | u.last_login, |
||
288 | u.prev_last_login, |
||
289 | g.name as group_name, |
||
290 | g.description as group_description, |
||
291 | ( |
||
292 | SELECT |
||
293 | COUNT(name_id) |
||
294 | FROM |
||
295 | elggmetadata |
||
296 | WHERE |
||
297 | entity_guid = a.guid |
||
298 | AND name_id = b.name_id |
||
299 | ) > 1 as arr, |
||
300 | b.value_type as meta_type, |
||
301 | c.string as meta_name, |
||
302 | d.string as meta_value |
||
303 | FROM |
||
304 | {$dbprefix}entities a |
||
305 | LEFT JOIN {$dbprefix}objects_entity o on o.guid = a.guid |
||
306 | LEFT JOIN {$dbprefix}users_entity u ON u.guid = a.guid |
||
307 | LEFT JOIN {$dbprefix}groups_entity g ON g.guid = a.guid |
||
308 | LEFT JOIN {$dbprefix}metadata b ON b.entity_guid = a.guid |
||
309 | LEFT JOIN {$dbprefix}metastrings c ON c.id = b.name_id |
||
310 | LEFT JOIN {$dbprefix}metastrings d ON d.id = b.value_id |
||
311 | $where_sql |
||
312 | $sort_sql"; |
||
313 | |||
314 | if ($countRows) { |
||
315 | yield get_data( |
||
316 | "select count(guid) c from {$dbprefix}entities a $where_sql" |
||
317 | )[0]->c; |
||
318 | } else yield 0; |
||
319 | |||
320 | $entity_data = mysql_unbuffered_query( |
||
321 | $sql, |
||
322 | _elgg_services()->db->getLink('api_exporter') |
||
323 | ); |
||
324 | $emit = !is_numeric($resume); |
||
325 | $max = (is_numeric($limit) && ($limit > 0)) ? $limit : false; |
||
326 | $count = 0; |
||
327 | $currentGuid = -1; |
||
328 | $uguid = -1; |
||
329 | $euguid = -1; |
||
330 | while ($row = mysql_fetch_object($entity_data)) { |
||
331 | if ($emit) { |
||
332 | if ($currentGuid != $row->guid) { |
||
333 | $fin = finalizeEntity($currentGuid); |
||
334 | foreach ($fin as $fs) yield $fs; |
||
335 | $currentGuid = -1; |
||
336 | $count += 1; |
||
337 | if (($max !== false) && ($count > $max)) break; |
||
338 | yield ','; |
||
339 | yield '{'; |
||
340 | $currentGuid = $row->guid; |
||
341 | $euguid = mysql_escape_string(intval($row->guid)); |
||
342 | $uguid = $row->guid; |
||
343 | |||
344 | yield '"guid":' . $row->guid . ',' . |
||
345 | '"type":"' . $row->type . '",' . |
||
346 | '"subtype":' . $row->subtype . ',' . |
||
347 | '"subtype_name":' . json_encode($subtypes["i_{$row->subtype}"]) . ',' . |
||
348 | '"time_created":' . $row->time_created . ',' . |
||
349 | '"url":' . json_encode(getURL($row)) . ',' . |
||
350 | '"access_id":' . $row->access_id . ',' . |
||
351 | '"time_updated":' . $row->time_updated . ',' . |
||
352 | '"owner_guid":' . $row->owner_guid . ',' . |
||
353 | '"container_guid":' . $row->container_guid . ',' . |
||
354 | '"enabled":"' . $row->enabled . '",' . |
||
355 | '"site_guid":' . $row->site_guid; |
||
356 | if (!is_null($row->title)) { |
||
357 | yield ',"title":' . json_encode($row->title) . ',' . |
||
358 | '"description":' . json_encode($row->description); |
||
359 | } |
||
360 | if (!is_null($row->group_name)) { |
||
361 | yield ',"name":' . json_encode($row->group_name) . ',' . |
||
362 | '"description":' . json_encode($row->group_description); |
||
363 | } |
||
364 | if (!is_null($row->name)) { |
||
365 | yield ',"name":' . json_encode($row->name) . ',' . |
||
366 | '"username":' . json_encode($row->username) . ',' . |
||
367 | '"language":"' . $row->language . '",' . |
||
368 | '"admin":"' . $row->admin . '",' . |
||
369 | '"banned":"' . $row->banned . '",' . |
||
370 | '"last_action":' . $row->user_last_action . ',' . |
||
371 | '"prev_last_action":' . $row->prev_last_action . ',' . |
||
372 | '"last_login":' . $row->last_login . ',' . |
||
373 | '"prev_last_login":' . $row->prev_last_login; |
||
374 | } else { |
||
375 | yield ',"last_action":' . $row->last_action; |
||
376 | } |
||
377 | } |
||
378 | if (!is_null($row->meta_name)) yield outVal($row); |
||
379 | } |
||
380 | if (!$emit) { |
||
381 | if ($row->guid == $resume) { |
||
382 | $emit = true; |
||
383 | } |
||
384 | } |
||
385 | } |
||
386 | if ($currentGuid > 0) { |
||
387 | $fin = finalizeEntity($currentGuid); |
||
388 | foreach ($fin as $fs) yield $fs; |
||
389 | } |
||
390 | } catch (Exception $e) { |
||
391 | yield ',"error": ' . json_encode($e); |
||
392 | } |
||
393 | } |
||
394 | |||
412 | ?> |
||
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.