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 |
||
12 | class AuthApi extends BaseApi |
||
13 | { |
||
14 | const API_URL = 'https://open.weixin.qq.com/connect/oauth2/authorize'; |
||
15 | |||
16 | protected static $authorizedUser; |
||
17 | |||
18 | /** |
||
19 | * 生成outh URL |
||
20 | * |
||
21 | * @param string $to |
||
22 | * @param string $scope |
||
23 | * @param string $state |
||
24 | * |
||
25 | * @return string |
||
26 | */ |
||
27 | public function url($to = null, $scope = 'snsapi_userinfo', $state = 'STATE') |
||
41 | |||
42 | /** |
||
43 | * 直接跳转 |
||
44 | * |
||
45 | * @param string $to |
||
46 | * @param string $scope |
||
47 | * @param string $state |
||
48 | */ |
||
49 | public function redirect($to = null, $scope = 'snsapi_userinfo', $state = 'STATE') |
||
55 | |||
56 | /** |
||
57 | * 获取用户信息 |
||
58 | * |
||
59 | * @param string $openId |
||
60 | * @param string $accessToken |
||
61 | * |
||
62 | * @return array |
||
63 | */ |
||
64 | View Code Duplication | public function getUser($openId, $accessToken) |
|
78 | |||
79 | /** |
||
80 | * 获取已授权用户 |
||
81 | * |
||
82 | * @return array $user |
||
83 | */ |
||
84 | public function user() |
||
100 | |||
101 | /** |
||
102 | * 通过授权获取用户 |
||
103 | * |
||
104 | * @param null $to |
||
105 | * @param string $scope |
||
106 | * @param string $state |
||
107 | * |
||
108 | * @return array |
||
109 | */ |
||
110 | public function authorize($to = null, $scope = 'snsapi_userinfo', $state = 'STATE') |
||
118 | |||
119 | /** |
||
120 | * 检查 Access Token 是否有效 |
||
121 | * |
||
122 | * @param string $accessToken |
||
123 | * @param string $openId |
||
124 | * |
||
125 | * @return boolean |
||
126 | */ |
||
127 | View Code Duplication | public function accessTokenIsValid($accessToken, $openId) |
|
141 | |||
142 | /** |
||
143 | * 刷新 access_token |
||
144 | * |
||
145 | * @param $refreshToken |
||
146 | * |
||
147 | * @return bool|array |
||
148 | */ |
||
149 | View Code Duplication | public function refresh($refreshToken) |
|
163 | |||
164 | /** |
||
165 | * 获取access token |
||
166 | * |
||
167 | * @param string $code |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | View Code Duplication | public function getAccessPermission($code) |
|
186 | } |
||
187 |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.