Conditions | 1 |
Paths | 1 |
Total Lines | 99 |
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 |
||
22 | function getUserAndRespond() |
||
23 | { |
||
24 | $firstname = ''; |
||
25 | $lastname = ''; |
||
26 | $displayname = ''; |
||
27 | $fullname = ''; |
||
|
|||
28 | $emailaddr = ''; |
||
29 | $openidid = ''; |
||
30 | $oidcid = ''; |
||
31 | |||
32 | Util::unsetSessionVar('logonerror'); |
||
33 | |||
34 | $state = Util::getGetVar('state'); // 'state' must match last CSRF value |
||
35 | $code = Util::getGetVar('code'); // 'code' must not be empty |
||
36 | $lastcsrf = Util::getCsrf()->getTheCookie(); |
||
37 | if ($state != $lastcsrf) { |
||
38 | // Verify that response's 'state' equals the last CSRF token |
||
39 | Util::setSessionVar('logonerror', 'Invalid state parameter.'); |
||
40 | } elseif (strlen($code) == 0) { |
||
41 | // Make sure the response has a non-empty 'code' |
||
42 | $error = Util::getGetVar('error'); |
||
43 | $error_description = Util::getGetVar('error_description'); |
||
44 | if ((strlen($error) > 0) && (strlen($error_description) > 0)) { |
||
45 | Util::setSessionVar('logonerror', $error_description . '. Please try again.'); |
||
46 | } else { |
||
47 | Util::setSessionVar('logonerror', 'Empty code parameter. Please try again.'); |
||
48 | } |
||
49 | } else { |
||
50 | // When using OAuth or OIDC, check portalcookie for providerId |
||
51 | $providerId = Util::getPortalOrNormalCookieVar('providerId'); |
||
52 | $providerName = Util::getAuthzIdP($providerId); |
||
53 | $prov = strtolower($providerName); // IdP name all lowercase |
||
54 | |||
55 | // Read the client secret keys from local config file |
||
56 | $clientid = Util::getConfigVar($prov . 'oauth2.clientid'); |
||
57 | $clientsecret = Util::getConfigVar($prov . 'oauth2.clientsecret'); |
||
58 | if ((strlen($clientid) > 0) && (strlen($clientsecret) > 0)) { |
||
59 | $oauth2 = new OAuth2Provider($providerName); |
||
60 | try { |
||
61 | $token = $oauth2->provider->getAccessToken( |
||
62 | 'authorization_code', |
||
63 | [ 'code' => $code ] |
||
64 | ); |
||
65 | $user = $oauth2->provider->getResourceOwner($token); |
||
66 | $oidcid = $user->getId(); |
||
67 | $emailaddr = $user->getEmail(); |
||
68 | // GitHub email may require special handling |
||
69 | if ((strlen($emailaddr) == 0) && ($prov == 'github')) { |
||
70 | $emailaddr = getGitHubEmail($oauth2, $token); |
||
71 | } |
||
72 | $name = $user->getName(); |
||
73 | $first = ''; |
||
74 | $last = ''; |
||
75 | if ($prov != 'github') { // No first/last for GitHub |
||
76 | $first = $user->getFirstName(); |
||
77 | $last = $user->getLastName(); |
||
78 | } |
||
79 | list($firstname, $lastname) = |
||
80 | Util::getFirstAndLastName($name, $first, $last); |
||
81 | } catch (Exception $e) { |
||
82 | Util::setSessionVar('logonerror', $e->getMessage()); |
||
83 | } |
||
84 | } else { |
||
85 | Util::setSessionVar( |
||
86 | 'logonerror', |
||
87 | 'Missing OAuth2 client configuration values.' |
||
88 | ); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | // If no error reported, check for session var 'storeattributes' |
||
93 | // which indicates to simply store the user attributes in the |
||
94 | // PHP session. If not set, then by default save the user |
||
95 | // attributes to the database (which also stores the user |
||
96 | // attributes in the PHP session). |
||
97 | if (strlen(Util::getSessionVar('logonerror')) == 0) { |
||
98 | $func = 'CILogon\Service\Util::saveUserToDataStore'; |
||
99 | if (!empty(Util::getSessionVar('storeattributes'))) { |
||
100 | $func = 'CILogon\Service\Util::setUserAttributeSessionVars'; |
||
101 | Util::unsetSessionVar('storeattributes'); |
||
102 | } |
||
103 | $func( |
||
104 | $openidid, |
||
105 | $providerId, |
||
106 | $providerName, |
||
107 | $firstname, |
||
108 | $lastname, |
||
109 | $displayname, |
||
110 | $emailaddr, |
||
111 | 'openid', |
||
112 | '', // ePPN |
||
113 | '', // ePTID |
||
114 | $openidid, |
||
115 | $oidcid |
||
116 | ); |
||
117 | } else { |
||
118 | Util::unsetSessionVar('submit'); |
||
119 | } |
||
120 | } |
||
121 | |||
156 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.