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:
Complex classes like Agent often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Agent, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | abstract class Agent |
||
6 | { |
||
7 | const SUCCESS = 'success'; |
||
8 | const INFO = 'info'; |
||
9 | const CODE = 'code'; |
||
10 | |||
11 | /** |
||
12 | * The configuration information. |
||
13 | * |
||
14 | * @var array |
||
15 | */ |
||
16 | protected $config = []; |
||
17 | |||
18 | /** |
||
19 | * The custom params of request. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $params = []; |
||
24 | |||
25 | /** |
||
26 | * The result data. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $result = []; |
||
31 | |||
32 | /** |
||
33 | * Constructor. |
||
34 | * |
||
35 | * @param array $config |
||
36 | */ |
||
37 | 27 | public function __construct(array $config = []) |
|
42 | |||
43 | /** |
||
44 | * Reset states. |
||
45 | */ |
||
46 | 42 | public function reset() |
|
54 | |||
55 | /** |
||
56 | * Get or set the configuration information. |
||
57 | * |
||
58 | * @param string|array $key |
||
59 | * @param mixed $value |
||
60 | * @param bool $override |
||
61 | * |
||
62 | * @return mixed |
||
63 | */ |
||
64 | 27 | View Code Duplication | public function config($key = null, $value = null, $override = false) |
72 | |||
73 | /** |
||
74 | * Get or set the custom params. |
||
75 | * |
||
76 | * @param string|array $key |
||
77 | * @param mixed $value |
||
78 | * @param bool $override |
||
79 | * |
||
80 | * @return mixed |
||
81 | */ |
||
82 | 27 | View Code Duplication | public function params($key = null, $value = null, $override = false) |
90 | |||
91 | /** |
||
92 | * SMS send process. |
||
93 | * |
||
94 | * @param $to |
||
95 | * @param $content |
||
96 | * @param $tempId |
||
97 | * @param array $data |
||
98 | * @param array $params |
||
99 | */ |
||
100 | 24 | public function sendSms($to, $content = null, $tempId = null, array $data = [], array $params = []) |
|
112 | |||
113 | /** |
||
114 | * Voice send process. |
||
115 | * |
||
116 | * @param $to |
||
117 | * @param $content |
||
118 | * @param $tempId |
||
119 | * @param array $data |
||
120 | * @param $code |
||
121 | * @param $fileId |
||
122 | * @param array $params |
||
123 | */ |
||
124 | 6 | public function sendVoice($to, $content = null, $tempId = null, array $data = [], $code = null, $fileId = null, array $params = []) |
|
140 | |||
141 | /** |
||
142 | * Formatting a mobile number from the list of mobile numbers. |
||
143 | * |
||
144 | * @param array $list |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | public function formatMobile(array $list) |
||
154 | |||
155 | /** |
||
156 | * @codeCoverageIgnore |
||
157 | * |
||
158 | * @param $url |
||
159 | * @param array $params |
||
160 | * @param array $opts |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | public function curlPost($url, array $params = [], array $opts = []) |
||
181 | |||
182 | /** |
||
183 | * @codeCoverageIgnore |
||
184 | * |
||
185 | * @param $url |
||
186 | * @param array $params |
||
187 | * @param array $opts |
||
188 | * |
||
189 | * @return array |
||
190 | */ |
||
191 | public function curlGet($url, array $params = [], array $opts = []) |
||
200 | |||
201 | /** |
||
202 | * cURl |
||
203 | * |
||
204 | * @codeCoverageIgnore |
||
205 | * |
||
206 | * @param array $opts curl options |
||
207 | * |
||
208 | * @return array ['request', 'response'] |
||
209 | * request: Whether request success. |
||
210 | * response: Response data. |
||
211 | */ |
||
212 | public static function curl(array $opts = []) |
||
235 | |||
236 | /** |
||
237 | * Get or set the result data. |
||
238 | * |
||
239 | * @param $name |
||
240 | * @param $value |
||
241 | * |
||
242 | * @return mixed |
||
243 | */ |
||
244 | 33 | public function result($name = null, $value = null) |
|
256 | |||
257 | /** |
||
258 | * Overload object properties. |
||
259 | * |
||
260 | * @param $name |
||
261 | * |
||
262 | * @return mixed |
||
263 | */ |
||
264 | 6 | public function __get($name) |
|
268 | |||
269 | /** |
||
270 | * When using isset() or empty() on inaccessible object properties, |
||
271 | * the __isset() overloading method will be called. |
||
272 | * |
||
273 | * @param $name |
||
274 | * |
||
275 | * @return bool |
||
276 | */ |
||
277 | 3 | public function __isset($name) |
|
281 | } |
||
282 |
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.