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 |
||
| 13 | class MessageBuilder |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Message target user or group. |
||
| 17 | * |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | protected $to = []; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Message type. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $msgType; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Agent id. |
||
| 31 | * |
||
| 32 | * @var mixed |
||
| 33 | */ |
||
| 34 | protected $agentId; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Message. |
||
| 38 | * |
||
| 39 | * @var mixed |
||
| 40 | */ |
||
| 41 | protected $message; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Safe message. |
||
| 45 | * |
||
| 46 | * @var mixed |
||
| 47 | */ |
||
| 48 | protected $safe = 0; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Broadcast instance. |
||
| 52 | * |
||
| 53 | * @var \EntWeChat\Broadcast\Broadcast |
||
| 54 | */ |
||
| 55 | protected $broadcast; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Message types. |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | private $msgTypes = [ |
||
|
|
|||
| 63 | Broadcast::MSG_TYPE_TEXT, |
||
| 64 | Broadcast::MSG_TYPE_NEWS, |
||
| 65 | Broadcast::MSG_TYPE_IMAGE, |
||
| 66 | Broadcast::MSG_TYPE_VIDEO, |
||
| 67 | Broadcast::MSG_TYPE_VOICE, |
||
| 68 | Broadcast::MSG_TYPE_CARD, |
||
| 69 | Broadcast::MSG_TYPE_FILE, |
||
| 70 | Broadcast::MSG_TYPE_MPNEWS, |
||
| 71 | Broadcast::MSG_TYPE_TEXTCARD, |
||
| 72 | ]; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * MessageBuilder constructor. |
||
| 76 | * |
||
| 77 | * @param \EntWeChat\Broadcast\Broadcast $broadcast |
||
| 78 | */ |
||
| 79 | public function __construct(Broadcast $broadcast) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Set message. |
||
| 86 | * |
||
| 87 | * @param string|array $message |
||
| 88 | * |
||
| 89 | * @return $this |
||
| 90 | */ |
||
| 91 | View Code Duplication | public function message($message) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Set agent to send message. |
||
| 104 | * |
||
| 105 | * @param $agentId |
||
| 106 | * |
||
| 107 | * @return $this |
||
| 108 | */ |
||
| 109 | public function by($agentId) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Set target user or group. |
||
| 118 | * |
||
| 119 | * @param array $to |
||
| 120 | * |
||
| 121 | * @return $this |
||
| 122 | */ |
||
| 123 | public function to(array $to) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Message target all. |
||
| 132 | * |
||
| 133 | * @return $this |
||
| 134 | */ |
||
| 135 | public function toAll() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Message target user. |
||
| 144 | * |
||
| 145 | * @return $this |
||
| 146 | */ |
||
| 147 | View Code Duplication | public function toUser() |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Message target party. |
||
| 160 | * |
||
| 161 | * @return $this |
||
| 162 | */ |
||
| 163 | View Code Duplication | public function toParty() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Message target tag. |
||
| 176 | * |
||
| 177 | * @return $this |
||
| 178 | */ |
||
| 179 | View Code Duplication | public function toTag() |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Use safe message. |
||
| 192 | * |
||
| 193 | * @return $this |
||
| 194 | */ |
||
| 195 | public function safe() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Send the message. |
||
| 204 | * |
||
| 205 | * @throws RuntimeException |
||
| 206 | * |
||
| 207 | * @return bool |
||
| 208 | */ |
||
| 209 | View Code Duplication | public function send() |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Return property. |
||
| 230 | * |
||
| 231 | * @param string $property |
||
| 232 | * |
||
| 233 | * @return mixed |
||
| 234 | */ |
||
| 235 | public function __get($property) |
||
| 241 | } |
||
| 242 |
This check marks private properties in classes that are never used. Those properties can be removed.