Complex classes like Transporte 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 Transporte, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class Transporte implements Node |
||
|
|||
40 | { |
||
41 | |||
42 | /** |
||
43 | * Modalidade do frete |
||
44 | * 0- Contratação do Frete por conta do Remetente |
||
45 | * (CIF); |
||
46 | * 1- Contratação do Frete por conta do destinatário/remetente |
||
47 | * (FOB); |
||
48 | * 2- Contratação do Frete por conta de terceiros; |
||
49 | * 3- Transporte |
||
50 | * próprio por conta do remetente; |
||
51 | * 4- Transporte próprio por conta do |
||
52 | * destinatário; |
||
53 | * 9- Sem Ocorrência de transporte. |
||
54 | */ |
||
55 | const FRETE_REMETENTE = 'remetente'; |
||
56 | const FRETE_DESTINATARIO = 'destinatario'; |
||
57 | const FRETE_TERCEIROS = 'terceiros'; |
||
58 | const FRETE_PROPRIOREMETENTE = 'proprio_remetente'; |
||
59 | const FRETE_PROPRIODESTINATARIO = 'proprio_destinatario'; |
||
60 | const FRETE_NENHUM = 'nenhum'; |
||
61 | |||
62 | private $frete; |
||
63 | private $transportador; |
||
64 | private $retencao; |
||
65 | private $veiculo; |
||
66 | private $reboque; |
||
67 | private $vagao; |
||
68 | private $balsa; |
||
69 | private $volumes; |
||
70 | |||
71 | 46 | public function __construct($transporte = []) |
|
75 | |||
76 | /** |
||
77 | * Modalidade do frete |
||
78 | * 0- Contratação do Frete por conta do Remetente |
||
79 | * (CIF); |
||
80 | * 1- Contratação do Frete por conta do destinatário/remetente |
||
81 | * (FOB); |
||
82 | * 2- Contratação do Frete por conta de terceiros; |
||
83 | * 3- Transporte |
||
84 | * próprio por conta do remetente; |
||
85 | * 4- Transporte próprio por conta do |
||
86 | * destinatário; |
||
87 | * 9- Sem Ocorrência de transporte. |
||
88 | * @param boolean $normalize informa se o frete deve estar no formato do XML |
||
89 | * @return mixed frete da Transporte |
||
90 | */ |
||
91 | 41 | public function getFrete($normalize = false) |
|
112 | |||
113 | /** |
||
114 | * Altera o valor do Frete para o informado no parâmetro |
||
115 | * @param mixed $frete novo valor para Frete |
||
116 | * @return Transporte A própria instância da classe |
||
117 | */ |
||
118 | 46 | public function setFrete($frete) |
|
143 | |||
144 | /** |
||
145 | * Dados da transportadora |
||
146 | */ |
||
147 | 14 | public function getTransportador() |
|
151 | |||
152 | 46 | public function setTransportador($transportador) |
|
157 | |||
158 | /** |
||
159 | * Dados da retenção ICMS do Transporte |
||
160 | */ |
||
161 | 14 | public function getRetencao() |
|
165 | |||
166 | 46 | public function setRetencao($retencao) |
|
171 | |||
172 | /** |
||
173 | * Dados do veículo |
||
174 | */ |
||
175 | 14 | public function getVeiculo() |
|
179 | |||
180 | 46 | public function setVeiculo($veiculo) |
|
185 | |||
186 | /** |
||
187 | * Dados do reboque/Dolly (v2.0) |
||
188 | */ |
||
189 | 14 | public function getReboque() |
|
193 | |||
194 | 46 | public function setReboque($reboque) |
|
199 | |||
200 | /** |
||
201 | * Identificação do vagão (v2.0) |
||
202 | */ |
||
203 | 14 | public function getVagao($normalize = false) |
|
210 | |||
211 | 46 | public function setVagao($vagao) |
|
216 | |||
217 | /** |
||
218 | * Identificação da balsa (v2.0) |
||
219 | */ |
||
220 | 14 | public function getBalsa($normalize = false) |
|
227 | |||
228 | 46 | public function setBalsa($balsa) |
|
233 | |||
234 | /** |
||
235 | * Dados dos volumes |
||
236 | */ |
||
237 | 14 | public function getVolumes() |
|
241 | |||
242 | 46 | public function setVolumes($volumes) |
|
247 | |||
248 | 3 | public function addVolume($volume) |
|
253 | |||
254 | 11 | public function toArray($recursive = false) |
|
255 | { |
||
256 | 11 | $transporte = []; |
|
257 | 11 | $transporte['frete'] = $this->getFrete(); |
|
258 | 11 | if (!is_null($this->getTransportador()) && $recursive) { |
|
259 | 1 | $transporte['transportador'] = $this->getTransportador()->toArray($recursive); |
|
260 | } else { |
||
261 | 10 | $transporte['transportador'] = $this->getTransportador(); |
|
262 | } |
||
263 | 11 | if (!is_null($this->getRetencao()) && $recursive) { |
|
264 | 1 | $transporte['retencao'] = $this->getRetencao()->toArray($recursive); |
|
265 | } else { |
||
266 | 10 | $transporte['retencao'] = $this->getRetencao(); |
|
267 | } |
||
268 | 11 | if (!is_null($this->getVeiculo()) && $recursive) { |
|
269 | 1 | $transporte['veiculo'] = $this->getVeiculo()->toArray($recursive); |
|
270 | } else { |
||
271 | 10 | $transporte['veiculo'] = $this->getVeiculo(); |
|
272 | } |
||
273 | 11 | if (!is_null($this->getReboque()) && $recursive) { |
|
274 | 1 | $transporte['reboque'] = $this->getReboque()->toArray($recursive); |
|
275 | } else { |
||
276 | 10 | $transporte['reboque'] = $this->getReboque(); |
|
277 | } |
||
278 | 11 | $transporte['vagao'] = $this->getVagao(); |
|
279 | 11 | $transporte['balsa'] = $this->getBalsa(); |
|
280 | 11 | if ($recursive) { |
|
281 | 2 | $volumes = []; |
|
282 | 2 | $_volumes = $this->getVolumes(); |
|
283 | 2 | foreach ($_volumes as $_volume) { |
|
284 | $volumes[] = $_volume->toArray($recursive); |
||
285 | } |
||
286 | 2 | $transporte['volumes'] = $volumes; |
|
287 | } else { |
||
288 | 9 | $transporte['volumes'] = $this->getVolumes(); |
|
289 | } |
||
290 | 11 | return $transporte; |
|
291 | } |
||
292 | |||
293 | 46 | public function fromArray($transporte = []) |
|
330 | |||
331 | 40 | public function getNode($name = null) |
|
375 | |||
376 | 33 | public function loadNode($element, $name = null) |
|
433 | } |
||
434 |
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.