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 |
||
28 | class TopFee extends MagicAttributes |
||
29 | { |
||
30 | /** |
||
31 | * 设置TopFee |
||
32 | * |
||
33 | * @param string $type |
||
34 | * |
||
35 | * @return $this |
||
36 | * |
||
37 | * @throws ShopsException |
||
38 | */ |
||
39 | public function setTopFee($type = Postage::KUAI_DI) |
||
40 | { |
||
41 | if (!isset($this->normal) && !isset($this->custom)) { |
||
42 | throw new ShopsException('normal 和 custom 必须设置'); |
||
43 | } |
||
44 | if (!isset($this->normal) || !isset($this->custom)) { |
||
45 | throw new ShopsException('normal 和 custom 必须全部设置'); |
||
46 | } |
||
47 | $keys = array_keys($this->custom); |
||
|
|||
48 | |||
49 | foreach ($keys as $v) { |
||
50 | if (!is_numeric($v)) { |
||
51 | throw new ShopsException('数据不合法'); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | $this->attributes['topFee'][] = array( |
||
56 | 'Type' => $type, |
||
57 | 'Normal' => $this->normal, |
||
58 | 'Custom' => $this->custom, |
||
59 | ); |
||
60 | |||
61 | return $this; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * 设置 Normal |
||
66 | * |
||
67 | * @param $startStandards |
||
68 | * @param $startFees |
||
69 | * @param $addStandards |
||
70 | * @param $addFees |
||
71 | * |
||
72 | * @return $this |
||
73 | */ |
||
74 | public function setNormal($startStandards, $startFees, $addStandards, $addFees) |
||
75 | { |
||
76 | $this->normal = array( |
||
77 | 'StartStandards' => $startStandards, |
||
78 | 'StartFees' => $startFees, |
||
79 | 'AddStandards' => $addStandards, |
||
80 | 'AddFees' => $addFees, |
||
81 | ); |
||
82 | |||
83 | return $this; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * 设置Custom |
||
88 | * |
||
89 | * @param $startStandards |
||
90 | * @param $startFees |
||
91 | * @param $addStandards |
||
92 | * @param $addFees |
||
93 | * @param $destProvince |
||
94 | * @param null $destCity |
||
95 | * @param string $destCountry |
||
96 | * |
||
97 | * @return $this |
||
98 | * |
||
99 | * @throws ShopsException |
||
100 | */ |
||
101 | public function setCustom( |
||
102 | $startStandards, $startFees, $addStandards, $addFees, |
||
103 | $destProvince, $destCity = null, $destCountry = '中国' |
||
104 | ) { |
||
105 | if (empty($destProvince)) { |
||
106 | throw new ShopsException('$destProvince不允许为空'); |
||
107 | } |
||
108 | |||
109 | $this->custom = array(); |
||
110 | |||
111 | //todo 未做反选,排除一个城市,选择其他 |
||
112 | |||
113 | if (empty($destCity)) { |
||
114 | |||
115 | //todo $destProvince的判断 |
||
116 | //todo 加入 全国省直辖市的 简称等 |
||
117 | //todo 加入 某些不平等条约的存在 例如 江浙沪 ,你们懂得!! |
||
118 | |||
119 | $destProvince = is_string($destProvince) ? array($destProvince) : $destProvince; |
||
120 | |||
121 | $regional = new Regional(); |
||
122 | |||
123 | foreach ($destProvince as $province) { |
||
124 | $citys = $regional->getCity($province); |
||
125 | |||
126 | if (empty($citys)) { |
||
127 | throw new ShopsException('请传入合法的省份名!!!'); |
||
128 | } |
||
129 | |||
130 | View Code Duplication | foreach ($citys[0] as $city) { |
|
131 | $this->attributes['custom'][] = array( |
||
132 | 'StartStandards' => $startStandards, |
||
133 | 'StartFees' => $startFees, |
||
134 | 'AddStandards' => $addStandards, |
||
135 | 'AddFees' => $addFees, |
||
136 | 'DestCountry' => $destCountry, |
||
137 | 'DestProvince' => $province, |
||
138 | 'DestCity' => $city, |
||
139 | ); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | return $this; |
||
144 | } else { |
||
145 | |||
146 | //todo 未做省份的检测 |
||
147 | //todo 未做市检测 |
||
148 | |||
149 | $destCity = is_string($destCity) ? array($destCity) : $destCity; |
||
150 | |||
151 | if (is_array($destCity)) { |
||
152 | View Code Duplication | foreach ($destCity as $city) { |
|
153 | $this->attributes['custom'][] = array( |
||
154 | 'StartStandards' => $startStandards, |
||
155 | 'StartFees' => $startFees, |
||
156 | 'AddStandards' => $addStandards, |
||
157 | 'AddFees' => $addFees, |
||
158 | 'DestCountry' => $destCountry, |
||
159 | 'DestProvince' => $destProvince, |
||
160 | 'DestCity' => $city, |
||
161 | ); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | return $this; |
||
166 | } |
||
167 | } |
||
168 | |||
169 | public function __get($property) |
||
173 | |||
174 | public function __isset($property) |
||
178 | } |
||
179 |
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.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.