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 | |||
44 | private $id; |
||
45 | private $orgao; |
||
46 | private $identificador; |
||
47 | private $chave; |
||
48 | private $data; |
||
49 | private $tipo; |
||
50 | private $sequencia; |
||
51 | private $descricao; |
||
52 | private $numero; |
||
53 | private $justificativa; |
||
54 | private $email; |
||
55 | private $modelo; |
||
56 | private $informacao; |
||
57 | |||
58 | 6 | public function __construct($evento = array()) |
|
62 | |||
63 | /** |
||
64 | * Identificador da TAG a ser assinada, a regra de formação do Id é: "ID" + |
||
65 | * tpEvento + chave da NF-e + nSeqEvento |
||
66 | */ |
||
67 | 5 | public function getID($normalize = false) |
|
68 | { |
||
69 | 5 | if (!$normalize) { |
|
70 | 2 | return $this->id; |
|
71 | } |
||
72 | 4 | return 'ID'.$this->id; |
|
73 | } |
||
74 | |||
75 | 6 | public function setID($id) |
|
80 | |||
81 | /** |
||
82 | * Código do órgão de recepção do Evento. Utilizar a Tabela do IBGE |
||
83 | * extendida, utilizar 91 para identificar o Ambiente Nacional |
||
84 | */ |
||
85 | 4 | public function getOrgao($normalize = false) |
|
86 | { |
||
87 | 4 | if (!$normalize || is_numeric($this->orgao)) { |
|
88 | 2 | return $this->orgao; |
|
89 | } |
||
90 | |||
91 | 4 | $db = SEFAZ::getInstance()->getConfiguracao()->getBanco(); |
|
92 | 4 | return $db->getCodigoOrgao($this->orgao); |
|
93 | } |
||
94 | |||
95 | 6 | public function setOrgao($orgao) |
|
100 | |||
101 | /** |
||
102 | * Identificação do autor do evento |
||
103 | */ |
||
104 | 4 | public function getIdentificador($normalize = false) |
|
105 | { |
||
106 | 4 | if (!$normalize) { |
|
107 | 4 | return $this->identificador; |
|
108 | } |
||
109 | 4 | return $this->identificador; |
|
110 | } |
||
111 | |||
112 | 6 | public function setIdentificador($identificador) |
|
117 | |||
118 | /** |
||
119 | * Chave de Acesso da NF-e vinculada ao evento |
||
120 | */ |
||
121 | 4 | public function getChave($normalize = false) |
|
122 | { |
||
123 | 4 | if (!$normalize) { |
|
124 | 2 | return $this->chave; |
|
125 | } |
||
126 | 4 | return $this->chave; |
|
127 | } |
||
128 | |||
129 | 6 | public function setChave($chave) |
|
134 | |||
135 | /** |
||
136 | * Data e Hora do Evento, formato UTC (AAAA-MM-DDThh:mm:ssTZD, onde TZD = |
||
137 | * +hh:mm ou -hh:mm) |
||
138 | */ |
||
139 | 4 | public function getData($normalize = false) |
|
140 | { |
||
141 | 4 | if (!$normalize) { |
|
142 | 1 | return $this->data; |
|
143 | } |
||
144 | 4 | return Util::toDateTime($this->data); |
|
145 | } |
||
146 | |||
147 | 6 | public function setData($data) |
|
148 | { |
||
149 | 6 | if (!is_numeric($data)) { |
|
150 | 6 | $data = strtotime($data); |
|
151 | 6 | } |
|
152 | 6 | $this->data = $data; |
|
153 | 6 | return $this; |
|
154 | } |
||
155 | |||
156 | /** |
||
157 | * Tipo do Evento |
||
158 | */ |
||
159 | 4 | public function getTipo($normalize = false) |
|
160 | { |
||
161 | 4 | if (!$normalize) { |
|
162 | 1 | return $this->tipo; |
|
163 | } |
||
164 | 4 | return $this->tipo; |
|
165 | } |
||
166 | |||
167 | 6 | public function setTipo($tipo) |
|
172 | |||
173 | /** |
||
174 | * Seqüencial do evento para o mesmo tipo de evento. Para maioria dos |
||
175 | * eventos será 1, nos casos em que possa existir mais de um evento, como é |
||
176 | * o caso da carta de correção, o autor do evento deve numerar de forma |
||
177 | * seqüencial. |
||
178 | */ |
||
179 | 4 | public function getSequencia($normalize = false) |
|
180 | { |
||
181 | 4 | if (!$normalize) { |
|
182 | 1 | return $this->sequencia; |
|
183 | } |
||
184 | 4 | return $this->sequencia; |
|
185 | } |
||
186 | |||
187 | 6 | public function setSequencia($sequencia) |
|
192 | |||
193 | /** |
||
194 | * Descrição do Evento |
||
195 | */ |
||
196 | 4 | public function getDescricao($normalize = false) |
|
197 | { |
||
198 | 4 | if (!$normalize) { |
|
199 | 1 | return $this->descricao; |
|
200 | } |
||
201 | 4 | return $this->descricao; |
|
202 | } |
||
203 | |||
204 | 6 | public function setDescricao($descricao) |
|
209 | |||
210 | /** |
||
211 | * Número do Protocolo de Status da NF-e. 1 posição (1 – Secretaria de |
||
212 | * Fazenda Estadual 2 – Receita Federal); 2 posições ano; 10 seqüencial no |
||
213 | * ano. |
||
214 | */ |
||
215 | 4 | public function getNumero($normalize = false) |
|
216 | { |
||
217 | 4 | if (!$normalize) { |
|
218 | 1 | return $this->numero; |
|
219 | } |
||
220 | 4 | return $this->numero; |
|
221 | } |
||
222 | |||
223 | 6 | public function setNumero($numero) |
|
228 | |||
229 | /** |
||
230 | * Justificativa do cancelamento |
||
231 | */ |
||
232 | 4 | public function getJustificativa($normalize = false) |
|
233 | { |
||
234 | 4 | if (!$normalize) { |
|
235 | 1 | return $this->justificativa; |
|
236 | } |
||
237 | 4 | return $this->justificativa; |
|
238 | } |
||
239 | |||
240 | 6 | public function setJustificativa($justificativa) |
|
245 | |||
246 | /** |
||
247 | * email do destinatário |
||
248 | */ |
||
249 | 2 | public function getEmail($normalize = false) |
|
250 | { |
||
251 | 2 | if (!$normalize) { |
|
252 | 2 | return $this->email; |
|
253 | } |
||
254 | return $this->email; |
||
255 | } |
||
256 | |||
257 | 6 | public function setEmail($email) |
|
262 | |||
263 | /** |
||
264 | * Código do modelo do Documento Fiscal. 55 = NF-e; 65 = NFC-e. |
||
265 | * @param boolean $normalize informa se o modelo deve estar no formato do XML |
||
266 | * @return mixed modelo do Envio |
||
267 | */ |
||
268 | 3 | public function getModelo($normalize = false) |
|
269 | { |
||
270 | 3 | if (!$normalize) { |
|
271 | 3 | return $this->modelo; |
|
272 | } |
||
273 | switch ($this->modelo) { |
||
274 | case Nota::MODELO_NFE: |
||
275 | return '55'; |
||
276 | case Nota::MODELO_NFCE: |
||
277 | return '65'; |
||
278 | } |
||
279 | return $this->modelo; |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Altera o valor do Modelo para o informado no parâmetro |
||
284 | * @param mixed $modelo novo valor para Modelo |
||
285 | * @return Envio A própria instância da classe |
||
286 | */ |
||
287 | 6 | public function setModelo($modelo) |
|
300 | |||
301 | /** |
||
302 | * Resposta de informação do evento |
||
303 | */ |
||
304 | 3 | public function getInformacao() |
|
305 | { |
||
306 | 3 | return $this->informacao; |
|
307 | } |
||
308 | |||
309 | 6 | public function setInformacao($informacao) |
|
314 | |||
315 | /** |
||
316 | * Informa se a identificação é um CNPJ |
||
317 | */ |
||
318 | 4 | public function isCNPJ() |
|
319 | { |
||
320 | 4 | return strlen($this->getIdentificador()) == 14; |
|
321 | } |
||
322 | |||
323 | /** |
||
324 | * Informa se o lote já foi processado e já tem um protocolo |
||
325 | */ |
||
326 | 3 | public function isProcessado() |
|
327 | { |
||
328 | 3 | return $this->getStatus() == '128'; |
|
330 | |||
331 | /** |
||
332 | * Informa se a nota foi cancelada com sucesso |
||
333 | */ |
||
334 | 1 | public function isCancelado() |
|
338 | |||
339 | 1 | public function toArray($recursive = false) |
|
357 | |||
358 | 6 | public function fromArray($evento = array()) |
|
433 | |||
434 | /** |
||
435 | * Gera o ID, a regra de formação do Id é: "ID" + |
||
436 | * tpEvento + chave da NF-e + nSeqEvento |
||
437 | */ |
||
438 | 4 | public function gerarID() |
|
448 | |||
449 | 4 | public function getNode($name = null) |
|
493 | |||
494 | 2 | public function getReturnNode() |
|
538 | |||
539 | 3 | public function loadNode($element, $name = null) |
|
573 | |||
574 | 3 | private function getConteudo($dom) |
|
594 | |||
595 | 3 | public function envia($dom) |
|
613 | |||
614 | /** |
||
615 | * Adiciona a informação no XML do evento |
||
616 | */ |
||
617 | 3 | public function addInformacao($dom) |
|
647 | |||
648 | /** |
||
649 | * Assina o XML com a assinatura eletrônica do tipo A1 |
||
650 | */ |
||
651 | 4 | public function assinar($dom = null) |
|
667 | |||
668 | /** |
||
669 | * Valida o documento após assinar |
||
670 | */ |
||
671 | 4 | public function validar($dom) |
|
694 | } |
||
695 |
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.