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 |
||
| 26 | class CorpWechatProvider extends AbstractProvider implements ProviderInterface |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * The base url of WeChat API. |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $userBaseInfoApi = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo'; |
||
| 34 | protected $userInfoApi = 'https://qyapi.weixin.qq.com/cgi-bin/user/get'; |
||
| 35 | protected $accessTokenApi = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'; |
||
| 36 | protected $oauthApi = 'https://open.weixin.qq.com/connect/oauth2/authorize'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * {@inheritdoc}. |
||
| 40 | */ |
||
| 41 | protected $openId; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * {@inheritdoc}. |
||
| 45 | */ |
||
| 46 | protected $scopes = ['snsapi_base']; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Indicates if the session state should be utilized. |
||
| 50 | * |
||
| 51 | * @var bool |
||
| 52 | */ |
||
| 53 | protected $stateless = true; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * {@inheritdoc}. |
||
| 57 | */ |
||
| 58 | protected function getAuthUrl($state) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * {@inheritdoc}. |
||
| 65 | */ |
||
| 66 | protected function buildAuthUrlFromBase($url, $state) |
||
| 67 | { |
||
| 68 | $query = http_build_query($this->getCodeFields($state), '', '&', $this->encodingType); |
||
| 69 | $url = $url.'?'.$query.'#wechat_redirect'; |
||
| 70 | |||
| 71 | return $url; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc}. |
||
| 76 | */ |
||
| 77 | View Code Duplication | protected function getCodeFields($state = null) |
|
|
|
|||
| 78 | { |
||
| 79 | $result = array_merge([ |
||
| 80 | 'appid' => $this->clientId, |
||
| 81 | 'redirect_uri' => $this->redirectUrl, |
||
| 82 | 'response_type' => 'code', |
||
| 83 | 'scope' => $this->formatScopes($this->scopes, $this->scopeSeparator), |
||
| 84 | 'state' => $state ?: md5(time()), |
||
| 85 | ], $this->parameters); |
||
| 86 | |||
| 87 | return $result; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * 获取 access token的路径. |
||
| 92 | */ |
||
| 93 | protected function getTokenUrl() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc}. |
||
| 100 | */ |
||
| 101 | protected function getUserByToken(AccessTokenInterface $token) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * {@inheritdoc}. |
||
| 119 | */ |
||
| 120 | protected function mapUserToObject(array $user) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * 构建access_token 的参数列表, 分为两种情况一种是 获取access token, 另一种是直接获取userid. |
||
| 136 | */ |
||
| 137 | protected function getTokenFields($code = false) |
||
| 138 | { |
||
| 139 | if (!$code) { |
||
| 140 | return [ |
||
| 141 | 'corpid' => $this->clientId, |
||
| 142 | 'corpsecret' => $this->clientSecret, |
||
| 143 | ]; |
||
| 144 | } |
||
| 145 | |||
| 146 | return [ |
||
| 147 | 'access_token' => $this->config['longlive_access_token'], |
||
| 148 | 'code' => $code, |
||
| 149 | ]; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * 原始微信oauth 应该是返回 access token + openid |
||
| 154 | * 企业号因为用的是7200秒的, 所以需要支持从外部去获取access_token 不会冲突 要返回 userid. |
||
| 155 | */ |
||
| 156 | public function getAccessToken($code) |
||
| 157 | { |
||
| 158 | //没有指定则自己获取 |
||
| 159 | if (!$this->config['longlive_access_token']) { |
||
| 160 | $this->config['longlive_access_token'] = $this->getLongiveAccessToken(); |
||
| 161 | } |
||
| 162 | $param = $this->getTokenFields($code); |
||
| 163 | $response = $this->getHttpClient()->get($this->userBaseInfoApi, [ |
||
| 164 | 'query' => $param, |
||
| 165 | ]); |
||
| 166 | $content = $response->getBody()->getContents(); |
||
| 167 | $content = json_decode($content, true); |
||
| 168 | $content['access_token'] = $this->config['longlive_access_token']; |
||
| 169 | $token = $this->parseAccessToken($content); |
||
| 170 | |||
| 171 | return $token; |
||
| 172 | } |
||
| 173 | |||
| 174 | // !!应该尽量不要调用, 除非 单独与overture/wechat使用, 否则同时获取accesstoken, 会冲突 |
||
| 175 | public function getLongiveAccessToken($force_refresh = false) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Remove the fucking callback parentheses. |
||
| 189 | * |
||
| 190 | * @param mixed $response |
||
| 191 | * |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | protected function removeCallback($response) |
||
| 204 | } |
||
| 205 |
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.