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 |
||
32 | class Payment |
||
33 | { |
||
34 | /** |
||
35 | * Scheme base path. |
||
36 | */ |
||
37 | const SCHEME_PATH = 'weixin://wxpay/bizpayurl'; |
||
38 | |||
39 | /** |
||
40 | * @var API |
||
41 | */ |
||
42 | protected $api; |
||
43 | |||
44 | /** |
||
45 | * Merchant instance. |
||
46 | * |
||
47 | * @var \EasyWeChat\Payment\Merchant |
||
48 | */ |
||
49 | protected $merchant; |
||
50 | |||
51 | /** |
||
52 | * Constructor. |
||
53 | * |
||
54 | * @param Merchant $merchant |
||
55 | */ |
||
56 | 7 | public function __construct(Merchant $merchant) |
|
60 | |||
61 | /** |
||
62 | * Build payment scheme for product. |
||
63 | * |
||
64 | * @param string $productId |
||
65 | * |
||
66 | * @return string |
||
67 | */ |
||
68 | 1 | public function scheme($productId) |
|
82 | |||
83 | /** |
||
84 | * Handle payment notify. |
||
85 | * |
||
86 | * @param callable $callback |
||
87 | * |
||
88 | * @return Response |
||
89 | */ |
||
90 | 2 | public function handleNotify(callable $callback) |
|
117 | |||
118 | /** |
||
119 | * Gernerate js config for payment. |
||
120 | * |
||
121 | * @param string $prepayId |
||
122 | * @param bool $json |
||
123 | * |
||
124 | * @return string|array |
||
125 | */ |
||
126 | 1 | public function configForPayment($prepayId, $json = true) |
|
140 | |||
141 | /** |
||
142 | * Generate js config for share user address. |
||
143 | * |
||
144 | * @param string|accessToken $accessToken |
||
145 | * @param bool $json |
||
146 | * |
||
147 | * @return string|array |
||
148 | */ |
||
149 | 1 | public function configForShareAddress($accessToken, $json = true) |
|
175 | |||
176 | /** |
||
177 | * Merchant setter. |
||
178 | * |
||
179 | * @param Merchant $merchant |
||
180 | */ |
||
181 | 1 | public function setMerchant(Merchant $merchant) |
|
185 | |||
186 | /** |
||
187 | * Merchant getter. |
||
188 | * |
||
189 | * @return Merchant |
||
190 | */ |
||
191 | 1 | public function getMerchant() |
|
195 | |||
196 | /** |
||
197 | * Return Notify instance. |
||
198 | * |
||
199 | * @return \EasyWeChat\Payment\Notify |
||
200 | */ |
||
201 | 1 | public function getNotify() |
|
205 | |||
206 | /** |
||
207 | * API setter. |
||
208 | * |
||
209 | * @param API $api |
||
210 | */ |
||
211 | 1 | public function setAPI(API $api) |
|
215 | |||
216 | /** |
||
217 | * Return API instance. |
||
218 | * |
||
219 | * @return API |
||
220 | */ |
||
221 | 1 | public function getAPI() |
|
225 | |||
226 | /** |
||
227 | * Magic call. |
||
228 | * |
||
229 | * @param string $method |
||
230 | * @param array $args |
||
231 | * |
||
232 | * @return mixed |
||
233 | * |
||
234 | * @codeCoverageIgnore |
||
235 | */ |
||
236 | public function __call($method, $args) |
||
242 | } |
||
243 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.