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 |
||
| 28 | class Session extends AbstractAPI |
||
| 29 | { |
||
| 30 | const API_CREATE = 'https://api.weixin.qq.com/customservice/kfsession/create'; |
||
| 31 | const API_CLOSE = 'https://api.weixin.qq.com/customservice/kfsession/close'; |
||
| 32 | const API_GET = 'https://api.weixin.qq.com/customservice/kfsession/getsession'; |
||
| 33 | const API_LISTS = 'https://api.weixin.qq.com/customservice/kfsession/getsessionlist'; |
||
| 34 | const API_WAITERS = 'https://api.weixin.qq.com/customservice/kfsession/getwaitcase'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * List all sessions of $account. |
||
| 38 | * |
||
| 39 | * @param string $account |
||
| 40 | * |
||
| 41 | * @return array |
||
| 42 | */ |
||
| 43 | 1 | public function lists($account) |
|
| 47 | |||
| 48 | /** |
||
| 49 | * List all waiters of $account. |
||
| 50 | * |
||
| 51 | * @return array |
||
| 52 | */ |
||
| 53 | 1 | public function waiters() |
|
| 54 | { |
||
| 55 | 1 | return $this->parseJSON('get', [self::API_WAITERS]); |
|
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Create a session. |
||
| 60 | * |
||
| 61 | * @param string $account |
||
| 62 | * @param string $openid |
||
|
|
|||
| 63 | * |
||
| 64 | * @return bool |
||
| 65 | */ |
||
| 66 | 1 | View Code Duplication | public function create($account, $openId) |
| 75 | |||
| 76 | /** |
||
| 77 | * Close a session. |
||
| 78 | * |
||
| 79 | * @param string $account |
||
| 80 | * @param string $openId |
||
| 81 | * |
||
| 82 | * @return bool |
||
| 83 | */ |
||
| 84 | 1 | View Code Duplication | public function close($account, $openId) |
| 93 | |||
| 94 | /** |
||
| 95 | * Get a session. |
||
| 96 | * |
||
| 97 | * @param string $openId |
||
| 98 | * |
||
| 99 | * @return bool |
||
| 100 | */ |
||
| 101 | 1 | public function get($openId) |
|
| 105 | } |
||
| 106 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.