Conditions | 8 |
Paths | 19 |
Total Lines | 60 |
Code Lines | 51 |
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 |
||
14 | public function getPersonData($uid) |
||
15 | { |
||
16 | $url = sprintf($this->url, str_replace('u', '0', $uid)); |
||
17 | $ch = curl_init(); |
||
18 | curl_setopt($ch, CURLOPT_URL, $url); |
||
19 | curl_setopt($ch, CURLOPT_HEADER, false); |
||
20 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
||
21 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); |
||
22 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
23 | $output = curl_exec($ch); |
||
24 | $error = curl_error($ch); |
||
25 | curl_close($ch); |
||
26 | if (false === $output) { |
||
27 | throw new \Exception('Could not get person data for ' . $uid . ': ' . $error); |
||
28 | } |
||
29 | $decodedOutput = json_decode($output); |
||
30 | if (null === $decodedOutput) { |
||
31 | throw new \Exception('Could not get person data for ' . $uid . ': ' . json_last_error_msg()); |
||
32 | } |
||
33 | if (0 === count($decodedOutput->d)) { |
||
34 | throw new \Exception('Could not get person data for ' . $uid . ': ' . json_encode($decodedOutput)); |
||
35 | } |
||
36 | $user = $decodedOutput->d; |
||
37 | $mainWorkAddresses = array_filter($user->WorkAddresses->results, function (\stdClass $value) { |
||
38 | return $value->isMainWorkAddress; |
||
39 | }); |
||
40 | $mainWorkAddress = reset($mainWorkAddresses); |
||
41 | if (!empty($mainWorkAddress)) { |
||
42 | $workAddress = [ |
||
43 | 'phone' => $mainWorkAddress->phone, |
||
44 | 'label' => $mainWorkAddress->buildingDescription, |
||
45 | 'street' => $mainWorkAddress->street, |
||
46 | 'number' => $mainWorkAddress->houseNr, |
||
47 | 'postalCode' => $mainWorkAddress->postalCode, |
||
48 | 'city' => $mainWorkAddress->city, |
||
49 | 'province' => null, |
||
50 | 'country' => null, |
||
51 | ]; |
||
52 | } else { |
||
53 | $workAddress = null; |
||
54 | } |
||
55 | $userData = [ |
||
56 | 'username' => $uid, |
||
57 | 'primaryEmail' => $user->preferredMailAddress, |
||
58 | 'firstName' => !empty($user->firstName) ? $user->firstName : $user->preferredName, |
||
59 | 'lastName' => $user->surname, |
||
60 | 'title' => !empty($user->title) ? $user->title : null, |
||
61 | 'sexe' => $user->gender, |
||
62 | 'birthDate' => null, |
||
63 | 'birthLocation' => null, |
||
64 | 'birthCountry' => null, |
||
65 | 'nationality' => null, |
||
66 | 'correspondenceLanguage' => null, |
||
67 | 'picture' => !empty($user->pictureUrl) ? $user->pictureUrl : null, |
||
68 | 'privateEmail' => null, |
||
69 | 'mobilePhoneNumber' => $user->mobilePhone, |
||
70 | 'workAddress' => $workAddress, |
||
71 | ]; |
||
72 | return $userData; |
||
73 | } |
||
74 | } |
||
75 |