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 |
||
| 27 | class WeChatProvider extends AbstractProvider implements ProviderInterface |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * The base url of WeChat API. |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $baseUrl = 'https://api.weixin.qq.com/sns'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * {@inheritdoc}. |
||
| 38 | */ |
||
| 39 | protected $openId; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * {@inheritdoc}. |
||
| 43 | */ |
||
| 44 | protected $scopes = ['snsapi_login']; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Indicates if the session state should be utilized. |
||
| 48 | * |
||
| 49 | * @var bool |
||
| 50 | */ |
||
| 51 | protected $stateless = true; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Return country code instead of country name. |
||
| 55 | * |
||
| 56 | * @var bool |
||
| 57 | */ |
||
| 58 | protected $withCountryCode = false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var WeChatComponentInterface |
||
| 62 | */ |
||
| 63 | protected $component; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Return country code instead of country name. |
||
| 67 | * |
||
| 68 | * @return $this |
||
| 69 | */ |
||
| 70 | public function withCountryCode() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * WeChat OpenPlatform 3rd component. |
||
| 79 | * |
||
| 80 | * @param WeChatComponentInterface $component |
||
| 81 | * |
||
| 82 | * @return $this |
||
| 83 | */ |
||
| 84 | public function component(WeChatComponentInterface $component) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * {@inheritdoc}. |
||
| 95 | */ |
||
| 96 | public function getAccessToken($code) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc}. |
||
| 108 | */ |
||
| 109 | protected function getAuthUrl($state) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * {@inheritdoc}. |
||
| 122 | */ |
||
| 123 | protected function buildAuthUrlFromBase($url, $state) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * {@inheritdoc}. |
||
| 132 | */ |
||
| 133 | protected function getCodeFields($state = null) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * {@inheritdoc}. |
||
| 150 | */ |
||
| 151 | protected function getTokenUrl() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * {@inheritdoc}. |
||
| 162 | */ |
||
| 163 | protected function getUserByToken(AccessTokenInterface $token) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritdoc}. |
||
| 190 | */ |
||
| 191 | protected function mapUserToObject(array $user) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * {@inheritdoc}. |
||
| 204 | */ |
||
| 205 | protected function getTokenFields($code) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Remove the fucking callback parentheses. |
||
| 219 | * |
||
| 220 | * @param mixed $response |
||
| 221 | * |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | View Code Duplication | protected function removeCallback($response) |
|
| 234 | } |
||
| 235 |
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.