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 Service 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 Service, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Service extends Singleton |
||
11 | { |
||
12 | /** |
||
13 | * @var String Url de destino de la llamada |
||
14 | */ |
||
15 | private $url; |
||
16 | /** |
||
17 | * @var array Parámetros de la llamada |
||
18 | */ |
||
19 | private $params; |
||
20 | /** |
||
21 | * @var array Opciones llamada |
||
22 | */ |
||
23 | private $options; |
||
24 | /** |
||
25 | * @var array Cabeceras de la llamada |
||
26 | */ |
||
27 | private $headers; |
||
28 | /** |
||
29 | * @var string type |
||
30 | */ |
||
31 | private $type; |
||
32 | /** |
||
33 | * @var resource $con |
||
34 | */ |
||
35 | private $con; |
||
36 | /** |
||
37 | * @var string $result |
||
38 | */ |
||
39 | private $result; |
||
40 | /** |
||
41 | * @var mixed |
||
42 | */ |
||
43 | private $info; |
||
44 | |||
45 | /** |
||
46 | * @Inyectable |
||
47 | * @var \PSFS\base\Logger Log de las llamadas |
||
48 | */ |
||
49 | protected $log; |
||
50 | /** |
||
51 | * @Inyectable |
||
52 | * @var \PSFS\base\Cache $cache |
||
53 | */ |
||
54 | protected $cache; |
||
55 | /** |
||
56 | * @var bool |
||
57 | */ |
||
58 | protected $isJson = true; |
||
59 | |||
60 | 1 | private function closeConnection() { |
|
65 | |||
66 | public function __destruct() |
||
70 | |||
71 | /** |
||
72 | * @return String |
||
73 | */ |
||
74 | public function getUrl() |
||
78 | |||
79 | /** |
||
80 | * @param String $url |
||
81 | */ |
||
82 | public function setUrl($url) |
||
87 | |||
88 | /** |
||
89 | * @return string |
||
90 | */ |
||
91 | public function getResult() |
||
95 | |||
96 | /** |
||
97 | * @param string $result |
||
98 | */ |
||
99 | public function setResult($result) |
||
103 | |||
104 | /** |
||
105 | * @return array |
||
106 | */ |
||
107 | public function getParams() |
||
111 | |||
112 | /** |
||
113 | * Add request param |
||
114 | * |
||
115 | * @param $key |
||
116 | * @param null $value |
||
117 | * |
||
118 | * @return \PSFS\base\Service |
||
119 | */ |
||
120 | public function addParam($key, $value = NULL) |
||
126 | |||
127 | /** |
||
128 | * @return array |
||
129 | */ |
||
130 | public function getOptions() |
||
134 | |||
135 | /** |
||
136 | * Add request param |
||
137 | * |
||
138 | * @param $key |
||
139 | * @param null $value |
||
140 | * |
||
141 | * @return \PSFS\base\Service |
||
142 | */ |
||
143 | public function addOption($key, $value = NULL) |
||
149 | |||
150 | /** |
||
151 | * @param array $params |
||
152 | */ |
||
153 | public function setParams($params) |
||
157 | |||
158 | /** |
||
159 | * @return array |
||
160 | */ |
||
161 | public function getHeaders() |
||
165 | |||
166 | /** |
||
167 | * @param array $headers |
||
168 | */ |
||
169 | public function setHeaders($headers) |
||
173 | |||
174 | /** |
||
175 | * @param $header |
||
176 | * @param null $content |
||
177 | * |
||
178 | * @return $this |
||
179 | */ |
||
180 | public function addHeader($header, $content = NULL) |
||
186 | |||
187 | /** |
||
188 | * @return string |
||
189 | */ |
||
190 | public function getType() |
||
194 | |||
195 | /** |
||
196 | * @param string $type |
||
197 | */ |
||
198 | public function setType($type) |
||
202 | |||
203 | /** |
||
204 | * @return Logger |
||
205 | */ |
||
206 | public function getLog() |
||
210 | |||
211 | /** |
||
212 | * @param Logger $log |
||
213 | */ |
||
214 | 1 | public function setLog($log) |
|
218 | |||
219 | /** |
||
220 | * @param bool $isJson |
||
221 | */ |
||
222 | public function setIsJson($isJson = true) { |
||
225 | |||
226 | /** |
||
227 | * @return bool |
||
228 | */ |
||
229 | public function getIsJson() { |
||
232 | |||
233 | /** |
||
234 | * Método que limpia el contexto de la llamada |
||
235 | */ |
||
236 | 1 | private function clearContext() |
|
244 | |||
245 | /** |
||
246 | * |
||
247 | */ |
||
248 | 1 | public function init() |
|
253 | |||
254 | /** |
||
255 | * Initialize CURL |
||
256 | */ |
||
257 | private function initialize() |
||
262 | |||
263 | /** |
||
264 | * Generate auth header |
||
265 | * @param string $secret |
||
266 | * @param string $module |
||
267 | */ |
||
268 | protected function addRequestToken($secret, $module = 'PSFS') |
||
272 | |||
273 | /** |
||
274 | * @param $user |
||
275 | * @param $pass |
||
276 | */ |
||
277 | protected function addAuthHeader($user, $pass) { |
||
281 | |||
282 | protected function applyOptions() { |
||
285 | |||
286 | protected function applyHeaders() { |
||
293 | |||
294 | protected function setDefaults() |
||
334 | |||
335 | public function callSrv() |
||
345 | |||
346 | /** |
||
347 | * @return mixed |
||
348 | */ |
||
349 | public function getCallInfo() { |
||
352 | } |
||
353 |