Conditions | 3 |
Paths | 4 |
Total Lines | 88 |
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 |
||
20 | function edit_profile_paas_mutation($hook, $entity_type, $returnvalue, $params) { |
||
21 | $guid = get_input('guid'); |
||
22 | $owner = get_entity($guid); |
||
23 | |||
24 | $dbprefix = elgg_get_config("dbprefix"); |
||
25 | $service_url = elgg_get_plugin_setting("graphql_client", "paas_integration"); |
||
26 | |||
27 | $session = elgg_get_session(); |
||
28 | $token = $session->get('token'); |
||
29 | |||
30 | $result = get_data_row("SELECT pleio_guid FROM {$dbprefix}users_entity WHERE guid = $guid"); |
||
31 | if ($result->pleio_guid) { |
||
32 | $gcID = $result->pleio_guid; |
||
33 | } |
||
34 | |||
35 | $client = new Client($service_url); |
||
36 | |||
37 | $headers = [ |
||
38 | "Authorization" => "Bearer $token" |
||
39 | ]; |
||
40 | |||
41 | $query = 'mutation ($gcID: ID!, $data: ModifyProfileInput!) { |
||
42 | modifyProfile(gcID: $gcID, data: $data) { |
||
43 | gcID |
||
44 | name |
||
45 | |||
46 | titleEn |
||
47 | titleFr |
||
48 | avatar |
||
49 | mobilePhone |
||
50 | officePhone |
||
51 | address { |
||
52 | streetAddress |
||
53 | city |
||
54 | postalCode |
||
55 | province |
||
56 | country |
||
57 | } |
||
58 | } |
||
59 | }'; |
||
60 | |||
61 | $profile_fields = get_input('profile'); |
||
62 | |||
63 | $variables = array( |
||
64 | 'gcID' => $gcID, |
||
|
|||
65 | 'data' => array( |
||
66 | 'name' => $profile_fields['name'], |
||
67 | 'titleEn' => copy_value_to_object_if_defined('job', $profile_fields['job']), |
||
68 | 'titleFr' => copy_value_to_object_if_defined('jobfr', $profile_fields['jobfr']), |
||
69 | 'mobilePhone' => copy_value_to_object_if_defined('mobile', $profile_fields['mobile']), |
||
70 | 'officePhone' => copy_value_to_object_if_defined('phone', $profile_fields['phone']), |
||
71 | 'address' => array( |
||
72 | 'streetAddress' => copy_value_to_object_if_defined($profile_fields['streetaddress']), |
||
73 | 'city' => copy_value_to_object_if_defined($profile_fields['city']), |
||
74 | 'province' => copy_value_to_object_if_defined($profile_fields['province']), |
||
75 | 'postalCode' => copy_value_to_object_if_defined($profile_fields['postalcode']), |
||
76 | 'country' => copy_value_to_object_if_defined($profile_fields['country']), |
||
77 | ) |
||
78 | ) |
||
79 | ); |
||
80 | |||
81 | // Send data to PaaS |
||
82 | $response = $client->response($query, $variables, $headers); |
||
83 | |||
84 | // Error check |
||
85 | if($response->errors()){ |
||
86 | return false; |
||
87 | } |
||
88 | |||
89 | // Contact details |
||
90 | $owner->name = $response->modifyProfile->name; |
||
91 | $owner->job = $response->modifyProfile->titleEn; |
||
92 | $owner->jobfr = $response->modifyProfile->titleFr; |
||
93 | $owner->mobile = $response->modifyProfile->mobilePhone; |
||
94 | $owner->phone = $response->modifyProfile->officePhone; |
||
95 | |||
96 | // Address details |
||
97 | $owner->streetaddress = $response->modifyProfile->address->streetAddress; |
||
98 | $owner->city = $response->modifyProfile->address->city; |
||
99 | $owner->province = $response->modifyProfile->address->province; |
||
100 | $owner->postalcode = $response->modifyProfile->address->postalCode; |
||
101 | $owner->country = $response->modifyProfile->address->country; |
||
102 | |||
103 | $owner->save(); |
||
104 | |||
105 | // Mutation has gone through |
||
106 | return true; |
||
107 | } |
||
108 | |||
116 |
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: