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 | * @var \NFe\Database\Banco |
||
| 40 | */ |
||
| 41 | private $banco; |
||
| 42 | /** |
||
| 43 | * @var \NFe\Entity\Emitente |
||
| 44 | */ |
||
| 45 | private $emitente; |
||
| 46 | /** |
||
| 47 | * @var Evento |
||
| 48 | */ |
||
| 49 | private $evento; |
||
| 50 | /** |
||
| 51 | * @var Certificado |
||
| 52 | */ |
||
| 53 | private $certificado; |
||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | private $token; |
||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | private $csc; |
||
| 62 | /** |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | private $token_ibpt; |
||
| 66 | /** |
||
| 67 | * @var int |
||
| 68 | */ |
||
| 69 | private $tempo_limite; |
||
| 70 | /** |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | private $sincrono; |
||
| 74 | /** |
||
| 75 | * @var int |
||
| 76 | */ |
||
| 77 | private $offline; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param mixed $configuracao array ou instância |
||
| 81 | */ |
||
| 82 | 102 | public function __construct($configuracao = []) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Banco que fornece informações sobre items da nota como: Códigos e Taxas |
||
| 89 | * @return \NFe\Database\Banco |
||
| 90 | */ |
||
| 91 | 72 | public function getBanco() |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Banco que fornece informações sobre items da nota como: Códigos e Taxas |
||
| 98 | * @param \NFe\Database\Banco $banco |
||
| 99 | * @return self |
||
| 100 | */ |
||
| 101 | 102 | public function setBanco($banco) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Emitente da nota fiscal |
||
| 109 | * @return \NFe\Entity\Emitente |
||
| 110 | */ |
||
| 111 | 47 | public function getEmitente() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Emitente da nota fiscal |
||
| 118 | * @param \NFe\Entity\Emitente $emitente |
||
| 119 | * @return self |
||
| 120 | */ |
||
| 121 | 102 | public function setEmitente($emitente) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Informa a instancia que receberá os eventos do processamento das notas |
||
| 129 | * @return Evento |
||
| 130 | */ |
||
| 131 | 9 | public function getEvento() |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Informa a instancia que receberá os eventos do processamento das notas |
||
| 138 | * @param Evento $evento |
||
| 139 | * @return self |
||
| 140 | */ |
||
| 141 | 102 | public function setEvento($evento) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Certificado para assinar os XMLs |
||
| 149 | * @return Certificado |
||
| 150 | */ |
||
| 151 | 105 | public function getCertificado() |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Informa o certificado para assinar os XMLs |
||
| 158 | * @param Certificado $certificado |
||
| 159 | * @return self |
||
| 160 | */ |
||
| 161 | 102 | public function setCertificado($certificado) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Conteúdo da chave pública ou certificado no formato PEM |
||
| 169 | * @return string |
||
| 170 | * @deprecated Use getCertificado()->getChavePublica |
||
| 171 | */ |
||
| 172 | 34 | public function getChavePublica() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Conteúdo da chave pública ou certificado no formato PEM |
||
| 179 | * @param string $chave_publica |
||
| 180 | * @return self |
||
| 181 | * @deprecated Use getCertificado()->setChavePublica |
||
| 182 | */ |
||
| 183 | 1 | public function setChavePublica($chave_publica) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Conteúdo da chave privada do certificado no formato PEM |
||
| 191 | * @return string |
||
| 192 | * @deprecated Use getCertificado()->getChavePrivada |
||
| 193 | */ |
||
| 194 | 34 | public function getChavePrivada() |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Conteúdo da chave privada do certificado no formato PEM |
||
| 201 | * @param string $chave_privada |
||
| 202 | * @return self |
||
| 203 | * @deprecated Use getCertificado()->setChavePrivada |
||
| 204 | */ |
||
| 205 | 1 | public function setChavePrivada($chave_privada) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Informa o caminho do arquivo da chave pública ou certificado no formato |
||
| 213 | * PEM |
||
| 214 | * @return string |
||
| 215 | * @deprecated Use getCertificado()->getArquivoChavePublica |
||
| 216 | */ |
||
| 217 | 22 | public function getArquivoChavePublica() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Informa o caminho do arquivo da chave pública ou certificado no formato |
||
| 224 | * PEM |
||
| 225 | * @param string $arquivo_chave_publica |
||
| 226 | * @return self |
||
| 227 | * @deprecated Use getCertificado()->setArquivoChavePublica |
||
| 228 | */ |
||
| 229 | 102 | public function setArquivoChavePublica($arquivo_chave_publica) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Caminho do arquivo da chave privada do certificado no formato PEM |
||
| 237 | * @return string |
||
| 238 | * @deprecated Use getCertificado()->getArquivoChavePrivada |
||
| 239 | */ |
||
| 240 | 22 | public function getArquivoChavePrivada() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Altera o caminho do arquivo da chave privada do certificado no formato PEM |
||
| 247 | * @param string $arquivo_chave_privada |
||
| 248 | * @return self |
||
| 249 | * @deprecated Use getCertificado()->setArquivoChavePrivada |
||
| 250 | */ |
||
| 251 | 102 | public function setArquivoChavePrivada($arquivo_chave_privada) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Data de expiração do certificado em timestamp |
||
| 259 | * @return int |
||
| 260 | * @deprecated Use getCertificado()->getExpiracao |
||
| 261 | */ |
||
| 262 | 1 | public function getExpiracao() |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Token do CSC |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | 31 | public function getToken() |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Informa o token do CSC, geralmente 000001 |
||
| 278 | * @param string $token |
||
| 279 | * @return self |
||
| 280 | */ |
||
| 281 | 102 | public function setToken($token) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Código do contribuinte para emissão de nota fiscal |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | 31 | public function getCSC() |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Informa o código do contribuinte para emissão de nota fiscal |
||
| 298 | * @param string $csc |
||
| 299 | * @return self |
||
| 300 | */ |
||
| 301 | 102 | public function setCSC($csc) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Token IBPT para consulta de impostos online |
||
| 309 | */ |
||
| 310 | 37 | public function getTokenIBPT() |
|
| 314 | |||
| 315 | 102 | public function setTokenIBPT($token_ibpt) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Tempo limite em segundos nas conexões com os Web services, 0 para sem tempo limite |
||
| 323 | */ |
||
| 324 | 24 | public function getTempoLimite() |
|
| 328 | |||
| 329 | 102 | public function setTempoLimite($tempo_limite) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Informa se o processo de autorização da nota é síncrono ou assíncrono |
||
| 337 | */ |
||
| 338 | 8 | public function getSincrono($normalize = false) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Informa se o processo de autorização da nota é síncrono ou assíncrono |
||
| 348 | */ |
||
| 349 | 6 | public function isSincrono() |
|
| 353 | |||
| 354 | 102 | public function setSincrono($sincrono) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Informa se está operando offline |
||
| 365 | * @return mixed offline da Configuracao |
||
| 366 | */ |
||
| 367 | 3 | public function getOffline($normalize = false) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Informa se está operando offline |
||
| 377 | */ |
||
| 378 | 23 | public function isOffline() |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Entra no modo offline e sai automaticamente após 3 minutos |
||
| 385 | */ |
||
| 386 | 4 | public function setOffline($offline) |
|
| 394 | |||
| 395 | 2 | public function toArray($recursive = false) |
|
| 410 | |||
| 411 | 102 | public function fromArray($configuracao = []) |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Certifica que o certificado está informado e é válido |
||
| 456 | * @throws \Exception quando o certificado estiver expirado ou não informado |
||
| 457 | */ |
||
| 458 | 34 | public function verificaValidadeCertificado() |
|
| 464 | } |
||
| 465 |
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.