Complex classes like Evento 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 Evento, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class Evento extends Retorno |
||
|
|
|||
| 38 | { |
||
| 39 | |||
| 40 | const VERSAO = '1.00'; |
||
| 41 | |||
| 42 | const TIPO_CANCELAMENTO = '110111'; |
||
| 43 | const TAG_RETORNO = 'retEvento'; |
||
| 44 | const TAG_RETORNO_ENVIO = 'retEnvEvento'; |
||
| 45 | |||
| 46 | private $id; |
||
| 47 | private $orgao; |
||
| 48 | private $identificador; |
||
| 49 | private $chave; |
||
| 50 | private $data; |
||
| 51 | private $tipo; |
||
| 52 | private $sequencia; |
||
| 53 | private $descricao; |
||
| 54 | private $numero; |
||
| 55 | private $justificativa; |
||
| 56 | private $email; |
||
| 57 | private $modelo; |
||
| 58 | private $informacao; |
||
| 59 | |||
| 60 | 2 | public function __construct($evento = []) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Identificador da TAG a ser assinada, a regra de formação do Id é: "ID" + |
||
| 67 | * tpEvento + chave da NF-e + nSeqEvento |
||
| 68 | */ |
||
| 69 | 1 | public function getID($normalize = false) |
|
| 70 | { |
||
| 71 | 1 | if (!$normalize) { |
|
| 72 | 1 | return $this->id; |
|
| 73 | } |
||
| 74 | return 'ID'.$this->id; |
||
| 75 | } |
||
| 76 | |||
| 77 | 2 | public function setID($id) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Código do órgão de recepção do Evento. Utilizar a Tabela do IBGE |
||
| 85 | * extendida, utilizar 91 para identificar o Ambiente Nacional |
||
| 86 | */ |
||
| 87 | 1 | public function getOrgao($normalize = false) |
|
| 88 | { |
||
| 89 | 1 | if (!$normalize || is_numeric($this->orgao)) { |
|
| 90 | 1 | return $this->orgao; |
|
| 91 | } |
||
| 92 | |||
| 93 | $db = SEFAZ::getInstance()->getConfiguracao()->getBanco(); |
||
| 94 | return $db->getCodigoOrgao($this->orgao); |
||
| 95 | } |
||
| 96 | |||
| 97 | 2 | public function setOrgao($orgao) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Identificação do autor do evento |
||
| 105 | */ |
||
| 106 | 1 | public function getIdentificador($normalize = false) |
|
| 107 | { |
||
| 108 | 1 | if (!$normalize) { |
|
| 109 | 1 | return $this->identificador; |
|
| 110 | } |
||
| 111 | return $this->identificador; |
||
| 112 | } |
||
| 113 | |||
| 114 | 2 | public function setIdentificador($identificador) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Chave de Acesso da NF-e vinculada ao evento |
||
| 122 | */ |
||
| 123 | 1 | public function getChave($normalize = false) |
|
| 124 | { |
||
| 125 | 1 | if (!$normalize) { |
|
| 126 | 1 | return $this->chave; |
|
| 127 | } |
||
| 128 | return $this->chave; |
||
| 129 | } |
||
| 130 | |||
| 131 | 2 | public function setChave($chave) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Data e Hora do Evento, formato UTC (AAAA-MM-DDThh:mm:ssTZD, onde TZD = |
||
| 139 | * +hh:mm ou -hh:mm) |
||
| 140 | */ |
||
| 141 | 1 | public function getData($normalize = false) |
|
| 142 | { |
||
| 143 | 1 | if (!$normalize) { |
|
| 144 | 1 | return $this->data; |
|
| 145 | } |
||
| 146 | return Util::toDateTime($this->data); |
||
| 147 | } |
||
| 148 | |||
| 149 | 2 | public function setData($data) |
|
| 150 | { |
||
| 151 | 2 | if (!is_numeric($data)) { |
|
| 152 | 2 | $data = strtotime($data); |
|
| 153 | } |
||
| 154 | 2 | $this->data = $data; |
|
| 155 | 2 | return $this; |
|
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Tipo do Evento |
||
| 160 | */ |
||
| 161 | 1 | public function getTipo($normalize = false) |
|
| 162 | { |
||
| 163 | 1 | if (!$normalize) { |
|
| 164 | 1 | return $this->tipo; |
|
| 165 | } |
||
| 166 | return $this->tipo; |
||
| 167 | } |
||
| 168 | |||
| 169 | 2 | public function setTipo($tipo) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Seqüencial do evento para o mesmo tipo de evento. Para maioria dos |
||
| 177 | * eventos será 1, nos casos em que possa existir mais de um evento, como é |
||
| 178 | * o caso da carta de correção, o autor do evento deve numerar de forma |
||
| 179 | * seqüencial. |
||
| 180 | */ |
||
| 181 | 1 | public function getSequencia($normalize = false) |
|
| 182 | { |
||
| 183 | 1 | if (!$normalize) { |
|
| 184 | 1 | return $this->sequencia; |
|
| 185 | } |
||
| 186 | return $this->sequencia; |
||
| 187 | } |
||
| 188 | |||
| 189 | 2 | public function setSequencia($sequencia) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Descrição do Evento |
||
| 197 | */ |
||
| 198 | 1 | public function getDescricao($normalize = false) |
|
| 199 | { |
||
| 200 | 1 | if (!$normalize) { |
|
| 201 | 1 | return $this->descricao; |
|
| 202 | } |
||
| 203 | return $this->descricao; |
||
| 204 | } |
||
| 205 | |||
| 206 | 2 | public function setDescricao($descricao) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Número do Protocolo de Status da NF-e. 1 posição (1 – Secretaria de |
||
| 214 | * Fazenda Estadual 2 – Receita Federal); 2 posições ano; 10 seqüencial no |
||
| 215 | * ano. |
||
| 216 | */ |
||
| 217 | 1 | public function getNumero($normalize = false) |
|
| 218 | { |
||
| 219 | 1 | if (!$normalize) { |
|
| 220 | 1 | return $this->numero; |
|
| 221 | } |
||
| 222 | return $this->numero; |
||
| 223 | } |
||
| 224 | |||
| 225 | 2 | public function setNumero($numero) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Justificativa do cancelamento |
||
| 233 | */ |
||
| 234 | 1 | public function getJustificativa($normalize = false) |
|
| 235 | { |
||
| 236 | 1 | if (!$normalize) { |
|
| 237 | 1 | return $this->justificativa; |
|
| 238 | } |
||
| 239 | return $this->justificativa; |
||
| 240 | } |
||
| 241 | |||
| 242 | 2 | public function setJustificativa($justificativa) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * email do destinatário |
||
| 250 | */ |
||
| 251 | 1 | public function getEmail($normalize = false) |
|
| 252 | { |
||
| 253 | 1 | if (!$normalize) { |
|
| 254 | 1 | return $this->email; |
|
| 255 | } |
||
| 256 | 1 | return $this->email; |
|
| 257 | } |
||
| 258 | |||
| 259 | 2 | public function setEmail($email) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Código do modelo do Documento Fiscal. 55 = NF-e; 65 = NFC-e. |
||
| 267 | * @param boolean $normalize informa se o modelo deve estar no formato do XML |
||
| 268 | * @return mixed modelo do Envio |
||
| 269 | */ |
||
| 270 | 1 | public function getModelo($normalize = false) |
|
| 271 | { |
||
| 272 | 1 | if (!$normalize) { |
|
| 273 | 1 | return $this->modelo; |
|
| 274 | } |
||
| 275 | 1 | switch ($this->modelo) { |
|
| 276 | 1 | case Nota::MODELO_NFE: |
|
| 277 | 1 | return '55'; |
|
| 278 | 1 | case Nota::MODELO_NFCE: |
|
| 279 | 1 | return '65'; |
|
| 280 | } |
||
| 281 | 1 | return $this->modelo; |
|
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Altera o valor do Modelo para o informado no parâmetro |
||
| 286 | * @param mixed $modelo novo valor para Modelo |
||
| 287 | * @return Envio A própria instância da classe |
||
| 288 | */ |
||
| 289 | 2 | public function setModelo($modelo) |
|
| 290 | { |
||
| 291 | switch ($modelo) { |
||
| 292 | 2 | case '55': |
|
| 293 | 1 | $modelo = Nota::MODELO_NFE; |
|
| 294 | 1 | break; |
|
| 295 | 2 | case '65': |
|
| 296 | 1 | $modelo = Nota::MODELO_NFCE; |
|
| 297 | 1 | break; |
|
| 298 | } |
||
| 299 | 2 | $this->modelo = $modelo; |
|
| 300 | 2 | return $this; |
|
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Resposta de informação do evento |
||
| 305 | */ |
||
| 306 | 1 | public function getInformacao() |
|
| 310 | |||
| 311 | 2 | public function setInformacao($informacao) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Informa se a identificação é um CNPJ |
||
| 319 | */ |
||
| 320 | public function isCNPJ() |
||
| 321 | { |
||
| 322 | return strlen($this->getIdentificador()) == 14; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Informa se o lote já foi processado e já tem um protocolo |
||
| 327 | */ |
||
| 328 | public function isProcessado() |
||
| 329 | { |
||
| 330 | return $this->getStatus() == '128'; |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Informa se a nota foi cancelada com sucesso |
||
| 335 | */ |
||
| 336 | public function isCancelado() |
||
| 337 | { |
||
| 338 | return in_array($this->getStatus(), ['135', '155']); |
||
| 339 | } |
||
| 340 | |||
| 341 | 1 | public function toArray($recursive = false) |
|
| 359 | |||
| 360 | 2 | public function fromArray($evento = []) |
|
| 361 | { |
||
| 362 | 2 | if ($evento instanceof Evento) { |
|
| 363 | 1 | $evento = $evento->toArray(); |
|
| 364 | 2 | } elseif (!is_array($evento)) { |
|
| 365 | return $this; |
||
| 366 | } |
||
| 367 | 2 | parent::fromArray($evento); |
|
| 368 | 2 | if (isset($evento['id'])) { |
|
| 369 | $this->setID($evento['id']); |
||
| 370 | } else { |
||
| 371 | 2 | $this->setID(null); |
|
| 372 | } |
||
| 373 | 2 | if (isset($evento['orgao'])) { |
|
| 374 | $this->setOrgao($evento['orgao']); |
||
| 375 | } else { |
||
| 376 | 2 | $this->setOrgao(null); |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Gera o ID, a regra de formação do Id é: "ID" + |
||
| 438 | * tpEvento + chave da NF-e + nSeqEvento |
||
| 439 | */ |
||
| 440 | public function gerarID() |
||
| 450 | |||
| 451 | public function getNode($name = null) |
||
| 495 | |||
| 496 | public function loadNode($element, $name = null) |
||
| 591 | |||
| 592 | public function loadResponse($resp) |
||
| 599 | |||
| 600 | public function loadStatusNode($element, $name = null) |
||
| 613 | |||
| 614 | public function getReturnNode() |
||
| 658 | |||
| 659 | public function loadReturnNode($element, $name = null) |
||
| 691 | |||
| 692 | private function getConteudo($dom) |
||
| 712 | |||
| 713 | public function envia($dom) |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Adiciona a informação no XML do evento |
||
| 733 | */ |
||
| 734 | public function addInformacao($dom) |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Assina o XML com a assinatura eletrônica do tipo A1 |
||
| 767 | */ |
||
| 768 | public function assinar($dom = null) |
||
| 785 | |||
| 786 | /** |
||
| 787 | * Valida o documento após assinar |
||
| 788 | */ |
||
| 789 | public function validar($dom) |
||
| 812 | } |
||
| 813 |
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.