| Conditions | 3 |
| Paths | 4 |
| Total Lines | 90 |
| 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 | $address = array( |
||
| 64 | 'streetAddress' => copy_value_to_object_if_defined($profile_fields['streetaddress']), |
||
| 65 | 'city' => copy_value_to_object_if_defined($profile_fields['city']), |
||
| 66 | 'province' => copy_value_to_object_if_defined($profile_fields['province']), |
||
| 67 | 'postalCode' => copy_value_to_object_if_defined($profile_fields['postalcode']), |
||
| 68 | 'country' => copy_value_to_object_if_defined($profile_fields['country']), |
||
| 69 | ); |
||
| 70 | |||
| 71 | $variables = array( |
||
| 72 | 'gcID' => $gcID, |
||
|
|
|||
| 73 | 'data' => array( |
||
| 74 | 'name' => $profile_fields['name'], |
||
| 75 | 'titleEn' => copy_value_to_object_if_defined($profile_fields['job']), |
||
| 76 | 'titleFr' => copy_value_to_object_if_defined($profile_fields['jobfr']), |
||
| 77 | 'mobilePhone' => copy_value_to_object_if_defined($profile_fields['mobile']), |
||
| 78 | 'officePhone' => copy_value_to_object_if_defined($profile_fields['phone']), |
||
| 79 | 'address' => address_validation($address), |
||
| 80 | ) |
||
| 81 | ); |
||
| 82 | |||
| 83 | // Send data to PaaS |
||
| 84 | $response = $client->response($query, $variables, $headers); |
||
| 85 | |||
| 86 | // Error check |
||
| 87 | if($response->errors()){ |
||
| 88 | return false; |
||
| 89 | } |
||
| 90 | |||
| 91 | // Contact details |
||
| 92 | $owner->name = $response->modifyProfile->name; |
||
| 93 | $owner->job = $response->modifyProfile->titleEn; |
||
| 94 | $owner->jobfr = $response->modifyProfile->titleFr; |
||
| 95 | $owner->mobile = $response->modifyProfile->mobilePhone; |
||
| 96 | $owner->phone = $response->modifyProfile->officePhone; |
||
| 97 | |||
| 98 | // Address details |
||
| 99 | $owner->streetaddress = $response->modifyProfile->address->streetAddress; |
||
| 100 | $owner->city = $response->modifyProfile->address->city; |
||
| 101 | $owner->province = $response->modifyProfile->address->province; |
||
| 102 | $owner->postalcode = $response->modifyProfile->address->postalCode; |
||
| 103 | $owner->country = $response->modifyProfile->address->country; |
||
| 104 | |||
| 105 | $owner->save(); |
||
| 106 | |||
| 107 | // Mutation has gone through |
||
| 108 | return true; |
||
| 109 | } |
||
| 110 | |||
| 130 | } |
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: