Conditions | 7 |
Paths | 156 |
Total Lines | 69 |
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 |
||
84 | public function authenticate() { |
||
85 | |||
86 | $session=Yii::app()->session; |
||
87 | |||
88 | if (isset($_REQUEST['oauth_token'])) { |
||
89 | $oauthToken = $_REQUEST['oauth_token']; |
||
90 | } |
||
91 | if (isset($_REQUEST['oauth_verifier'])) { |
||
92 | $oauthVerifier = $_REQUEST['oauth_verifier']; |
||
93 | } |
||
94 | |||
95 | try { |
||
96 | |||
97 | if (!isset($oauthToken)) { |
||
98 | // Create consumer. |
||
99 | $consumer = new OAuthConsumer($this->key, $this->secret); |
||
100 | |||
101 | // Set the scope (must match service endpoint). |
||
102 | $scope = $this->scope; |
||
103 | |||
104 | // Set the application name as it is displayed on the authorization page. |
||
105 | $applicationName = Yii::app()->name; |
||
106 | |||
107 | // Use the URL of the current page as the callback URL. |
||
108 | $protocol = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") |
||
109 | ? 'https://' : 'http://'; |
||
110 | $server = $_SERVER['HTTP_HOST']; |
||
111 | $path = $_SERVER["REQUEST_URI"]; |
||
112 | $callbackUrl = $protocol . $server . $path; |
||
113 | |||
114 | // Get request token. |
||
115 | $token = EOAuthUtils::GetRequestToken($consumer, $scope, |
||
116 | $this->provider->request_token_endpoint, $applicationName, $callbackUrl); |
||
117 | |||
118 | // Store consumer and token in session. |
||
119 | $session['OAUTH_CONSUMER'] = $consumer; |
||
120 | $session['OAUTH_TOKEN'] = $token; |
||
121 | |||
122 | // Get authorization URL. |
||
123 | $url = EOAuthUtils::GetAuthorizationUrl($token, |
||
124 | $this->provider->authorize_token_endpoint); |
||
125 | |||
126 | // Redirect to authorization URL. |
||
127 | Yii::app()->request->redirect($url); |
||
128 | } else { |
||
129 | // Retrieve consumer and token from session. |
||
130 | $consumer = $session['OAUTH_CONSUMER']; |
||
131 | $token = $session['OAUTH_TOKEN']; |
||
132 | |||
133 | // Set authorized token. |
||
134 | $token->key = $oauthToken; |
||
135 | |||
136 | // Upgrade to access token. |
||
137 | $token = EOAuthUtils::GetAccessToken($consumer, $token, $oauthVerifier, |
||
|
|||
138 | $this->provider->access_token_endpoint); |
||
139 | |||
140 | // Set OAuth provider. |
||
141 | $this->provider->consumer=$consumer; |
||
142 | $this->provider->token=$token; |
||
143 | |||
144 | $this->_authenticated=true; |
||
145 | } |
||
146 | |||
147 | } catch (OAuthException $e) { |
||
148 | $this->_error=$e->getMessage(); |
||
149 | } |
||
150 | |||
151 | return $this->isAuthenticated; |
||
152 | } |
||
153 | |||
172 |
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: