Complex classes like Destinatario 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 Destinatario, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class Destinatario extends Pessoa |
||
37 | { |
||
38 | |||
39 | /** |
||
40 | * Indicador da IE do destinatário: |
||
41 | * 1 – Contribuinte ICMSpagamento à |
||
42 | * vista; |
||
43 | * 2 – Contribuinte isento de inscrição; |
||
44 | * 9 – Não Contribuinte |
||
45 | */ |
||
46 | const INDICADOR_PAGAMENTO = 'pagamento'; |
||
47 | const INDICADOR_ISENTO = 'isento'; |
||
48 | const INDICADOR_NENHUM = 'nenhum'; |
||
49 | |||
50 | private $cpf; |
||
51 | private $email; |
||
52 | private $indicador; |
||
53 | |||
54 | 29 | public function __construct($destinatario = array()) |
|
58 | |||
59 | /** |
||
60 | * Número identificador do destinatario |
||
61 | */ |
||
62 | 3 | public function getID($normalize = false) |
|
69 | |||
70 | /** |
||
71 | * Nome do destinatário |
||
72 | */ |
||
73 | 12 | public function getNome($normalize = false) |
|
77 | |||
78 | 29 | public function setNome($nome) |
|
82 | |||
83 | /** |
||
84 | * CPF do cliente |
||
85 | */ |
||
86 | 11 | public function getCPF($normalize = false) |
|
93 | |||
94 | 29 | public function setCPF($cpf) |
|
99 | |||
100 | /** |
||
101 | * Informar o e-mail do destinatário. O campo pode ser utilizado para |
||
102 | * informar o e-mail de recepção da NF-e indicada pelo destinatário |
||
103 | */ |
||
104 | 12 | public function getEmail($normalize = false) |
|
111 | |||
112 | 29 | public function setEmail($email) |
|
117 | |||
118 | /** |
||
119 | * Indicador da IE do destinatário: |
||
120 | * 1 – Contribuinte ICMSpagamento à |
||
121 | * vista; |
||
122 | * 2 – Contribuinte isento de inscrição; |
||
123 | * 9 – Não Contribuinte |
||
124 | */ |
||
125 | 12 | public function getIndicador($normalize = false) |
|
140 | |||
141 | 29 | public function setIndicador($indicador) |
|
146 | |||
147 | 6 | public function toArray() |
|
156 | |||
157 | 29 | public function fromArray($destinatario = array()) |
|
187 | |||
188 | 11 | public function getNode($name = null) |
|
|
|||
189 | { |
||
190 | 11 | $dom = new \DOMDocument('1.0', 'UTF-8'); |
|
191 | 11 | $element = $dom->createElement(is_null($name)?'dest':$name); |
|
192 | 11 | if (!is_null($this->getCNPJ())) { |
|
193 | 2 | Util::appendNode($element, 'CNPJ', $this->getCNPJ(true)); |
|
194 | 2 | } else { |
|
195 | 9 | Util::appendNode($element, 'CPF', $this->getCPF(true)); |
|
196 | } |
||
197 | 11 | if (!is_null($this->getNome())) { |
|
198 | 9 | Util::appendNode($element, 'xNome', $this->getNome(true)); |
|
199 | 9 | } |
|
200 | 11 | if (!is_null($this->getEndereco())) { |
|
201 | 9 | $endereco = $this->getEndereco()->getNode('enderDest'); |
|
202 | 9 | $endereco = $dom->importNode($endereco, true); |
|
203 | 9 | if (!is_null($this->getTelefone())) { |
|
204 | 7 | Util::appendNode($endereco, 'fone', $this->getTelefone(true)); |
|
205 | 7 | } |
|
206 | 9 | $element->appendChild($endereco); |
|
207 | 9 | } |
|
208 | 11 | Util::appendNode($element, 'indIEDest', $this->getIndicador(true)); |
|
209 | 11 | if (!is_null($this->getCNPJ()) && !is_null($this->getIE())) { |
|
210 | 2 | Util::appendNode($element, 'IE', $this->getIE(true)); |
|
211 | 2 | } |
|
212 | 11 | if (!is_null($this->getEmail())) { |
|
213 | 9 | Util::appendNode($element, 'email', $this->getEmail(true)); |
|
214 | 9 | } |
|
215 | 11 | return $element; |
|
216 | } |
||
217 | |||
218 | 6 | public function loadNode($element, $name = null) |
|
245 | } |
||
246 |
A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.
You can also find more information in the “Code” section of your repository.