Complex classes like Tools 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 Tools, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Tools extends ToolsBase |
||
| 28 | { |
||
| 29 | const CONSULTA_CONSOLIDADA = 1; |
||
| 30 | const CONSULTA_R1000 = 2; |
||
| 31 | const CONSULTA_R1070 = 3; |
||
| 32 | const CONSULTA_R2010 = 4; |
||
| 33 | const CONSULTA_R2020 = 5; |
||
| 34 | const CONSULTA_R2030 = 6; |
||
| 35 | const CONSULTA_R2040 = 7; |
||
| 36 | const CONSULTA_R2050 = 8; |
||
| 37 | const CONSULTA_R2055 = 13; |
||
| 38 | const CONSULTA_R2060 = 9; |
||
| 39 | const CONSULTA_R2098 = 10; |
||
| 40 | const CONSULTA_R2099 = 11; |
||
| 41 | const CONSULTA_R3010 = 12; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | public $lastRequest; |
||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | public $lastResponse; |
||
| 51 | /** |
||
| 52 | * @var SoapInterface |
||
| 53 | */ |
||
| 54 | public $soap; |
||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | public $namespace = 'http://sped.fazenda.gov.br/'; |
||
| 59 | /** |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $soapnamespaces = [ |
||
| 63 | 'xmlns:soapenv' => "http://schemas.xmlsoap.org/soap/envelope/", |
||
| 64 | 'xmlns:sped'=> "http://sped.fazenda.gov.br/" |
||
| 65 | ]; |
||
| 66 | /** |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $uri = [ |
||
| 70 | '1' => 'https://reinf.receita.fazenda.gov.br/WsREINF/RecepcaoLoteReinf.svc', |
||
| 71 | '2' => 'https://preprodefdreinf.receita.fazenda.gov.br/WsREINF/RecepcaoLoteReinf.svc' |
||
| 72 | |||
| 73 | ]; |
||
| 74 | /** |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | protected $uriconsulta = [ |
||
| 78 | '1' => 'https://reinf.receita.fazenda.gov.br/WsReinfConsultas/ConsultasReinf.svc', |
||
| 79 | '2' => 'https://preprodefdreinf.receita.fazenda.gov.br/WsReinfConsultas/ConsultasReinf.svc' |
||
| 80 | ]; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | protected $action; |
||
| 86 | /** |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | protected $method; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Constructor |
||
| 93 | * @param string $config |
||
| 94 | * @param Certificate $certificate |
||
| 95 | */ |
||
| 96 | public function __construct($config, Certificate $certificate) |
||
| 97 | { |
||
| 98 | parent::__construct($config, $certificate); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * SOAP communication dependency injection |
||
| 103 | * @param SoapInterface $soap |
||
| 104 | */ |
||
| 105 | public function loadSoapClass(SoapInterface $soap) |
||
| 106 | { |
||
| 107 | $this->soap = $soap; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Run EFD-REINF Query |
||
| 112 | * @param integer $mod |
||
| 113 | * @param stdClass $std |
||
| 114 | * @throws ProcessException |
||
| 115 | */ |
||
| 116 | public function consultar($mod, stdClass $std = null) |
||
| 117 | { |
||
| 118 | if (isset($std)) { |
||
| 119 | //converte os nomes das propriedades do stdClass para caixa baixa |
||
| 120 | $std = Factory::propertiesToLower($std); |
||
| 121 | } |
||
| 122 | switch ($mod) { |
||
| 123 | case 1: |
||
| 124 | $evt = 0; |
||
| 125 | $request = $this->consultConsolidadas($evt, $std); |
||
|
|
|||
| 126 | break; |
||
| 127 | case 2: |
||
| 128 | $evt = 1000; |
||
| 129 | $request = $this->consultR1($evt); |
||
| 130 | break; |
||
| 131 | case 3: |
||
| 132 | $evt = 1070; |
||
| 133 | $request = $this->consultR1($evt); |
||
| 134 | break; |
||
| 135 | case 4: |
||
| 136 | $evt = 2010; |
||
| 137 | $request = $this->consultR2010($evt, $std); |
||
| 138 | break; |
||
| 139 | case 5: |
||
| 140 | $evt = 2020; |
||
| 141 | $request = $this->consultR2020($evt, $std); |
||
| 142 | break; |
||
| 143 | case 6: |
||
| 144 | $evt = 2030; |
||
| 145 | $request = $this->consultR20($evt, $std); |
||
| 146 | break; |
||
| 147 | case 7: |
||
| 148 | $evt = 2040; |
||
| 149 | $request = $this->consultR20($evt, $std); |
||
| 150 | break; |
||
| 151 | case 8: |
||
| 152 | $evt = 2050; |
||
| 153 | $request = $this->consultR20($evt, $std); |
||
| 154 | break; |
||
| 155 | case 9: |
||
| 156 | $evt = 2060; |
||
| 157 | $request = $this->consultR2060($evt, $std); |
||
| 158 | break; |
||
| 159 | case 10: |
||
| 160 | $evt = 2098; |
||
| 161 | $request = $this->consultR209($evt, $std); |
||
| 162 | break; |
||
| 163 | case 11: |
||
| 164 | $evt = 2099; |
||
| 165 | $request = $this->consultR209($evt, $std); |
||
| 166 | break; |
||
| 167 | case 12: |
||
| 168 | $evt = 3010; |
||
| 169 | $request = $this->consultR3010($evt, $std); |
||
| 170 | break; |
||
| 171 | case 13: |
||
| 172 | $evt = 2055; |
||
| 173 | $request = $this->consultR2055($evt, $std); |
||
| 174 | break; |
||
| 175 | default: |
||
| 176 | throw ProcessException::wrongArgument(2003, ''); |
||
| 177 | } |
||
| 178 | $this->lastResponse = $this->sendRequest($request); |
||
| 179 | return $this->lastResponse; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Consultation of consolidated information |
||
| 184 | * @param integer $evt |
||
| 185 | * @param stdClass $std |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | public function consultConsolidadas($evt, stdClass $std) |
||
| 189 | { |
||
| 190 | $properties = [ |
||
| 191 | 'numeroprotocolofechamento' => [ |
||
| 192 | 'required' => true, |
||
| 193 | 'type' => 'string', |
||
| 194 | 'regex' => '' |
||
| 195 | ], |
||
| 196 | ]; |
||
| 197 | $this->validInputParameters($properties, $std); |
||
| 198 | |||
| 199 | $this->method = "ConsultaInformacoesConsolidadas"; |
||
| 200 | $this->action = "{$this->namespace}ConsultasReinf/{$this->method}"; |
||
| 201 | $request = "<sped:{$this->method}>" |
||
| 202 | . "<sped:tipoInscricaoContribuinte>{$this->tpInsc}</sped:tipoInscricaoContribuinte>" |
||
| 203 | . "<sped:numeroInscricaoContribuinte>{$this->nrInsc}</sped:numeroInscricaoContribuinte>" |
||
| 204 | . "<sped:numeroProtocoloFechamento>{$std->numeroprotocolofechamento}</sped:numeroProtocoloFechamento>" |
||
| 205 | . "</sped:{$this->method}>"; |
||
| 206 | return $request; |
||
| 207 | } |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * Consultation R1000 and R1070 |
||
| 212 | * @param integer $evt |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | protected function consultR1($evt) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Consultation R2010 |
||
| 229 | * @param integer $evt |
||
| 230 | * @param stdClass $std |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | protected function consultR2010($evt, $std) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Consultation R2020 |
||
| 282 | * @param integer $evt |
||
| 283 | * @param stdClass $std |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | protected function consultR2020($evt, $std) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Consultation R2030, R2040, R2050 |
||
| 333 | * @param integer $evt |
||
| 334 | * @param stdClass $std |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | protected function consultR20($evt, $std) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Consultation R2055 |
||
| 376 | * @param integer $evt |
||
| 377 | * @param stdClass $std |
||
| 378 | * @return string |
||
| 379 | */ |
||
| 380 | protected function consultR2055($evt, $std) |
||
| 417 | |||
| 418 | |||
| 419 | /** |
||
| 420 | * Consultation R2060 |
||
| 421 | * @param integer $evt |
||
| 422 | * @param stdClass $std |
||
| 423 | * @return string |
||
| 424 | */ |
||
| 425 | protected function consultR2060($evt, $std) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Consultation R2098 and R2099 |
||
| 471 | * @param integer $evt |
||
| 472 | * @param stdClass $std |
||
| 473 | * @return string |
||
| 474 | */ |
||
| 475 | protected function consultR209($evt, $std) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Consultation R3010 |
||
| 501 | * @param integer $evt |
||
| 502 | * @param stdClass $std |
||
| 503 | * @return string |
||
| 504 | */ |
||
| 505 | protected function consultR3010($evt, $std) |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Send batch of events |
||
| 542 | * @param integer $grupo |
||
| 543 | * @param array $eventos |
||
| 544 | * @return string |
||
| 545 | */ |
||
| 546 | public function enviarLoteEventos($grupo, $eventos = []) |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Send request to webservice |
||
| 599 | * @param string $request |
||
| 600 | * @return string |
||
| 601 | */ |
||
| 602 | protected function sendRequest($request) |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Verify the availability of a digital certificate. |
||
| 641 | * If available, place it where it is needed |
||
| 642 | * @param FactoryInterface $evento |
||
| 643 | * @throws RuntimeException |
||
| 644 | */ |
||
| 645 | protected function checkCertificate(FactoryInterface $evento) |
||
| 653 | |||
| 654 | protected function validInputParameters($properties, $std) |
||
| 676 | } |
||
| 677 |
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):