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 | /** |
||
14 | * @var String Url de destino de la llamada |
||
15 | */ |
||
16 | private $url; |
||
17 | /** |
||
18 | * @var array Parámetros de la llamada |
||
19 | */ |
||
20 | private $params; |
||
21 | /** |
||
22 | * @var array Opciones llamada |
||
23 | */ |
||
24 | private $options; |
||
25 | /** |
||
26 | * @var array Cabeceras de la llamada |
||
27 | */ |
||
28 | private $headers; |
||
29 | /** |
||
30 | * @var string type |
||
31 | */ |
||
32 | private $type; |
||
33 | /** |
||
34 | * @var resource $con |
||
35 | */ |
||
36 | private $con; |
||
37 | /** |
||
38 | * @var string $result |
||
39 | */ |
||
40 | private $result; |
||
41 | /** |
||
42 | * @var mixed |
||
43 | */ |
||
44 | private $info; |
||
45 | |||
46 | /** |
||
47 | * @Injectable |
||
48 | * @var \PSFS\base\Logger Log de las llamadas |
||
49 | */ |
||
50 | protected $log; |
||
51 | /** |
||
52 | * @Injectable |
||
53 | * @var \PSFS\base\Cache $cache |
||
54 | */ |
||
55 | protected $cache; |
||
56 | /** |
||
57 | * @var bool |
||
58 | */ |
||
59 | protected $isJson = true; |
||
60 | |||
61 | 1 | private function closeConnection() { |
|
66 | |||
67 | public function __destruct() |
||
71 | |||
72 | /** |
||
73 | * @return String |
||
74 | */ |
||
75 | public function getUrl() |
||
79 | |||
80 | /** |
||
81 | * @param String $url |
||
82 | */ |
||
83 | public function setUrl($url) |
||
88 | |||
89 | /** |
||
90 | * @return string |
||
91 | */ |
||
92 | public function getResult() |
||
96 | |||
97 | /** |
||
98 | * @param string $result |
||
99 | */ |
||
100 | public function setResult($result) |
||
104 | |||
105 | /** |
||
106 | * @return array |
||
107 | */ |
||
108 | public function getParams() |
||
112 | |||
113 | /** |
||
114 | * Add request param |
||
115 | * |
||
116 | * @param $key |
||
117 | * @param null $value |
||
118 | * |
||
119 | * @return \PSFS\base\Service |
||
120 | */ |
||
121 | public function addParam($key, $value = NULL) |
||
127 | |||
128 | /** |
||
129 | * @return array |
||
130 | */ |
||
131 | public function getOptions() |
||
135 | |||
136 | /** |
||
137 | * Add request param |
||
138 | * |
||
139 | * @param $key |
||
140 | * @param null $value |
||
141 | * |
||
142 | * @return \PSFS\base\Service |
||
143 | */ |
||
144 | public function addOption($key, $value = NULL) |
||
150 | |||
151 | /** |
||
152 | * @param array $params |
||
153 | */ |
||
154 | public function setParams($params) |
||
158 | |||
159 | /** |
||
160 | * @return array |
||
161 | */ |
||
162 | public function getHeaders() |
||
166 | |||
167 | /** |
||
168 | * @param array $headers |
||
169 | */ |
||
170 | public function setHeaders($headers) |
||
174 | |||
175 | /** |
||
176 | * @param $header |
||
177 | * @param null $content |
||
178 | * |
||
179 | * @return $this |
||
180 | */ |
||
181 | public function addHeader($header, $content = NULL) |
||
187 | |||
188 | /** |
||
189 | * @return string |
||
190 | */ |
||
191 | public function getType() |
||
195 | |||
196 | /** |
||
197 | * @param string $type |
||
198 | */ |
||
199 | public function setType($type) |
||
203 | |||
204 | /** |
||
205 | * @return Logger |
||
206 | */ |
||
207 | public function getLog() |
||
211 | |||
212 | /** |
||
213 | * @param Logger $log |
||
214 | */ |
||
215 | 1 | public function setLog($log) |
|
219 | |||
220 | /** |
||
221 | * @param bool $isJson |
||
222 | */ |
||
223 | public function setIsJson($isJson = true) { |
||
226 | |||
227 | /** |
||
228 | * @return bool |
||
229 | */ |
||
230 | public function getIsJson() { |
||
233 | |||
234 | /** |
||
235 | * Método que limpia el contexto de la llamada |
||
236 | */ |
||
237 | 1 | private function clearContext() |
|
245 | |||
246 | /** |
||
247 | * |
||
248 | */ |
||
249 | 1 | public function init() |
|
254 | |||
255 | /** |
||
256 | * Initialize CURL |
||
257 | */ |
||
258 | private function initialize() |
||
263 | |||
264 | /** |
||
265 | * Generate auth header |
||
266 | * @param string $secret |
||
267 | * @param string $module |
||
268 | */ |
||
269 | protected function addRequestToken($secret, $module = 'PSFS') |
||
273 | |||
274 | /** |
||
275 | * @param $user |
||
276 | * @param $pass |
||
277 | */ |
||
278 | protected function addAuthHeader($user, $pass) { |
||
282 | |||
283 | protected function applyOptions() { |
||
288 | |||
289 | protected function applyHeaders() { |
||
298 | |||
299 | protected function setDefaults() |
||
345 | |||
346 | public function callSrv() |
||
370 | |||
371 | /** |
||
372 | * @return mixed |
||
373 | */ |
||
374 | public function getCallInfo() { |
||
377 | |||
378 | } |
||
379 |
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: