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 |
||
| 25 | class TaobaoProvider extends AbstractProvider implements ProviderInterface |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * The base url of Taobao API. |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $baseUrl = 'https://oauth.taobao.com'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Taobao API service URL address |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $gatewayUrl = 'https://eco.taobao.com/router/rest'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The API version for the request. |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $version = '2.0'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $format = 'json'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $signMethod = 'md5'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Web 对应 PC 端(淘宝 logo )浏览器页面样式;Tmall 对应天猫的浏览器页面样式;Wap 对应无线端的浏览器页面样式。 |
||
| 60 | */ |
||
| 61 | protected $view = 'web'; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The scopes being requested. |
||
| 65 | * |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected $scopes = ['user_info']; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Get the authentication URL for the provider. |
||
| 72 | * |
||
| 73 | * @param string $state |
||
| 74 | * |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | protected function getAuthUrl($state) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * 获取授权码接口参数. |
||
| 84 | * |
||
| 85 | * @param string|null $state |
||
| 86 | * |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | public function getCodeFields($state = null) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Get the token URL for the provider. |
||
| 107 | * |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | protected function getTokenUrl() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Get the Post fields for the token request. |
||
| 117 | * |
||
| 118 | * @param string $code |
||
| 119 | * |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | protected function getTokenFields($code) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get the access token for the given code. |
||
| 129 | * |
||
| 130 | * @param string $code |
||
| 131 | * |
||
| 132 | * @return \Overtrue\Socialite\AccessToken |
||
| 133 | */ |
||
| 134 | View Code Duplication | public function getAccessToken($code) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Get the access token from the token response body. |
||
| 145 | * |
||
| 146 | * @param string $body |
||
| 147 | * |
||
| 148 | * @return \Overtrue\Socialite\AccessToken |
||
| 149 | */ |
||
| 150 | public function parseAccessToken($body) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Get the raw user for the given access token. |
||
| 157 | * |
||
| 158 | * @param \Overtrue\Socialite\AccessTokenInterface $token |
||
| 159 | * |
||
| 160 | * @return array |
||
| 161 | */ |
||
| 162 | 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) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param $params |
||
| 188 | * |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | protected function generateSign($params) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param \Overtrue\Socialite\AccessTokenInterface $token |
||
| 210 | * @param array $apiFields |
||
| 211 | * |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | protected function getPublicFields(AccessTokenInterface $token, array $apiFields = []) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * {@inheritdoc}. |
||
| 233 | */ |
||
| 234 | protected function getUserInfoUrl($url, AccessTokenInterface $token) |
||
| 242 | } |
||
| 243 |
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.