Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 17 | abstract class EOAuth2Service extends EAuthServiceBase implements IAuthService { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string OAuth2 client id. |
||
| 21 | */ |
||
| 22 | protected $client_id; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string OAuth2 client secret key. |
||
| 26 | */ |
||
| 27 | protected $client_secret; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string OAuth scopes. |
||
| 31 | */ |
||
| 32 | protected $scope = ''; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array Provider options. Must contain the keys: authorize, access_token. |
||
| 36 | */ |
||
| 37 | protected $providerOptions = array( |
||
| 38 | 'authorize' => '', |
||
| 39 | 'access_token' => '', |
||
| 40 | ); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string current OAuth2 access token. |
||
| 44 | */ |
||
| 45 | protected $access_token = ''; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string Error key name in _GET options. |
||
| 49 | */ |
||
| 50 | protected $errorParam = 'error'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string Error description key name in _GET options. |
||
| 54 | */ |
||
| 55 | protected $errorDescriptionParam = 'error_description'; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var string Error code for access_denied response. |
||
| 59 | */ |
||
| 60 | protected $errorAccessDeniedCode = 'access_denied'; |
||
| 61 | |||
| 62 | |||
| 63 | public function init($component, $options = array()) { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Authenticate the user. |
||
| 72 | * |
||
| 73 | * @return boolean whether user was successfuly authenticated. |
||
| 74 | * @throws EAuthException |
||
| 75 | */ |
||
| 76 | public function authenticate() { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Returns the url to request to get OAuth2 code. |
||
| 122 | * |
||
| 123 | * @param string $redirect_uri url to redirect after user confirmation. |
||
| 124 | * @return string url to request. |
||
| 125 | */ |
||
| 126 | protected function getCodeUrl($redirect_uri) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Returns the url to request to get OAuth2 access token. |
||
| 133 | * |
||
| 134 | * @param string $code |
||
| 135 | * @return string url to request. |
||
| 136 | */ |
||
| 137 | protected function getTokenUrl($code) { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Returns the OAuth2 access token. |
||
| 143 | * |
||
| 144 | * @param string $code the OAuth2 code. See {@link getCodeUrl}. |
||
| 145 | * @return string the token. |
||
| 146 | */ |
||
| 147 | protected function getAccessToken($code) { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Save access token to the session. |
||
| 153 | * |
||
| 154 | * @param string $token access token. |
||
| 155 | */ |
||
| 156 | protected function saveAccessToken($token) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Restore access token from the session. |
||
| 163 | * |
||
| 164 | * @return boolean whether the access token was successfuly restored. |
||
| 165 | */ |
||
| 166 | protected function restoreAccessToken() { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Returns fields required for signed request. |
||
| 183 | * By default returns array('access_token' => $this->access_token). |
||
| 184 | * Used in {@link makeSignedRequest}. |
||
| 185 | * |
||
| 186 | * @return array |
||
| 187 | */ |
||
| 188 | protected function getSignedRequestFields() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Returns the protected resource. |
||
| 195 | * |
||
| 196 | * @param string $url url to request. |
||
| 197 | * @param array $options HTTP request options. Keys: query, data, referer. |
||
| 198 | * @param boolean $parseJson Whether to parse response in json format. |
||
| 199 | * @return stdClass the response. |
||
| 200 | * @see makeRequest |
||
| 201 | */ |
||
| 202 | public function makeSignedRequest($url, $options = array(), $parseJson = true) { |
||
| 218 | } |
||
| 219 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.