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 $token; |
||
| 47 | private $csc; |
||
| 48 | private $token_ibpt; |
||
| 49 | private $tempo_limite; |
||
| 50 | private $sincrono; |
||
| 51 | private $offline; |
||
| 52 | |||
| 53 | 7 | public function __construct($configuracao = array()) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Banco que fornece informações sobre items da nota como: Códigos e Taxas |
||
| 60 | */ |
||
| 61 | 32 | public function getBanco() |
|
| 65 | |||
| 66 | 7 | public function setBanco($banco) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Emitente da nota fiscal |
||
| 74 | */ |
||
| 75 | 8 | public function getEmitente() |
|
| 79 | |||
| 80 | 9 | public function setEmitente($emitente) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Informa a instancia que receberá os eventos do processamento das notas |
||
| 88 | */ |
||
| 89 | 4 | public function getEvento() |
|
| 93 | |||
| 94 | 7 | public function setEvento($evento) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Conteúdo da chave pública ou certificado no formato PEM |
||
| 102 | */ |
||
| 103 | 3 | public function getChavePublica() |
|
| 107 | |||
| 108 | 12 | public function setChavePublica($chave_publica) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Conteúdo da chave privada do certificado no formato PEM |
||
| 116 | */ |
||
| 117 | 3 | public function getChavePrivada() |
|
| 121 | |||
| 122 | 12 | public function setChavePrivada($chave_privada) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Informa o caminho do arquivo da chave pública ou certificado no formato |
||
| 130 | * PEM |
||
| 131 | */ |
||
| 132 | 1 | public function getArquivoChavePublica() |
|
| 136 | |||
| 137 | 12 | public function setArquivoChavePublica($arquivo_chave_publica) |
|
| 138 | { |
||
| 139 | 12 | $this->arquivo_chave_publica = $arquivo_chave_publica; |
|
| 140 | 12 | if (file_exists($arquivo_chave_publica)) { |
|
| 141 | 5 | $this->setChavePublica(file_get_contents($arquivo_chave_publica)); |
|
| 142 | 5 | } |
|
| 143 | 12 | return $this; |
|
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Caminho do arquivo da chave privada do certificado no formato PEM |
||
| 148 | */ |
||
| 149 | 1 | public function getArquivoChavePrivada() |
|
| 153 | |||
| 154 | 12 | public function setArquivoChavePrivada($arquivo_chave_privada) |
|
| 155 | { |
||
| 156 | 12 | $this->arquivo_chave_privada = $arquivo_chave_privada; |
|
| 157 | 12 | if (file_exists($arquivo_chave_privada)) { |
|
| 158 | 5 | $this->setChavePrivada(file_get_contents($arquivo_chave_privada)); |
|
| 159 | 5 | } |
|
| 160 | 12 | return $this; |
|
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Token do CSC |
||
| 165 | */ |
||
| 166 | 4 | public function getToken() |
|
| 170 | |||
| 171 | 7 | public function setToken($token) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Código do contribuinte para emissão de nota fiscal |
||
| 179 | */ |
||
| 180 | 4 | public function getCSC() |
|
| 184 | |||
| 185 | 7 | public function setCSC($csc) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Token IBPT para consulta de impostos online |
||
| 193 | */ |
||
| 194 | 8 | public function getTokenIBPT() |
|
| 198 | |||
| 199 | 7 | public function setTokenIBPT($token_ibpt) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Tempo limite em segundos nas conexões com os Web services, 0 para sem tempo limite |
||
| 207 | */ |
||
| 208 | 1 | public function getTempoLimite() |
|
| 212 | |||
| 213 | 7 | public function setTempoLimite($tempo_limite) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Informa se o processo de autorização da nota é síncrono ou assíncrono |
||
| 221 | */ |
||
| 222 | 1 | public function getSincrono($normalize = false) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Informa se o processo de autorização da nota é síncrono ou assíncrono |
||
| 232 | */ |
||
| 233 | public function isSincrono() |
||
| 237 | |||
| 238 | 7 | public function setSincrono($sincrono) |
|
| 239 | { |
||
| 240 | 7 | if (!in_array($sincrono, array('N', 'Y'))) { |
|
| 241 | $sincrono = $sincrono?'Y':'N'; |
||
| 242 | } |
||
| 243 | 7 | $this->sincrono = $sincrono; |
|
| 244 | 7 | return $this; |
|
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Informa se está operando offline |
||
| 249 | * @return mixed offline da Configuracao |
||
| 250 | */ |
||
| 251 | public function getOffline() |
||
| 252 | { |
||
| 253 | return $this->offline; |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Informa se está operando offline |
||
| 258 | */ |
||
| 259 | public function isOffline() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Entra no modo offline e sai automaticamente após 3 minutos |
||
| 266 | */ |
||
| 267 | public function setOffline($offline) |
||
| 272 | |||
| 273 | 1 | public function toArray() |
|
| 288 | |||
| 289 | 7 | public function fromArray($configuracao = array()) |
|
| 358 | } |
||
| 359 |
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.