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- Por conta do emitente; |
||
| 45 | * 1- Por conta do |
||
| 46 | * destinatário/remetente; |
||
| 47 | * 2- Por conta de terceiros; |
||
| 48 | * 9- Sem frete (v2.0) |
||
| 49 | */ |
||
| 50 | const FRETE_EMITENTE = 'emitente'; |
||
| 51 | const FRETE_DESTINATARIO = 'destinatario'; |
||
| 52 | const FRETE_TERCEIROS = 'terceiros'; |
||
| 53 | const FRETE_NENHUM = 'nenhum'; |
||
| 54 | |||
| 55 | private $frete; |
||
| 56 | private $transportador; |
||
| 57 | private $retencao; |
||
| 58 | private $veiculo; |
||
| 59 | private $reboque; |
||
| 60 | private $vagao; |
||
| 61 | private $balsa; |
||
| 62 | private $volumes; |
||
| 63 | |||
| 64 | 40 | public function __construct($transporte = array()) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Modalidade do frete |
||
| 71 | * 0- Por conta do emitente; |
||
| 72 | * 1- Por conta do |
||
| 73 | * destinatário/remetente; |
||
| 74 | * 2- Por conta de terceiros; |
||
| 75 | * 9- Sem frete (v2.0) |
||
| 76 | */ |
||
| 77 | 35 | public function getFrete($normalize = false) |
|
| 78 | { |
||
| 79 | 35 | if (!$normalize) { |
|
| 80 | 35 | return $this->frete; |
|
| 81 | } |
||
| 82 | 35 | switch ($this->frete) { |
|
| 83 | 35 | case self::FRETE_EMITENTE: |
|
| 84 | 2 | return '0'; |
|
| 85 | 33 | case self::FRETE_DESTINATARIO: |
|
| 86 | 2 | return '1'; |
|
| 87 | 32 | case self::FRETE_TERCEIROS: |
|
| 88 | 2 | return '2'; |
|
| 89 | 30 | case self::FRETE_NENHUM: |
|
| 90 | 29 | return '9'; |
|
| 91 | } |
||
| 92 | 1 | return $this->frete; |
|
| 93 | } |
||
| 94 | |||
| 95 | 40 | public function setFrete($frete) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Dados da transportadora |
||
| 117 | */ |
||
| 118 | 7 | public function getTransportador() |
|
| 122 | |||
| 123 | 40 | public function setTransportador($transportador) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Dados da retenção ICMS do Transporte |
||
| 131 | */ |
||
| 132 | 7 | public function getRetencao() |
|
| 136 | |||
| 137 | 40 | public function setRetencao($retencao) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Dados do veículo |
||
| 145 | */ |
||
| 146 | 7 | public function getVeiculo() |
|
| 150 | |||
| 151 | 40 | public function setVeiculo($veiculo) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Dados do reboque/Dolly (v2.0) |
||
| 159 | */ |
||
| 160 | 7 | public function getReboque() |
|
| 164 | |||
| 165 | 40 | public function setReboque($reboque) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Identificação do vagão (v2.0) |
||
| 173 | */ |
||
| 174 | 7 | public function getVagao($normalize = false) |
|
| 181 | |||
| 182 | 40 | public function setVagao($vagao) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Identificação da balsa (v2.0) |
||
| 190 | */ |
||
| 191 | 7 | public function getBalsa($normalize = false) |
|
| 198 | |||
| 199 | 40 | public function setBalsa($balsa) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Dados dos volumes |
||
| 207 | */ |
||
| 208 | 7 | public function getVolumes() |
|
| 212 | |||
| 213 | 40 | public function setVolumes($volumes) |
|
| 218 | |||
| 219 | 3 | public function addVolume($volume) |
|
| 224 | |||
| 225 | 4 | public function toArray($recursive = false) |
|
| 226 | { |
||
| 227 | 4 | $transporte = array(); |
|
| 228 | 4 | $transporte['frete'] = $this->getFrete(); |
|
| 229 | 4 | if (!is_null($this->getTransportador()) && $recursive) { |
|
| 230 | $transporte['transportador'] = $this->getTransportador()->toArray($recursive); |
||
| 231 | } else { |
||
| 232 | 4 | $transporte['transportador'] = $this->getTransportador(); |
|
| 233 | } |
||
| 234 | 4 | if (!is_null($this->getRetencao()) && $recursive) { |
|
| 235 | $transporte['retencao'] = $this->getRetencao()->toArray($recursive); |
||
| 236 | } else { |
||
| 237 | 4 | $transporte['retencao'] = $this->getRetencao(); |
|
| 238 | } |
||
| 239 | 4 | if (!is_null($this->getVeiculo()) && $recursive) { |
|
| 240 | $transporte['veiculo'] = $this->getVeiculo()->toArray($recursive); |
||
| 241 | } else { |
||
| 242 | 4 | $transporte['veiculo'] = $this->getVeiculo(); |
|
| 243 | } |
||
| 244 | 4 | if (!is_null($this->getReboque()) && $recursive) { |
|
| 245 | $transporte['reboque'] = $this->getReboque()->toArray($recursive); |
||
| 246 | } else { |
||
| 247 | 4 | $transporte['reboque'] = $this->getReboque(); |
|
| 248 | } |
||
| 249 | 4 | $transporte['vagao'] = $this->getVagao(); |
|
| 250 | 4 | $transporte['balsa'] = $this->getBalsa(); |
|
| 251 | 4 | if ($recursive) { |
|
| 252 | 1 | $volumes = array(); |
|
| 253 | 1 | $_volumes = $this->getVolumes(); |
|
| 254 | 1 | foreach ($_volumes as $_volume) { |
|
| 255 | $volumes[] = $_volume->toArray($recursive); |
||
| 256 | } |
||
| 257 | 1 | $transporte['volumes'] = $volumes; |
|
| 258 | } else { |
||
| 259 | 3 | $transporte['volumes'] = $this->getVolumes(); |
|
| 260 | } |
||
| 261 | 4 | return $transporte; |
|
| 262 | } |
||
| 263 | |||
| 264 | 40 | public function fromArray($transporte = array()) |
|
| 313 | |||
| 314 | 35 | public function getNode($name = null) |
|
| 358 | |||
| 359 | 31 | public function loadNode($element, $name = null) |
|
| 416 | } |
||
| 417 |
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.