Completed
Push — master ( a3c7a7...bd72ff )
by Carlos
02:47
created

Stock   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 27.27 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 1
dl 21
loc 77
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 11 11 2
A reduce() 10 10 2
B getSkuInfo() 0 23 4

How to fix   Duplicated Code   

Duplicated Code

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
2
/**
3
 * Stock.php
4
 *
5
 * Part of Overtrue\Wechat.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    a939638621 <[email protected]>
11
 * @copyright 2015 a939638621 <[email protected]>
12
 * @link      https://github.com/a939638621
13
 */
14
15
namespace Overtrue\Wechat\Shop;
16
17
18
use Overtrue\Wechat\Shop\Foundation\Base;
19
use Overtrue\Wechat\Shop\Foundation\Stock as StockInterface;
20
use Overtrue\Wechat\Shop\Foundation\ShopsException;
21
22
/**
23
 * Class Stock
24
 * @package Shop
25
 */
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)
42
    {
43
        $this->response = $this->http->jsonPost(self::API_ADD,array(
44
            'product_id' => $productId,
45
            'sku_info' => is_null($skuInfo) ? '' : $this->getSkuInfo($skuInfo),
0 ignored issues
show
Bug introduced by
It seems like $skuInfo defined by parameter $skuInfo on line 41 can also be of type string; however, Overtrue\Wechat\Shop\Stock::getSkuInfo() does only seem to accept array, maybe add an additional type check?

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.

Loading history...
46
            'quantity' => $quantity
47
        ));
48
49
        return $this->getResponse();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getResponse(); of type boolean|array adds the type array to the return on line 49 which is incompatible with the return type declared by the interface Overtrue\Wechat\Shop\Foundation\Stock::add of type boolean.
Loading history...
50
51
    }
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)
63
    {
64
        $this->response = $this->http->jsonPost(self::API_REDUCE,array(
65
            'product_id' => $productId,
66
            'sku_info' => is_null($skuInfo) ? '' : $this->getSkuInfo($skuInfo),
0 ignored issues
show
Bug introduced by
It seems like $skuInfo defined by parameter $skuInfo on line 62 can also be of type string; however, Overtrue\Wechat\Shop\Stock::getSkuInfo() does only seem to accept array, maybe add an additional type check?

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.

Loading history...
67
            'quantity' => $quantity
68
        ));
69
70
        return $this->getResponse();
71
    }
72
73
    /**
74
     * 拼装 SkuInfo Str
75
     *
76
     * @param array $skuInfo
77
     * @return string
78
     */
79
    public static function getSkuInfo($skuInfo)
80
    {
81
        if (is_string($skuInfo)) return $skuInfo;
82
83
        $str = '';
84
//        array(
85
//            array('id','vid'),
86
//            array('id1','vid1'),
87
//            //.....
88
//        );
89
        $i = 0;
90
        foreach ($skuInfo as $v) {
91
            $i++;
92
            if (count($skuInfo) > $i) {
93
                $str.= $v[0].':'.$v[1].';';
94
            } else {
95
                $str.= $v[0].':'.$v[1];
96
            }
97
98
        }
99
100
        return $str;
101
    }
102
}