Complex classes like Tarefa 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 Tarefa, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class Tarefa |
||
|
|||
31 | { |
||
32 | |||
33 | /** |
||
34 | * Ação a ser realizada sobre o objeto ou recibo |
||
35 | */ |
||
36 | const ACAO_CONSULTAR = 'consultar'; |
||
37 | const ACAO_INUTILIZAR = 'inutilizar'; |
||
38 | const ACAO_CANCELAR = 'cancelar'; |
||
39 | |||
40 | private $id; |
||
41 | private $acao; |
||
42 | private $nota; |
||
43 | private $documento; |
||
44 | private $agente; |
||
45 | private $resposta; |
||
46 | |||
47 | 13 | public function __construct($tarefa = array()) |
|
51 | |||
52 | /** |
||
53 | * Código aleatório e opcional que identifica a tarefa |
||
54 | */ |
||
55 | 12 | public function getID() |
|
56 | { |
||
57 | 12 | return $this->id; |
|
58 | } |
||
59 | |||
60 | 13 | public function setID($id) |
|
65 | |||
66 | /** |
||
67 | * Ação a ser realizada sobre o objeto ou recibo |
||
68 | */ |
||
69 | 12 | public function getAcao() |
|
70 | { |
||
71 | 12 | return $this->acao; |
|
72 | } |
||
73 | |||
74 | 13 | public function setAcao($acao) |
|
79 | |||
80 | /** |
||
81 | * Nota que será processada se informado |
||
82 | */ |
||
83 | 12 | public function getNota() |
|
84 | { |
||
85 | 12 | return $this->nota; |
|
86 | } |
||
87 | |||
88 | 13 | public function setNota($nota) |
|
93 | |||
94 | /** |
||
95 | * Informa o XML do objeto, quando não informado o XML é gerado a partir do |
||
96 | * objeto |
||
97 | */ |
||
98 | 4 | public function getDocumento() |
|
99 | { |
||
100 | 4 | return $this->documento; |
|
101 | } |
||
102 | |||
103 | 13 | public function setDocumento($documento) |
|
108 | |||
109 | /** |
||
110 | * Agente que obteve ou vai obter a resposta, podendo ser: pedido de |
||
111 | * inutilização(NF\Inutilizacao), recibo(NF\Recibo) ou pedido de |
||
112 | * cancelamento(NF\Evento) |
||
113 | */ |
||
114 | 12 | public function getAgente() |
|
115 | { |
||
116 | 12 | return $this->agente; |
|
117 | } |
||
118 | |||
119 | 13 | public function setAgente($agente) |
|
124 | |||
125 | /** |
||
126 | * Resposta da tarefa após ser executada |
||
127 | */ |
||
128 | 5 | public function getResposta() |
|
129 | { |
||
130 | 5 | return $this->resposta; |
|
131 | } |
||
132 | |||
133 | 13 | public function setResposta($resposta) |
|
138 | |||
139 | 2 | public function toArray($recursive = false) |
|
140 | { |
||
141 | 2 | $tarefa = array(); |
|
142 | 2 | $tarefa['id'] = $this->getID(); |
|
143 | 2 | $tarefa['acao'] = $this->getAcao(); |
|
144 | 2 | if (!is_null($this->getNota()) && $recursive) { |
|
166 | |||
167 | 13 | public function fromArray($tarefa = array()) |
|
206 | |||
207 | /** |
||
208 | * Resposta da tarefa após ser executada |
||
209 | */ |
||
210 | 12 | public function executa() |
|
230 | |||
231 | 4 | private function cancela() |
|
271 | |||
272 | 3 | private function inutiliza() |
|
273 | { |
||
274 | 3 | $nota = $this->getNota(); |
|
275 | 3 | $inutilizacao = $this->getAgente(); |
|
276 | 3 | if (is_null($inutilizacao)) { |
|
277 | 1 | if (is_null($nota)) { |
|
278 | 1 | throw new \Exception('A nota não foi informada na tarefa de inutilização "'.$this->getID().'"', 404); |
|
279 | } |
||
280 | $inutilizacao = new Inutilizacao(); |
||
281 | $inutilizacao->setAno(date('Y')); |
||
282 | $inutilizacao->setJustificativa($nota->getJustificativa()); |
||
283 | $this->setAgente($inutilizacao); |
||
284 | 2 | } elseif (!($inutilizacao instanceof Inutilizacao)) { |
|
285 | 1 | throw new \Exception('O agente informado não é uma inutilização', 500); |
|
286 | } |
||
287 | 1 | if (!is_null($nota)) { |
|
288 | 1 | $inutilizacao->setCNPJ($nota->getEmitente()->getCNPJ()); |
|
289 | 1 | $inutilizacao->setSerie($nota->getSerie()); |
|
290 | 1 | $inutilizacao->setInicio($nota->getNumero()); |
|
291 | 1 | $inutilizacao->setFinal($nota->getNumero()); |
|
292 | 1 | $inutilizacao->setUF($nota->getEmitente()->getEndereco()-> |
|
293 | 1 | getMunicipio()->getEstado()->getUF()); |
|
294 | 1 | $inutilizacao->setAmbiente($nota->getAmbiente()); |
|
295 | 1 | $inutilizacao->setModelo($nota->getModelo()); |
|
296 | } |
||
297 | 1 | $dom = $inutilizacao->getNode()->ownerDocument; |
|
298 | 1 | $dom = $inutilizacao->assinar($dom); |
|
299 | 1 | $dom = $inutilizacao->validar($dom); |
|
300 | 1 | $dom = $inutilizacao->envia($dom); |
|
301 | 1 | $this->setDocumento($dom); |
|
302 | 1 | return $inutilizacao; |
|
303 | } |
||
304 | |||
305 | 5 | private function consulta() |
|
336 | } |
||
337 |
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.