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 |
||
| 24 | class QQProvider extends AbstractProvider implements ProviderInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * The base url of QQ API. |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $baseUrl = 'https://graph.qq.com'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * User openid. |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $openId; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * get token(openid) with unionid. |
||
| 42 | * @var bool |
||
| 43 | */ |
||
| 44 | protected $withUnionId = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * User unionid. |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $unionId; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The scopes being requested. |
||
| 55 | * |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | protected $scopes = ['get_user_info']; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The uid of user authorized. |
||
| 62 | * |
||
| 63 | * @var int |
||
| 64 | */ |
||
| 65 | protected $uid; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Get the authentication URL for the provider. |
||
| 69 | * |
||
| 70 | * @param string $state |
||
| 71 | * |
||
| 72 | * @return string |
||
| 73 | */ |
||
| 74 | protected function getAuthUrl($state) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Get the token URL for the provider. |
||
| 81 | * |
||
| 82 | * @return string |
||
| 83 | */ |
||
| 84 | protected function getTokenUrl() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Get the Post fields for the token request. |
||
| 91 | * |
||
| 92 | * @param string $code |
||
| 93 | * |
||
| 94 | * @return array |
||
| 95 | */ |
||
| 96 | protected function getTokenFields($code) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get the access token for the given code. |
||
| 103 | * |
||
| 104 | * @param string $code |
||
| 105 | * |
||
| 106 | * @return \Overtrue\Socialite\AccessToken |
||
| 107 | */ |
||
| 108 | View Code Duplication | public function getAccessToken($code) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Get the access token from the token response body. |
||
| 119 | * |
||
| 120 | * @param string $body |
||
| 121 | * |
||
| 122 | * @return \Overtrue\Socialite\AccessToken |
||
| 123 | */ |
||
| 124 | public function parseAccessToken($body) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * |
||
| 133 | * @return self |
||
| 134 | */ |
||
| 135 | public function withUnionId() { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Get the raw user for the given access token. |
||
| 142 | * |
||
| 143 | * @param \Overtrue\Socialite\AccessTokenInterface $token |
||
| 144 | * |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | protected function getUserByToken(AccessTokenInterface $token) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Map the raw user array to a Socialite User instance. |
||
| 171 | * |
||
| 172 | * @param array $user |
||
| 173 | * |
||
| 174 | * @return \Overtrue\Socialite\User |
||
| 175 | */ |
||
| 176 | protected function mapUserToObject(array $user) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Remove the fucking callback parentheses. |
||
| 190 | * |
||
| 191 | * @param string $response |
||
| 192 | * |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | View Code Duplication | protected function removeCallback($response) |
|
| 205 | } |
||
| 206 |
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.