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 |
||
| 14 | class MessageBuilder |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Message to send. |
||
| 18 | * |
||
| 19 | * @var \EntWeChat\Message\AbstractMessage; |
||
| 20 | */ |
||
| 21 | protected $message; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Message receiver. |
||
| 25 | * |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $to; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Message sender. |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $by; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Staff instance. |
||
| 39 | * |
||
| 40 | * @var \EntWeChat\Staff\Staff |
||
| 41 | */ |
||
| 42 | protected $staff; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * User types. |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | private $userTypes = [ |
||
|
|
|||
| 50 | Staff::USER_TYPE_STAFF, |
||
| 51 | Staff::USER_TYPE_USERID, |
||
| 52 | Staff::USER_TYPE_OPENID, |
||
| 53 | ]; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * MessageBuilder constructor. |
||
| 57 | * |
||
| 58 | * @param \EntWeChat\Staff\Staff $staff |
||
| 59 | */ |
||
| 60 | public function __construct(Staff $staff) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Set message to send. |
||
| 67 | * |
||
| 68 | * @param string|AbstractMessage $message |
||
| 69 | * |
||
| 70 | * @throws InvalidArgumentException |
||
| 71 | * |
||
| 72 | * @return MessageBuilder |
||
| 73 | */ |
||
| 74 | View Code Duplication | public function message($message) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Set staff account to send message. |
||
| 87 | * |
||
| 88 | * @param string $type |
||
| 89 | * @param string $id |
||
| 90 | * |
||
| 91 | * @return MessageBuilder |
||
| 92 | */ |
||
| 93 | public function by($type, $id) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Set target user open id. |
||
| 105 | * |
||
| 106 | * @param string $type |
||
| 107 | * @param string $id |
||
| 108 | * |
||
| 109 | * @return MessageBuilder |
||
| 110 | */ |
||
| 111 | public function to($type, $id) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Send the message. |
||
| 123 | * |
||
| 124 | * @throws RuntimeException |
||
| 125 | * |
||
| 126 | * @return bool |
||
| 127 | */ |
||
| 128 | View Code Duplication | public function send() |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Return property. |
||
| 153 | * |
||
| 154 | * @param $property |
||
| 155 | * |
||
| 156 | * @return mixed |
||
| 157 | */ |
||
| 158 | public function __get($property) |
||
| 164 | } |
||
| 165 |
This check marks private properties in classes that are never used. Those properties can be removed.