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 |
||
5 | abstract class Sms |
||
6 | { |
||
7 | protected $signName; |
||
8 | protected $templateId; |
||
9 | protected $content; |
||
10 | protected $templateVar = []; |
||
11 | protected $verifyCode; |
||
12 | |||
13 | /** |
||
14 | * @return string |
||
15 | */ |
||
16 | private function getAgentName() |
||
29 | |||
30 | /** |
||
31 | * @return string |
||
32 | */ |
||
33 | protected function getTemplateContentByConfig() |
||
38 | |||
39 | /** |
||
40 | * @param integer $time |
||
41 | */ |
||
42 | public function setContentByVerifyCode($time = null) |
||
53 | |||
54 | /** |
||
55 | * @param array $templateVar |
||
56 | */ |
||
57 | public function setContentByCustomVar($templateVar = []) |
||
72 | |||
73 | /** |
||
74 | * @param string $content |
||
75 | */ |
||
76 | public function setContent($content) |
||
80 | |||
81 | /** |
||
82 | * @return string |
||
83 | */ |
||
84 | public function getContent() |
||
88 | |||
89 | /** |
||
90 | * @param array $templateVar |
||
91 | * @param bool $hasKey |
||
92 | */ |
||
93 | public function setTemplateVar($templateVar = [], $hasKey = false) |
||
94 | { |
||
95 | $count = count($templateVar); |
||
96 | |||
97 | if (is_array($templateVar) && $count > 0) { |
||
98 | foreach ($templateVar as $key => $value) { |
||
99 | if ($hasKey) { |
||
100 | View Code Duplication | if ($value == 'verifyCode') { |
|
|
|||
101 | $this->verifyCode = $this->makeRandom(); |
||
102 | $this->templateVar[$key] = "{$this->verifyCode}"; |
||
103 | } else { |
||
104 | $this->templateVar[$key] = "{$value}"; |
||
105 | } |
||
106 | View Code Duplication | } else { |
|
107 | if ($value == 'verifyCode') { |
||
108 | $this->verifyCode = $this->makeRandom(); |
||
109 | $this->templateVar[] = "{$this->verifyCode}"; |
||
110 | } else { |
||
111 | $this->templateVar[] = "{$value}"; |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | } else { |
||
116 | $this->templateVar = []; |
||
117 | } |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @return string |
||
122 | */ |
||
123 | public function getTemplateVar() |
||
127 | |||
128 | /** |
||
129 | * @param string $signName |
||
130 | */ |
||
131 | public function setSignName($signName = null) |
||
135 | |||
136 | /** |
||
137 | * @return string |
||
138 | */ |
||
139 | public function getSignName() |
||
143 | |||
144 | /** |
||
145 | * @param mixed $id |
||
146 | */ |
||
147 | public function setTemplateId($id = null) |
||
151 | |||
152 | /** |
||
153 | * @return string |
||
154 | */ |
||
155 | public function getTemplateId() |
||
159 | |||
160 | /** |
||
161 | * @return void |
||
162 | */ |
||
163 | abstract protected function transformConfig(); |
||
164 | |||
165 | /** |
||
166 | * @param string $mobile |
||
167 | * @param bool $send |
||
168 | * @return mixed |
||
169 | */ |
||
170 | abstract protected function singlesSend($mobile, $send = true); |
||
171 | |||
172 | /** |
||
173 | * @param string $url |
||
174 | * @param array $params |
||
175 | * @return array |
||
176 | */ |
||
177 | abstract protected function curl($url, $params); |
||
178 | |||
179 | /** |
||
180 | * @param $ch |
||
181 | * @return array |
||
182 | */ |
||
183 | abstract protected function transformerResponse($ch); |
||
184 | |||
185 | /** |
||
186 | * @param $ch |
||
187 | * @return array |
||
188 | */ |
||
189 | protected function httpResponse($ch) |
||
205 | |||
206 | /** |
||
207 | * @return int |
||
208 | */ |
||
209 | protected function makeRandom() |
||
213 | } |
||
214 |
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.