Complex classes like Configuracao 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 Configuracao, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class Configuracao |
||
|
|||
37 | { |
||
38 | |||
39 | private $banco; |
||
40 | private $emitente; |
||
41 | private $evento; |
||
42 | private $chave_publica; |
||
43 | private $chave_privada; |
||
44 | private $arquivo_chave_publica; |
||
45 | private $arquivo_chave_privada; |
||
46 | private $expiracao; |
||
47 | private $token; |
||
48 | private $csc; |
||
49 | private $token_ibpt; |
||
50 | private $tempo_limite; |
||
51 | private $sincrono; |
||
52 | private $offline; |
||
53 | |||
54 | 87 | public function __construct($configuracao = array()) |
|
58 | |||
59 | /** |
||
60 | * Banco que fornece informações sobre items da nota como: Códigos e Taxas |
||
61 | */ |
||
62 | 62 | public function getBanco() |
|
66 | |||
67 | 87 | public function setBanco($banco) |
|
72 | |||
73 | /** |
||
74 | * Emitente da nota fiscal |
||
75 | */ |
||
76 | 38 | public function getEmitente() |
|
80 | |||
81 | 87 | public function setEmitente($emitente) |
|
86 | |||
87 | /** |
||
88 | * Informa a instancia que receberá os eventos do processamento das notas |
||
89 | */ |
||
90 | 4 | public function getEvento() |
|
94 | |||
95 | 87 | public function setEvento($evento) |
|
100 | |||
101 | /** |
||
102 | * Conteúdo da chave pública ou certificado no formato PEM |
||
103 | */ |
||
104 | 87 | public function getChavePublica() |
|
108 | |||
109 | 87 | public function setChavePublica($chave_publica) |
|
115 | |||
116 | /** |
||
117 | * Conteúdo da chave privada do certificado no formato PEM |
||
118 | */ |
||
119 | 28 | public function getChavePrivada() |
|
123 | |||
124 | 87 | public function setChavePrivada($chave_privada) |
|
129 | |||
130 | /** |
||
131 | * Informa o caminho do arquivo da chave pública ou certificado no formato |
||
132 | * PEM |
||
133 | */ |
||
134 | 20 | public function getArquivoChavePublica() |
|
138 | |||
139 | 87 | public function setArquivoChavePublica($arquivo_chave_publica) |
|
140 | { |
||
141 | 87 | $this->arquivo_chave_publica = $arquivo_chave_publica; |
|
142 | 87 | if (file_exists($arquivo_chave_publica)) { |
|
143 | 42 | $this->setChavePublica(file_get_contents($arquivo_chave_publica)); |
|
144 | } |
||
145 | 87 | return $this; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * Caminho do arquivo da chave privada do certificado no formato PEM |
||
150 | */ |
||
151 | 20 | public function getArquivoChavePrivada() |
|
155 | |||
156 | 87 | public function setArquivoChavePrivada($arquivo_chave_privada) |
|
157 | { |
||
158 | 87 | $this->arquivo_chave_privada = $arquivo_chave_privada; |
|
159 | 87 | if (file_exists($arquivo_chave_privada)) { |
|
160 | 42 | $this->setChavePrivada(file_get_contents($arquivo_chave_privada)); |
|
161 | } |
||
162 | 87 | return $this; |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * Data de expiração do certificado em timestamp |
||
167 | */ |
||
168 | 2 | public function getExpiracao() |
|
172 | |||
173 | 87 | private function setExpiracao($expiracao) |
|
178 | |||
179 | /** |
||
180 | * Token do CSC |
||
181 | */ |
||
182 | 26 | public function getToken() |
|
186 | |||
187 | 87 | public function setToken($token) |
|
192 | |||
193 | /** |
||
194 | * Código do contribuinte para emissão de nota fiscal |
||
195 | */ |
||
196 | 26 | public function getCSC() |
|
200 | |||
201 | 87 | public function setCSC($csc) |
|
206 | |||
207 | /** |
||
208 | * Token IBPT para consulta de impostos online |
||
209 | */ |
||
210 | 31 | public function getTokenIBPT() |
|
214 | |||
215 | 87 | public function setTokenIBPT($token_ibpt) |
|
220 | |||
221 | /** |
||
222 | * Tempo limite em segundos nas conexões com os Web services, 0 para sem tempo limite |
||
223 | */ |
||
224 | 20 | public function getTempoLimite() |
|
228 | |||
229 | 87 | public function setTempoLimite($tempo_limite) |
|
234 | |||
235 | /** |
||
236 | * Informa se o processo de autorização da nota é síncrono ou assíncrono |
||
237 | */ |
||
238 | 4 | public function getSincrono($normalize = false) |
|
245 | |||
246 | /** |
||
247 | * Informa se o processo de autorização da nota é síncrono ou assíncrono |
||
248 | */ |
||
249 | 3 | public function isSincrono() |
|
253 | |||
254 | 87 | public function setSincrono($sincrono) |
|
255 | { |
||
256 | 87 | if (!in_array($sincrono, array('N', 'Y'))) { |
|
257 | $sincrono = $sincrono?'Y':'N'; |
||
258 | } |
||
259 | 87 | $this->sincrono = $sincrono; |
|
260 | 87 | return $this; |
|
261 | } |
||
262 | |||
263 | /** |
||
264 | * Informa se está operando offline |
||
265 | * @return mixed offline da Configuracao |
||
266 | */ |
||
267 | 1 | public function getOffline($normalize = false) |
|
274 | |||
275 | /** |
||
276 | * Informa se está operando offline |
||
277 | */ |
||
278 | 20 | public function isOffline() |
|
282 | |||
283 | /** |
||
284 | * Entra no modo offline e sai automaticamente após 3 minutos |
||
285 | */ |
||
286 | 2 | public function setOffline($offline) |
|
287 | { |
||
288 | 2 | if (!is_null($offline) && !is_numeric($offline)) { |
|
289 | $offline = strtotime($offline); |
||
290 | } |
||
291 | 2 | $this->offline = $offline; |
|
292 | 2 | return $this; |
|
293 | } |
||
294 | |||
295 | 1 | public function toArray($recursive = false) |
|
312 | |||
313 | 87 | public function fromArray($configuracao = array()) |
|
314 | { |
||
315 | 87 | if ($configuracao instanceof Configuracao) { |
|
316 | $configuracao = $configuracao->toArray(); |
||
317 | 87 | } elseif (!is_array($configuracao)) { |
|
318 | return $this; |
||
319 | } |
||
320 | 87 | if (!isset($configuracao['banco']) || is_null($configuracao['banco'])) { |
|
321 | 87 | $this->setBanco(new Estatico()); |
|
322 | } else { |
||
323 | 1 | $this->setBanco($configuracao['banco']); |
|
324 | } |
||
325 | 87 | if (!isset($configuracao['emitente']) || is_null($configuracao['emitente'])) { |
|
326 | 87 | $this->setEmitente(new Emitente()); |
|
327 | } else { |
||
328 | 1 | $this->setEmitente($configuracao['emitente']); |
|
329 | } |
||
330 | 87 | if (isset($configuracao['evento'])) { |
|
331 | 1 | $this->setEvento($configuracao['evento']); |
|
332 | } else { |
||
333 | 87 | $this->setEvento(null); |
|
334 | } |
||
335 | 87 | if (isset($configuracao['chave_publica'])) { |
|
336 | $this->setChavePublica($configuracao['chave_publica']); |
||
337 | } else { |
||
338 | 87 | $this->setChavePublica(null); |
|
339 | } |
||
340 | 87 | if (isset($configuracao['chave_privada'])) { |
|
341 | $this->setChavePrivada($configuracao['chave_privada']); |
||
342 | } else { |
||
343 | 87 | $this->setChavePrivada(null); |
|
344 | } |
||
345 | 87 | if (isset($configuracao['arquivo_chave_publica'])) { |
|
346 | 1 | $this->setArquivoChavePublica($configuracao['arquivo_chave_publica']); |
|
347 | } else { |
||
348 | 87 | $this->setArquivoChavePublica(null); |
|
349 | } |
||
350 | 87 | if (isset($configuracao['arquivo_chave_privada'])) { |
|
351 | 1 | $this->setArquivoChavePrivada($configuracao['arquivo_chave_privada']); |
|
352 | } else { |
||
353 | 87 | $this->setArquivoChavePrivada(null); |
|
354 | } |
||
355 | 87 | if (isset($configuracao['token'])) { |
|
356 | $this->setToken($configuracao['token']); |
||
357 | } else { |
||
358 | 87 | $this->setToken(null); |
|
359 | } |
||
360 | 87 | if (isset($configuracao['csc'])) { |
|
361 | $this->setCSC($configuracao['csc']); |
||
362 | } else { |
||
363 | 87 | $this->setCSC(null); |
|
364 | } |
||
365 | 87 | if (isset($configuracao['token_ibpt'])) { |
|
366 | $this->setTokenIBPT($configuracao['token_ibpt']); |
||
367 | } else { |
||
368 | 87 | $this->setTokenIBPT(null); |
|
369 | } |
||
370 | 87 | if (!isset($configuracao['tempo_limite']) || is_null($configuracao['tempo_limite'])) { |
|
371 | 87 | $this->setTempoLimite(4); |
|
372 | } else { |
||
373 | 1 | $this->setTempoLimite($configuracao['tempo_limite']); |
|
374 | } |
||
375 | 87 | if (!isset($configuracao['sincrono']) || is_null($configuracao['sincrono'])) { |
|
376 | 87 | $this->setSincrono('Y'); |
|
377 | } else { |
||
378 | 1 | $this->setSincrono($configuracao['sincrono']); |
|
379 | } |
||
380 | 87 | return $this; |
|
381 | } |
||
382 | |||
383 | 87 | private function carregaChavePublica() |
|
384 | { |
||
385 | 87 | if (is_null($this->getChavePublica())) { |
|
386 | 87 | $this->setExpiracao(null); |
|
387 | } else { |
||
388 | 42 | $cert = openssl_x509_read($this->getChavePublica()); |
|
389 | 42 | $cert_data = openssl_x509_parse($cert); |
|
390 | 42 | $this->setExpiracao($cert_data['validTo_time_t']); |
|
391 | } |
||
392 | 87 | } |
|
393 | |||
394 | 29 | public function verificaValidadeCertificado() |
|
405 | } |
||
406 |
The class complexity is the sum of the complexity of all methods. A very high value is usually an indication that your class does not follow the single reponsibility principle and does more than one job.
Some resources for further reading:
You can also find more detailed suggestions for refactoring in the “Code” section of your repository.