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