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 DelayQueue |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var string 延迟队列服务器地址 http://127.0.0.1:9277 |
||
| 16 | */ |
||
| 17 | protected $server; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var int httpClient超时设置 |
||
| 21 | */ |
||
| 22 | protected $timeout = 10; |
||
| 23 | |||
| 24 | public function __construct($server) |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param int $timeout |
||
| 31 | */ |
||
| 32 | public function setTimeout($timeout) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @return int |
||
| 39 | */ |
||
| 40 | public function getTimeout() |
||
| 44 | |||
| 45 | /** |
||
| 46 | * 添加Job到延迟队列中 |
||
| 47 | * |
||
| 48 | * @param string $className 处理Job的类名, 必须是[DelayQueue\Handler\AbstractHandler]的子类 |
||
| 49 | * @param Job $job |
||
| 50 | * @throws ClassNotFoundException |
||
| 51 | * @throws Exception |
||
| 52 | * @throws InvalidResponseBodyException |
||
| 53 | * @throws SubClassException |
||
| 54 | */ |
||
| 55 | public function push($className, Job $job) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * 从队列中取出已过期的Job |
||
| 68 | * |
||
| 69 | * @param array $topics 队列名称 |
||
| 70 | * @return null|array |
||
| 71 | * @throws Exception |
||
| 72 | * @throws InvalidResponseBodyException |
||
| 73 | */ |
||
| 74 | public function pop(array $topics) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * 从延迟队列中删除Job |
||
| 111 | * |
||
| 112 | * @param string $id Job唯一标识 |
||
| 113 | * @throws Exception |
||
| 114 | * @throws InvalidResponseBodyException |
||
| 115 | */ |
||
| 116 | View Code Duplication | public function delete($id) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Job处理完成, 确认删除 |
||
| 129 | * |
||
| 130 | * @param string $id Job唯一标识 |
||
| 131 | * @return true |
||
| 132 | * @throws Exception |
||
| 133 | * @throws InvalidResponseBodyException |
||
| 134 | */ |
||
| 135 | View Code Duplication | public function finish($id) |
|
| 145 | |||
| 146 | public function validateClassName($className) { |
||
| 156 | |||
| 157 | protected function getHttpClient() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param array $body |
||
| 174 | * @throws Exception |
||
| 175 | * @throws InvalidResponseBodyException |
||
| 176 | */ |
||
| 177 | protected function checkResponseBody(array $body) |
||
| 186 | } |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.