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

TopFee   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 150
Duplicated Lines 16 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
c 1
b 0
f 0
lcom 1
cbo 3
dl 24
loc 150
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B setTopFee() 0 19 7
A setNormal() 0 11 1
C setCustom() 24 75 10
A __get() 0 4 1
A __isset() 0 4 1

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
 * TopFee.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\Data;
16
17
18
use Overtrue\Wechat\Utils\MagicAttributes;
19
use Overtrue\Wechat\Shop\Foundation\ShopsException;
20
use Overtrue\Wechat\Shop\Postage;
21
22
/**
23
 * Class TopFee
24
 * @package Shop\Data
25
 * @property $custom
26
 * @property $normal
27
 * @property $topFee
28
 */
29
class TopFee extends MagicAttributes
30
{
31
32
    /**
33
     * 设置TopFee
34
     *
35
     * @param string $type
36
     * @return $this
37
     * @throws ShopsException
38
     */
39
    public function setTopFee($type = Postage::KUAI_DI)
40
    {
41
42
        if (!isset($this->normal) && !isset($this->custom)) throw new ShopsException('normal 和 custom 必须设置');
43
        if (!isset($this->normal) || !isset($this->custom)) throw new ShopsException('normal 和 custom 必须全部设置');
44
        $keys = array_keys($this->custom);
0 ignored issues
show
Documentation introduced by
The property custom does not exist on object<Overtrue\Wechat\Shop\Data\TopFee>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
45
46
        foreach ($keys as $v) {
47
            if (!is_numeric($v)) throw new ShopsException('数据不合法');
48
        }
49
50
        $this->attributes['topFee'][] = array(
51
            'Type' => $type,
52
            'Normal' => $this->normal,
0 ignored issues
show
Documentation introduced by
The property normal does not exist on object<Overtrue\Wechat\Shop\Data\TopFee>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
53
            'Custom' => $this->custom
0 ignored issues
show
Documentation introduced by
The property custom does not exist on object<Overtrue\Wechat\Shop\Data\TopFee>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
54
        );
55
56
        return $this;
57
    }
58
59
    /**
60
     * 设置 Normal
61
     *
62
     * @param $startStandards
63
     * @param $startFees
64
     * @param $addStandards
65
     * @param $addFees
66
     * @return $this
67
     */
68
    public function setNormal($startStandards, $startFees, $addStandards, $addFees)
69
    {
70
        $this->normal = array(
0 ignored issues
show
Documentation introduced by
The property normal does not exist on object<Overtrue\Wechat\Shop\Data\TopFee>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
71
            'StartStandards' => $startStandards,
72
            'StartFees' => $startFees,
73
            'AddStandards' => $addStandards,
74
            'AddFees' => $addFees
75
        );
76
77
        return $this;
78
    }
79
80
    /**
81
     * 设置Custom
82
     *
83
     * @param $startStandards
84
     * @param $startFees
85
     * @param $addStandards
86
     * @param $addFees
87
     * @param $destProvince
88
     * @param null $destCity
89
     * @param string $destCountry
90
     * @return $this
91
     * @throws ShopsException
92
     */
93
    public function setCustom(
94
        $startStandards, $startFees, $addStandards, $addFees,
95
        $destProvince, $destCity = null,$destCountry = '中国'
96
    )
97
    {
98
99
        if (empty($destProvince)) throw new ShopsException('$destProvince不允许为空');
100
101
        $this->custom = array();
0 ignored issues
show
Documentation introduced by
The property custom does not exist on object<Overtrue\Wechat\Shop\Data\TopFee>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
102
103
        //todo 未做反选,排除一个城市,选择其他
104
105
        if (empty($destCity)) {
106
107
            //todo  $destProvince的判断
108
            //todo  加入 全国省直辖市的 简称等
109
            //todo 加入 某些不平等条约的存在 例如 江浙沪 ,你们懂得!!
110
111
112
113
            $destProvince = is_string($destProvince) ? array($destProvince) : $destProvince;
114
115
            $regional = new Regional();
116
117
            foreach ($destProvince as $province) {
118
119
120
                $citys = $regional->getCity($province);
121
122
                if (empty($citys)) throw new ShopsException('请传入合法的省份名!!!');
123
124 View Code Duplication
                foreach ($citys[0] as $city) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
125
                    $this->attributes['custom'][] = array(
126
                        'StartStandards' => $startStandards,
127
                        'StartFees' => $startFees,
128
                        'AddStandards' => $addStandards,
129
                        'AddFees' => $addFees,
130
                        'DestCountry' => $destCountry,
131
                        'DestProvince' => $province,
132
                        'DestCity' => $city
133
                    );
134
                }
135
136
            }
137
138
            return $this;
139
140
        } else {
141
142
            //todo 未做省份的检测
143
            //todo 未做市检测
144
145
            $destCity = is_string($destCity) ? array($destCity) : $destCity;
146
147
            if (is_array($destCity)) {
148 View Code Duplication
                foreach ($destCity as $city) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
149
150
                    $this->attributes['custom'][] = array(
151
                        'StartStandards' => $startStandards,
152
                        'StartFees' => $startFees,
153
                        'AddStandards' => $addStandards,
154
                        'AddFees' => $addFees,
155
                        'DestCountry' => $destCountry,
156
                        'DestProvince' => $destProvince,
157
                        'DestCity' => $city
158
                    );
159
160
                }
161
            }
162
163
164
            return $this;
165
166
        }
167
    }
168
169
    public function __get($property)
170
    {
171
        return parent::__get($property);
172
    }
173
174
    public function __isset($property)
175
    {
176
        return isset($this->attributes[$this->snake($property)]);
177
    }
178
}