Complex classes like Inutilizacao 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 Inutilizacao, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class Inutilizacao extends Retorno |
||
|
|||
38 | { |
||
39 | |||
40 | private $id; |
||
41 | private $ano; |
||
42 | private $cnpj; |
||
43 | private $modelo; |
||
44 | private $serie; |
||
45 | private $inicio; |
||
46 | private $final; |
||
47 | private $justificativa; |
||
48 | private $numero; |
||
49 | |||
50 | 6 | public function __construct($inutilizacao = array()) |
|
54 | |||
55 | /** |
||
56 | * Formado por: |
||
57 | * ID = Literal |
||
58 | * 43 = Código Estado |
||
59 | * 15 = Ano |
||
60 | * |
||
61 | * 00000000000000 = CNPJ |
||
62 | * 55 = Modelo |
||
63 | * 001 = Série |
||
64 | * 000000411 = |
||
65 | * Número Inicial |
||
66 | * 000000411 = Número Final |
||
67 | */ |
||
68 | 4 | public function getID($normalize = false) |
|
69 | { |
||
70 | 4 | if (!$normalize) { |
|
71 | 1 | return $this->id; |
|
72 | } |
||
73 | 4 | return 'ID'.$this->id; |
|
74 | } |
||
75 | |||
76 | 6 | public function setID($id) |
|
81 | |||
82 | 4 | public function getAno($normalize = false) |
|
83 | { |
||
84 | 4 | if (!$normalize) { |
|
85 | 1 | return $this->ano; |
|
86 | } |
||
87 | 4 | return $this->ano % 100; |
|
88 | } |
||
89 | |||
90 | 6 | public function setAno($ano) |
|
95 | |||
96 | 4 | public function getCNPJ($normalize = false) |
|
97 | { |
||
98 | 4 | if (!$normalize) { |
|
99 | 1 | return $this->cnpj; |
|
100 | } |
||
101 | 4 | return $this->cnpj; |
|
102 | } |
||
103 | |||
104 | 6 | public function setCNPJ($cnpj) |
|
109 | |||
110 | /** |
||
111 | * Código do modelo do Documento Fiscal. 55 = NF-e; 65 = NFC-e. |
||
112 | * @param boolean $normalize informa se o modelo deve estar no formato do XML |
||
113 | * @return mixed modelo do Envio |
||
114 | */ |
||
115 | 4 | public function getModelo($normalize = false) |
|
116 | { |
||
117 | 4 | if (!$normalize) { |
|
118 | 3 | return $this->modelo; |
|
119 | } |
||
120 | 4 | switch ($this->modelo) { |
|
121 | 4 | case Nota::MODELO_NFE: |
|
122 | return '55'; |
||
123 | 4 | case Nota::MODELO_NFCE: |
|
124 | 3 | return '65'; |
|
125 | } |
||
126 | 1 | return $this->modelo; |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * Altera o valor do Modelo para o informado no parâmetro |
||
131 | * @param mixed $modelo novo valor para Modelo |
||
132 | * @return Envio A própria instância da classe |
||
133 | */ |
||
134 | 6 | public function setModelo($modelo) |
|
135 | { |
||
136 | switch ($modelo) { |
||
137 | 6 | case '55': |
|
138 | $modelo = Nota::MODELO_NFE; |
||
139 | break; |
||
140 | 6 | case '65': |
|
141 | 4 | $modelo = Nota::MODELO_NFCE; |
|
142 | 4 | break; |
|
143 | } |
||
144 | 6 | $this->modelo = $modelo; |
|
145 | 6 | return $this; |
|
146 | } |
||
147 | |||
148 | 4 | public function getSerie($normalize = false) |
|
149 | { |
||
150 | 4 | if (!$normalize) { |
|
151 | 1 | return $this->serie; |
|
152 | } |
||
153 | 4 | return $this->serie; |
|
154 | } |
||
155 | |||
156 | 6 | public function setSerie($serie) |
|
161 | |||
162 | 4 | public function getInicio($normalize = false) |
|
163 | { |
||
164 | 4 | if (!$normalize) { |
|
165 | 3 | return $this->inicio; |
|
166 | } |
||
167 | 4 | return $this->inicio; |
|
168 | } |
||
169 | |||
170 | 6 | public function setInicio($inicio) |
|
175 | |||
176 | 4 | public function getFinal($normalize = false) |
|
177 | { |
||
178 | 4 | if (!$normalize) { |
|
179 | 1 | return $this->final; |
|
180 | } |
||
181 | 4 | return $this->final; |
|
182 | } |
||
183 | |||
184 | 6 | public function setFinal($final) |
|
189 | |||
190 | 4 | public function getJustificativa($normalize = false) |
|
191 | { |
||
192 | 4 | if (!$normalize) { |
|
193 | 1 | return $this->justificativa; |
|
194 | } |
||
195 | 4 | return $this->justificativa; |
|
196 | } |
||
197 | |||
198 | 6 | public function setJustificativa($justificativa) |
|
203 | |||
204 | 2 | public function getNumero($normalize = false) |
|
205 | { |
||
206 | 2 | if (!$normalize) { |
|
207 | 2 | return $this->numero; |
|
208 | } |
||
209 | 2 | return $this->numero; |
|
210 | } |
||
211 | |||
212 | 6 | public function setNumero($numero) |
|
217 | |||
218 | /** |
||
219 | * Informa se os números foram inutilizados |
||
220 | */ |
||
221 | 3 | public function isInutilizado() |
|
222 | { |
||
223 | 3 | return $this->getStatus() == '102'; |
|
224 | } |
||
225 | |||
226 | 1 | public function toArray($recursive = false) |
|
227 | { |
||
240 | |||
241 | 6 | public function fromArray($inutilizacao = array()) |
|
242 | { |
||
243 | 6 | if ($inutilizacao instanceof Inutilizacao) { |
|
244 | 1 | $inutilizacao = $inutilizacao->toArray(); |
|
245 | 6 | } elseif (!is_array($inutilizacao)) { |
|
246 | 1 | return $this; |
|
247 | } |
||
248 | 6 | parent::fromArray($inutilizacao); |
|
249 | 6 | if (isset($inutilizacao['id'])) { |
|
250 | 1 | $this->setID($inutilizacao['id']); |
|
251 | } else { |
||
252 | 6 | $this->setID(null); |
|
253 | } |
||
254 | 6 | if (isset($inutilizacao['ano'])) { |
|
255 | 1 | $this->setAno($inutilizacao['ano']); |
|
256 | } else { |
||
257 | 6 | $this->setAno(null); |
|
258 | } |
||
259 | 6 | if (isset($inutilizacao['cnpj'])) { |
|
260 | 1 | $this->setCNPJ($inutilizacao['cnpj']); |
|
261 | } else { |
||
262 | 6 | $this->setCNPJ(null); |
|
263 | } |
||
264 | 6 | if (isset($inutilizacao['modelo'])) { |
|
265 | 1 | $this->setModelo($inutilizacao['modelo']); |
|
266 | } else { |
||
267 | 6 | $this->setModelo(null); |
|
268 | } |
||
269 | 6 | if (isset($inutilizacao['serie'])) { |
|
270 | 1 | $this->setSerie($inutilizacao['serie']); |
|
271 | } else { |
||
272 | 6 | $this->setSerie(null); |
|
273 | } |
||
274 | 6 | if (isset($inutilizacao['inicio'])) { |
|
275 | 1 | $this->setInicio($inutilizacao['inicio']); |
|
276 | } else { |
||
277 | 6 | $this->setInicio(null); |
|
278 | } |
||
279 | 6 | if (isset($inutilizacao['final'])) { |
|
280 | 1 | $this->setFinal($inutilizacao['final']); |
|
281 | } else { |
||
282 | 6 | $this->setFinal(null); |
|
283 | } |
||
284 | 6 | if (isset($inutilizacao['justificativa'])) { |
|
285 | 1 | $this->setJustificativa($inutilizacao['justificativa']); |
|
286 | } else { |
||
287 | 6 | $this->setJustificativa(null); |
|
288 | } |
||
289 | 6 | if (isset($inutilizacao['numero'])) { |
|
290 | 1 | $this->setNumero($inutilizacao['numero']); |
|
291 | } else { |
||
292 | 6 | $this->setNumero(null); |
|
293 | } |
||
294 | 6 | return $this; |
|
295 | } |
||
296 | |||
297 | 4 | public function gerarID() |
|
298 | { |
||
299 | 4 | $id = sprintf( |
|
300 | 4 | '%02d%02d%s%02d%03d%09d%09d', |
|
301 | 4 | $this->getUF(true), |
|
302 | 4 | $this->getAno(true), // 2 dígitos |
|
303 | 4 | $this->getCNPJ(true), |
|
304 | 4 | $this->getModelo(true), |
|
305 | 4 | $this->getSerie(true), |
|
306 | 4 | $this->getInicio(true), |
|
307 | 4 | $this->getFinal(true) |
|
308 | ); |
||
309 | 4 | return $id; |
|
310 | } |
||
311 | |||
312 | 4 | public function getNode($name = null) |
|
342 | |||
343 | 2 | public function getReturnNode() |
|
344 | { |
||
345 | 2 | $outros = parent::getNode('infInut'); |
|
346 | 2 | $element = $this->getNode('retInutNFe'); |
|
347 | 2 | $dom = $element->ownerDocument; |
|
348 | 2 | $info = $dom->getElementsByTagName('infInut')->item(0); |
|
349 | 2 | $info->removeAttribute('Id'); |
|
350 | 2 | $removeTags = array('tpAmb', 'xServ', 'xJust'); |
|
351 | 2 | foreach ($removeTags as $key) { |
|
352 | 2 | $node = $info->getElementsByTagName($key)->item(0); |
|
353 | 2 | $info->removeChild($node); |
|
354 | } |
||
355 | 2 | $uf = $info->getElementsByTagName('cUF')->item(0); |
|
356 | 2 | foreach ($outros->childNodes as $node) { |
|
357 | 2 | $node = $dom->importNode($node, true); |
|
358 | 2 | $list = $info->getElementsByTagName($node->nodeName); |
|
359 | 2 | if ($list->length == 1) { |
|
360 | 2 | continue; |
|
361 | } |
||
362 | 2 | switch ($node->nodeName) { |
|
363 | 2 | case 'dhRecbto': |
|
364 | 2 | $info->appendChild($node); |
|
365 | 2 | break; |
|
366 | default: |
||
367 | 2 | $info->insertBefore($node, $uf); |
|
368 | } |
||
369 | } |
||
370 | 2 | Util::appendNode($info, 'nProt', $this->getNumero(true)); |
|
371 | 2 | return $element; |
|
372 | } |
||
373 | |||
374 | 3 | public function loadNode($element, $name = null) |
|
390 | |||
391 | 3 | public function envia($dom) |
|
406 | |||
407 | /** |
||
408 | * Assina o XML com a assinatura eletrônica do tipo A1 |
||
409 | */ |
||
410 | 4 | public function assinar($dom = null) |
|
427 | |||
428 | /** |
||
429 | * Valida o documento após assinar |
||
430 | */ |
||
431 | 4 | public function validar($dom) |
|
454 | } |
||
455 |
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.