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 |
||
11 | class Service extends Singleton |
||
12 | { |
||
13 | const CTYPE_JSON = 'application/json'; |
||
14 | const CTYPE_MULTIPART = 'multipart/form-data'; |
||
15 | const CTYPE_FORM = 'application/x-www-form-urlencoded'; |
||
16 | const CTYPE_PLAIN = 'text/plain'; |
||
17 | const PSFS_TRACK_HEADER = 'X-PSFS-UID'; |
||
18 | |||
19 | /** |
||
20 | * @var String Url de destino de la llamada |
||
21 | */ |
||
22 | private $url; |
||
23 | /** |
||
24 | * @var array Parámetros de la llamada |
||
25 | */ |
||
26 | private $params; |
||
27 | /** |
||
28 | * @var array Opciones llamada |
||
29 | */ |
||
30 | private $options; |
||
31 | /** |
||
32 | * @var array Cabeceras de la llamada |
||
33 | */ |
||
34 | private $headers; |
||
35 | /** |
||
36 | * @var string type |
||
37 | */ |
||
38 | private $type; |
||
39 | /** |
||
40 | * @var resource $con |
||
41 | */ |
||
42 | private $con; |
||
43 | /** |
||
44 | * @var string $result |
||
45 | */ |
||
46 | private $result; |
||
47 | /** |
||
48 | * @var mixed |
||
49 | */ |
||
50 | private $info = []; |
||
51 | |||
52 | /** |
||
53 | * @Injectable |
||
54 | * @var \PSFS\base\Logger Log de las llamadas |
||
55 | */ |
||
56 | protected $log; |
||
57 | /** |
||
58 | * @Injectable |
||
59 | * @var \PSFS\base\Cache $cache |
||
60 | */ |
||
61 | protected $cache; |
||
62 | /** |
||
63 | * @var bool |
||
64 | */ |
||
65 | protected $isJson = true; |
||
66 | /** |
||
67 | * @var bool |
||
68 | */ |
||
69 | protected $isMultipart = false; |
||
70 | |||
71 | 1 | private function closeConnection() { |
|
76 | |||
77 | public function __destruct() |
||
81 | |||
82 | /** |
||
83 | * @return String |
||
84 | */ |
||
85 | public function getUrl() |
||
89 | |||
90 | /** |
||
91 | * @param String $url |
||
92 | */ |
||
93 | public function setUrl($url) |
||
98 | |||
99 | /** |
||
100 | * @return string |
||
101 | */ |
||
102 | public function getResult() |
||
106 | |||
107 | /** |
||
108 | * @param string $result |
||
109 | */ |
||
110 | public function setResult($result) |
||
114 | |||
115 | /** |
||
116 | * @return array |
||
117 | */ |
||
118 | public function getParams() |
||
122 | |||
123 | /** |
||
124 | * Add request param |
||
125 | * |
||
126 | * @param $key |
||
127 | * @param null $value |
||
128 | * |
||
129 | * @return \PSFS\base\Service |
||
130 | */ |
||
131 | public function addParam($key, $value = NULL) |
||
137 | |||
138 | /** |
||
139 | * @return array |
||
140 | */ |
||
141 | public function getOptions() |
||
145 | |||
146 | /** |
||
147 | * Add request param |
||
148 | * |
||
149 | * @param $key |
||
150 | * @param null $value |
||
151 | * |
||
152 | * @return \PSFS\base\Service |
||
153 | */ |
||
154 | public function addOption($key, $value = NULL) |
||
160 | |||
161 | /** |
||
162 | * @param array $params |
||
163 | */ |
||
164 | public function setParams($params) |
||
168 | |||
169 | /** |
||
170 | * @return array |
||
171 | */ |
||
172 | public function getHeaders() |
||
176 | |||
177 | /** |
||
178 | * @param array $headers |
||
179 | */ |
||
180 | public function setHeaders($headers) |
||
184 | |||
185 | /** |
||
186 | * @param $header |
||
187 | * @param null $content |
||
188 | * |
||
189 | * @return $this |
||
190 | */ |
||
191 | public function addHeader($header, $content = NULL) |
||
197 | |||
198 | /** |
||
199 | * @return string |
||
200 | */ |
||
201 | public function getType() |
||
205 | |||
206 | /** |
||
207 | * @param string $type |
||
208 | */ |
||
209 | public function setType($type) |
||
213 | |||
214 | /** |
||
215 | * @return Logger |
||
216 | */ |
||
217 | public function getLog() |
||
221 | |||
222 | /** |
||
223 | * @param Logger $log |
||
224 | */ |
||
225 | 1 | public function setLog($log) |
|
229 | |||
230 | /** |
||
231 | * @param bool $isJson |
||
232 | */ |
||
233 | public function setIsJson($isJson = true) { |
||
239 | |||
240 | /** |
||
241 | * @return bool |
||
242 | */ |
||
243 | public function getIsJson() { |
||
246 | |||
247 | /** |
||
248 | * @param bool $isMultipart |
||
249 | */ |
||
250 | public function setIsMultipart($isMultipart = true) { |
||
256 | |||
257 | /** |
||
258 | * @return bool |
||
259 | */ |
||
260 | public function getIsMultipart() { |
||
263 | |||
264 | /** |
||
265 | * Método que limpia el contexto de la llamada |
||
266 | */ |
||
267 | 1 | private function clearContext() |
|
275 | |||
276 | /** |
||
277 | * |
||
278 | */ |
||
279 | 1 | public function init() |
|
284 | |||
285 | /** |
||
286 | * Initialize CURL |
||
287 | */ |
||
288 | private function initialize() |
||
294 | |||
295 | /** |
||
296 | * Generate auth header |
||
297 | * @param string $secret |
||
298 | * @param string $module |
||
299 | */ |
||
300 | protected function addRequestToken($secret, $module = 'PSFS') |
||
304 | |||
305 | /** |
||
306 | * @param $user |
||
307 | * @param $pass |
||
308 | */ |
||
309 | protected function addAuthHeader($user, $pass) { |
||
313 | |||
314 | protected function applyOptions() { |
||
319 | |||
320 | protected function applyHeaders() { |
||
330 | |||
331 | protected function setDefaults() |
||
383 | |||
384 | public function callSrv() |
||
409 | |||
410 | /** |
||
411 | * @return mixed |
||
412 | */ |
||
413 | public function getCallInfo() { |
||
416 | |||
417 | } |
||
418 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: