Complex classes like Endereco 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 Endereco, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class Endereco implements Node |
||
37 | { |
||
38 | |||
39 | private $pais; |
||
40 | private $cep; |
||
41 | private $municipio; |
||
42 | private $bairro; |
||
43 | private $logradouro; |
||
44 | private $numero; |
||
45 | private $complemento; |
||
46 | |||
47 | 89 | public function __construct($endereco = array()) |
|
51 | |||
52 | 34 | public function getPais() |
|
56 | |||
57 | 89 | public function setPais($pais) |
|
62 | |||
63 | 34 | public function getCEP($normalize = false) |
|
70 | |||
71 | 89 | public function setCEP($cep) |
|
76 | |||
77 | 57 | public function getMunicipio() |
|
81 | |||
82 | 89 | public function setMunicipio($municipio) |
|
87 | |||
88 | 41 | public function getBairro($normalize = false) |
|
95 | |||
96 | 89 | public function setBairro($bairro) |
|
101 | |||
102 | 41 | public function getLogradouro($normalize = false) |
|
109 | |||
110 | 89 | public function setLogradouro($logradouro) |
|
115 | |||
116 | 41 | public function getNumero($normalize = false) |
|
123 | |||
124 | 89 | public function setNumero($numero) |
|
129 | |||
130 | 34 | public function getComplemento($normalize = false) |
|
137 | |||
138 | 89 | public function setComplemento($complemento) |
|
143 | |||
144 | /** |
||
145 | * Obtém as informações básicas do endereço em uma linha de texto |
||
146 | * @param boolean $normalize informa se o valor deve ser normalizado para um XML |
||
147 | * @return string endereço com logradouro, número e bairro |
||
148 | */ |
||
149 | 8 | public function getDescricao($normalize = false) |
|
153 | |||
154 | /** |
||
155 | * Desmembra a descrição e salva as informações do endereço em seu respectivo campo |
||
156 | * @param string $descricao linha de endereço com diversas informações |
||
157 | * @return Endereco retorna a própria instância |
||
158 | */ |
||
159 | 4 | public function parseDescricao($descricao) |
|
170 | |||
171 | 8 | public function toArray($recursive = false) |
|
172 | { |
||
173 | 8 | $endereco = array(); |
|
174 | 8 | if (!is_null($this->getPais()) && $recursive) { |
|
175 | 1 | $endereco['pais'] = $this->getPais()->toArray($recursive); |
|
176 | 1 | } else { |
|
177 | 7 | $endereco['pais'] = $this->getPais(); |
|
178 | } |
||
179 | 8 | $endereco['cep'] = $this->getCEP(); |
|
180 | 8 | if (!is_null($this->getMunicipio()) && $recursive) { |
|
181 | 1 | $endereco['municipio'] = $this->getMunicipio()->toArray($recursive); |
|
182 | 1 | } else { |
|
183 | 7 | $endereco['municipio'] = $this->getMunicipio(); |
|
184 | } |
||
185 | 8 | $endereco['bairro'] = $this->getBairro(); |
|
186 | 8 | $endereco['logradouro'] = $this->getLogradouro(); |
|
187 | 8 | $endereco['numero'] = $this->getNumero(); |
|
188 | 8 | $endereco['complemento'] = $this->getComplemento(); |
|
189 | 8 | return $endereco; |
|
190 | } |
||
191 | |||
192 | 89 | public function fromArray($endereco = array()) |
|
193 | { |
||
194 | 89 | if ($endereco instanceof Endereco) { |
|
195 | 7 | $endereco = $endereco->toArray(); |
|
196 | 89 | } elseif (!is_array($endereco)) { |
|
197 | 7 | return $this; |
|
198 | } |
||
199 | 89 | if (!isset($endereco['pais']) || is_null($endereco['pais'])) { |
|
200 | 89 | $this->setPais(new Pais(array('codigo' => 1058, 'nome' => 'Brasil'))); |
|
201 | 89 | } else { |
|
202 | 7 | $this->setPais($endereco['pais']); |
|
203 | } |
||
204 | 89 | if (isset($endereco['cep'])) { |
|
205 | 7 | $this->setCEP($endereco['cep']); |
|
206 | 7 | } else { |
|
207 | 89 | $this->setCEP(null); |
|
208 | } |
||
209 | 89 | if (!isset($endereco['municipio']) || is_null($endereco['municipio'])) { |
|
210 | 89 | $this->setMunicipio(new Municipio()); |
|
211 | 89 | } else { |
|
212 | 7 | $this->setMunicipio($endereco['municipio']); |
|
213 | } |
||
214 | 89 | if (isset($endereco['bairro'])) { |
|
215 | 7 | $this->setBairro($endereco['bairro']); |
|
216 | 7 | } else { |
|
217 | 89 | $this->setBairro(null); |
|
218 | } |
||
219 | 89 | if (isset($endereco['logradouro'])) { |
|
220 | 7 | $this->setLogradouro($endereco['logradouro']); |
|
221 | 7 | } else { |
|
222 | 89 | $this->setLogradouro(null); |
|
223 | } |
||
224 | 89 | if (isset($endereco['numero'])) { |
|
225 | 7 | $this->setNumero($endereco['numero']); |
|
226 | 7 | } else { |
|
227 | 89 | $this->setNumero(null); |
|
228 | } |
||
229 | 89 | if (isset($endereco['complemento'])) { |
|
230 | 1 | $this->setComplemento($endereco['complemento']); |
|
231 | 1 | } else { |
|
232 | 89 | $this->setComplemento(null); |
|
233 | } |
||
234 | 89 | return $this; |
|
235 | } |
||
236 | |||
237 | 33 | public function checkCodigos() |
|
242 | |||
243 | 33 | public function getNode($name = null) |
|
244 | { |
||
263 | |||
264 | 27 | public function loadNode($element, $name = null) |
|
340 | } |
||
341 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.