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:
Complex classes like Ucpaas often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Ucpaas, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Ucpaas |
||
|
|
|||
| 11 | { |
||
| 12 | /** |
||
| 13 | * 云之讯REST API版本号。当前版本号为:2014-06-30 |
||
| 14 | */ |
||
| 15 | const SoftVersion = '2014-06-30'; |
||
| 16 | /** |
||
| 17 | * API请求地址 |
||
| 18 | */ |
||
| 19 | const BaseUrl = 'https://api.ucpaas.com/'; |
||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | * 开发者账号ID。由32个英文字母和阿拉伯数字组成的开发者账号唯一标识符。 |
||
| 23 | */ |
||
| 24 | private $accountSid; |
||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | * 开发者账号TOKEN |
||
| 28 | */ |
||
| 29 | private $token; |
||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | * 时间戳 |
||
| 33 | */ |
||
| 34 | private $timestamp; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param $options 数组参数必填 |
||
| 38 | * $options = array( |
||
| 39 | * |
||
| 40 | * ) |
||
| 41 | * |
||
| 42 | * @throws Exception |
||
| 43 | */ |
||
| 44 | public function __construct($options) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @return string |
||
| 58 | * 包头验证信息,使用Base64编码(账户Id:时间戳) |
||
| 59 | */ |
||
| 60 | private function getAuthorization() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @return string |
||
| 69 | * 验证参数,URL后必须带有sig参数,sig= MD5(账户Id + 账户授权令牌 + 时间戳,共32位)(注:转成大写) |
||
| 70 | */ |
||
| 71 | private function getSigParameter() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param $url |
||
| 80 | * @param string $type |
||
| 81 | * |
||
| 82 | * @return mixed|string |
||
| 83 | */ |
||
| 84 | private function getResult($url, $body = null, $type = 'json', $method) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param $url |
||
| 98 | * @param $type |
||
| 99 | * @param $body post数据 |
||
| 100 | * @param $method post或get |
||
| 101 | * |
||
| 102 | * @return mixed|string |
||
| 103 | */ |
||
| 104 | private function connection($url, $body, $type, $method) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param string $type 默认json,也可指定xml,否则抛出异常 |
||
| 153 | * |
||
| 154 | * @throws Exception |
||
| 155 | * |
||
| 156 | * @return mixed|string 返回指定$type格式的数据 |
||
| 157 | * @links http://www.ucpaas.com/page/doc/doc_rest2-1.jsp |
||
| 158 | */ |
||
| 159 | public function getDevinfo($type = 'json') |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param $appId 应用ID |
||
| 176 | * @param $clientType 计费方式。0 开发者计费;1 云平台计费。默认为0. |
||
| 177 | * @param $charge 充值的金额 |
||
| 178 | * @param $friendlyName 昵称 |
||
| 179 | * @param $mobile 手机号码 |
||
| 180 | * |
||
| 181 | * @return json/xml |
||
| 182 | * @links http://www.ucpaas.com/page/doc/doc_rest2-2.jsp |
||
| 183 | */ |
||
| 184 | public function applyClient($appId, $clientType, $charge, $friendlyName, $mobile, $type = 'json') |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param $clientNumber |
||
| 215 | * @param $appId |
||
| 216 | * @param string $type |
||
| 217 | * |
||
| 218 | * @throws Exception |
||
| 219 | * |
||
| 220 | * @return mixed|string |
||
| 221 | * @links http://www.ucpaas.com/page/doc/doc_rest2-3.jsp |
||
| 222 | */ |
||
| 223 | public function releaseClient($clientNumber, $appId, $type = 'json') |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param $appId |
||
| 249 | * @param $start |
||
| 250 | * @param $limit |
||
| 251 | * @param string $type |
||
| 252 | * |
||
| 253 | * @throws Exception |
||
| 254 | * |
||
| 255 | * @return mixed|string |
||
| 256 | * @links http://www.ucpaas.com/page/doc/doc_rest2-4.jsp |
||
| 257 | */ |
||
| 258 | View Code Duplication | public function getClientList($appId, $start, $limit, $type = 'json') |
|
| 283 | |||
| 284 | /** |
||
| 285 | * @param $appId |
||
| 286 | * @param $clientNumber |
||
| 287 | * @param string $type |
||
| 288 | * |
||
| 289 | * @throws Exception |
||
| 290 | * |
||
| 291 | * @return mixed|string |
||
| 292 | * @links http://www.ucpaas.com/page/doc/doc_rest2-5.jsp |
||
| 293 | */ |
||
| 294 | View Code Duplication | public function getClientInfo($appId, $clientNumber, $type = 'json') |
|
| 308 | |||
| 309 | /** |
||
| 310 | * @param $appId |
||
| 311 | * @param $mobile |
||
| 312 | * @param string $type |
||
| 313 | * |
||
| 314 | * @throws Exception |
||
| 315 | * |
||
| 316 | * @return mixed|string |
||
| 317 | * @links http://www.ucpaas.com/page/doc/doc_rest2-9.jsp |
||
| 318 | */ |
||
| 319 | View Code Duplication | public function getClientInfoByMobile($appId, $mobile, $type = 'json') |
|
| 333 | |||
| 334 | /** |
||
| 335 | * @param $appId |
||
| 336 | * @param $date |
||
| 337 | * @param string $type |
||
| 338 | * |
||
| 339 | * @throws Exception |
||
| 340 | * |
||
| 341 | * @return mixed|string |
||
| 342 | * @links http://www.ucpaas.com/page/doc/doc_rest2-6.jsp |
||
| 343 | */ |
||
| 344 | public function getBillList($appId, $date, $type = 'json') |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @param $appId |
||
| 370 | * @param $clientNumber |
||
| 371 | * @param $chargeType |
||
| 372 | * @param $charge |
||
| 373 | * @param string $type |
||
| 374 | * |
||
| 375 | * @throws Exception |
||
| 376 | * |
||
| 377 | * @return mixed|string |
||
| 378 | * @links http://www.ucpaas.com/page/doc/doc_rest2-8.jsp |
||
| 379 | */ |
||
| 380 | View Code Duplication | public function chargeClient($appId, $clientNumber, $chargeType, $charge, $type = 'json') |
|
| 407 | |||
| 408 | /** |
||
| 409 | * @param $appId |
||
| 410 | * @param $fromClient |
||
| 411 | * @param $to |
||
| 412 | * @param null $fromSerNum |
||
| 413 | * @param null $toSerNum |
||
| 414 | * @param string $type |
||
| 415 | * |
||
| 416 | * @throws Exception |
||
| 417 | * |
||
| 418 | * @return mixed|string |
||
| 419 | * @links http://www.ucpaas.com/page/doc/doc_rest3-1.jsp |
||
| 420 | */ |
||
| 421 | public function callBack($appId, $fromClient, $to, $fromSerNum = null, $toSerNum = null, $type = 'json') |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @param $appId |
||
| 453 | * @param $verifyCode |
||
| 454 | * @param $to |
||
| 455 | * @param string $type |
||
| 456 | * |
||
| 457 | * @throws Exception |
||
| 458 | * |
||
| 459 | * @return mixed|string |
||
| 460 | * @links http://www.ucpaas.com/page/doc/doc_rest3-2.jsp |
||
| 461 | */ |
||
| 462 | View Code Duplication | public function voiceCode($appId, $verifyCode, $to, $type = 'json') |
|
| 487 | |||
| 488 | /** |
||
| 489 | * @param $appId |
||
| 490 | * @param $to |
||
| 491 | * @param $templateId |
||
| 492 | * @param null $param |
||
| 493 | * @param string $type |
||
| 494 | * |
||
| 495 | * @throws Exception |
||
| 496 | * |
||
| 497 | * @return mixed|string |
||
| 498 | * @links http://www.ucpaas.com/page/doc/doc_rest4-1.jsp |
||
| 499 | */ |
||
| 500 | View Code Duplication | public function templateSMS($appId, $to, $templateId, $param = null, $type = 'json') |
|
| 527 | } |
||
| 528 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.