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 |
||
26 | class Stock extends Base implements StockInterface |
||
27 | { |
||
28 | |||
29 | const API_ADD = 'https://api.weixin.qq.com/merchant/stock/add'; |
||
30 | const API_REDUCE = 'https://api.weixin.qq.com/merchant/stock/reduce'; |
||
31 | |||
32 | /** |
||
33 | * 增加库存 |
||
34 | * |
||
35 | * @param $productId |
||
36 | * @param $quantity |
||
37 | * @param string|array $skuInfo |
||
38 | * @return bool |
||
39 | * @throws ShopsException |
||
40 | */ |
||
41 | View Code Duplication | public function add($productId, $quantity, $skuInfo = null) |
|
52 | |||
53 | /** |
||
54 | * 减少库存 |
||
55 | * |
||
56 | * @param array $productId |
||
57 | * @param string|array $skuInfo |
||
58 | * @param int $quantity |
||
59 | * @return bool |
||
60 | * @throws ShopsException |
||
61 | */ |
||
62 | View Code Duplication | public function reduce($productId, $quantity, $skuInfo = null) |
|
72 | |||
73 | /** |
||
74 | * 拼装 SkuInfo Str |
||
75 | * |
||
76 | * @param array $skuInfo |
||
77 | * @return string |
||
78 | */ |
||
79 | public static function getSkuInfo($skuInfo) |
||
102 | } |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.