Conditions | 1 |
Paths | 1 |
Total Lines | 87 |
Code Lines | 52 |
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 |
||
6 | function solr_api_init() { |
||
7 | |||
8 | |||
9 | |||
10 | elgg_register_event_handler('delete', 'object', 'intercept_object_deletion'); |
||
11 | //elgg_register_event_handler('create','object','cp_create_notification',900); |
||
12 | // @TODO limit access to only the specified user agent string for more strict security |
||
13 | // @TODO list all subtypes and types |
||
14 | |||
15 | // http://192.168.245.130/gcconnex/services/api/rest/json/?method=get.entity_list&type=object&subtype=blog |
||
16 | // http://192.168.245.130/gcconnex/services/api/rest/json/?method=get.user_list |
||
17 | // http://192.168.245.130/gcconnex/services/api/rest/json/?method=get.group_list |
||
18 | |||
19 | |||
20 | // @TODO will need authentication mechanism for this |
||
21 | elgg_ws_expose_function( |
||
22 | 'delete.updated_index_list', |
||
23 | 'delete_updated_index_list', |
||
24 | [ |
||
25 | 'guids' => [ |
||
26 | 'type' => 'array', |
||
27 | 'required' => false, |
||
28 | 'description' => 'deletes records based on collection of entity guids' |
||
29 | ] |
||
30 | ], |
||
31 | 'deletes record that already updated the solr index', |
||
32 | 'POST', |
||
33 | false, |
||
34 | false |
||
35 | ); |
||
36 | |||
37 | |||
38 | // api expose function calls |
||
39 | // parameters: method, function, array of parameters, description, call method/get or post, require api authentication, require user authentication |
||
40 | elgg_ws_expose_function( |
||
41 | 'get.entity_list', |
||
42 | 'get_entity_list', |
||
43 | [ |
||
44 | 'type' => [ |
||
45 | 'type' => 'string', |
||
46 | 'required' => true, |
||
47 | 'description' => 'the type of entity in string format', |
||
48 | ], |
||
49 | 'subtype' => [ |
||
50 | 'type' => 'string', |
||
51 | 'required' => false, |
||
52 | 'description' => 'the subtype of entity in string format, not required', |
||
53 | ], |
||
54 | |||
55 | ], |
||
56 | 'retrieves all entities filtered by type [and subtype]', |
||
57 | 'GET', |
||
58 | false, |
||
59 | false |
||
60 | ); |
||
61 | |||
62 | |||
63 | elgg_ws_expose_function( |
||
64 | 'get.user_list', |
||
65 | 'get_user_list', |
||
66 | null, |
||
67 | 'retrieves a user list', |
||
68 | 'GET', |
||
69 | false, |
||
70 | false |
||
71 | ); |
||
72 | |||
73 | elgg_ws_expose_function( |
||
74 | 'get.group_list', |
||
75 | 'get_group_list', |
||
76 | null, |
||
77 | 'retrieves a group list', |
||
78 | 'GET', |
||
79 | false, |
||
80 | false |
||
81 | ); |
||
82 | |||
83 | elgg_ws_expose_function( |
||
84 | 'get.list_of_deleted_records', |
||
85 | 'get_list_of_deleted_records', |
||
86 | null, |
||
87 | 'retrieves a list of deleted content', |
||
88 | 'GET', |
||
89 | false, |
||
90 | false |
||
91 | ); |
||
92 | } |
||
93 | |||
288 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.