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 ToolsCommon |
||
| 28 | { |
||
| 29 | const EVT_CONFIRMACAO = 210200; |
||
| 30 | const EVT_CIENCIA = 210210; |
||
| 31 | const EVT_DESCONHECIMENTO = 210220; |
||
| 32 | const EVT_NAO_REALIZADA = 210240; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Request authorization to issue CTe in batch with one or more documents |
||
| 36 | * @param array $aXml array of cte's xml |
||
| 37 | * @param string $idLote lote number |
||
| 38 | * @param bool $compactar flag to compress data with gzip |
||
| 39 | * @return string soap response xml |
||
| 40 | */ |
||
| 41 | public function sefazEnviaLote( |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Request authorization to issue CTe OS with one document only |
||
| 94 | * @param type $xml |
||
| 95 | * @return type |
||
| 96 | */ |
||
| 97 | public function sefazEnviaCTeOS($xml) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Check status of Batch of CTe sent by receipt of this shipment |
||
| 117 | * @param string $recibo |
||
| 118 | * @param int $tpAmb |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | public function sefazConsultaRecibo($recibo, $tpAmb = null) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Check the CTe status for the 44-digit key and retrieve the protocol |
||
| 152 | * @param string $chave |
||
| 153 | * @param int $tpAmb |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | public function sefazConsultaChave($chave, $tpAmb = null) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Request to disable one or an NFe sequence of a given series |
||
| 185 | * @param int $nSerie |
||
| 186 | * @param int $nIni |
||
| 187 | * @param int $nFin |
||
| 188 | * @param string $xJust |
||
| 189 | * @param int $tpAmb |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | public function sefazInutiliza( |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Search for the registration data of an NFe issuer, |
||
| 262 | * if in contingency mode this service will cause a |
||
| 263 | * Exception and remember not all Sefaz have this service available, |
||
| 264 | * so it will not work in some cases. |
||
| 265 | * @param string $uf federation unit |
||
| 266 | * @param string $cnpj CNPJ number (optional) |
||
| 267 | * @param string $iest IE number (optional) |
||
| 268 | * @param string $cpf CPF number (optional) |
||
| 269 | * @return string xml soap response |
||
| 270 | */ |
||
| 271 | public function sefazCadastro( |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Check services status SEFAZ/SVC |
||
| 311 | * If $uf is empty use normal check with contingency |
||
| 312 | * If $uf is NOT empty ignore contingency mode |
||
| 313 | * @param string $uf initials of federation unit |
||
| 314 | * @param int $tpAmb |
||
| 315 | * @return string xml soap response |
||
| 316 | */ |
||
| 317 | public function sefazStatus($uf = '', $tpAmb = null) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Service for the distribution of summary information and |
||
| 348 | * electronic tax documents of interest to an actor. |
||
| 349 | * @param integer $ultNSU last NSU number recived |
||
| 350 | * @param integer $numNSU NSU number you wish to consult |
||
| 351 | * @param string $fonte data source 'AN' and for some cases it may be 'RS' |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | public function sefazDistDFe( |
||
| 355 | $ultNSU = 0, |
||
| 356 | $numNSU = 0, |
||
| 357 | $fonte = 'AN' |
||
| 358 | ) { |
||
| 359 | //carrega serviço |
||
| 360 | $servico = 'CTeDistribuicaoDFe'; |
||
| 361 | $this->checkContingencyForWebServices($servico); |
||
| 362 | $this->servico( |
||
| 363 | $servico, |
||
| 364 | $fonte, |
||
| 365 | $this->tpAmb, |
||
| 366 | true |
||
| 367 | ); |
||
| 368 | $cUF = UFList::getCodeByUF($this->config->siglaUF); |
||
| 369 | $ultNSU = str_pad($ultNSU, 15, '0', STR_PAD_LEFT); |
||
| 370 | $tagNSU = "<distNSU><ultNSU>$ultNSU</ultNSU></distNSU>"; |
||
| 371 | if ($numNSU != 0) { |
||
| 372 | $numNSU = str_pad($numNSU, 15, '0', STR_PAD_LEFT); |
||
| 373 | $tagNSU = "<consNSU><NSU>$numNSU</NSU></consNSU>"; |
||
| 374 | } |
||
| 375 | //monta a consulta |
||
| 376 | $consulta = "<distDFeInt xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
||
| 377 | . "<tpAmb>".$this->tpAmb."</tpAmb>" |
||
| 378 | . "<cUFAutor>$cUF</cUFAutor>" |
||
| 379 | . ((strlen($this->config->cnpj)==14) ? |
||
| 380 | "<CNPJ>".$this->config->cnpj."</CNPJ>" : |
||
| 381 | "<CPF>".$this->config->cnpj."</CPF>" |
||
| 382 | ) |
||
| 383 | . $tagNSU."</distDFeInt>"; |
||
| 384 | //valida o xml da requisição |
||
| 385 | $this->isValid($this->urlVersion, $consulta, 'distDFeInt'); |
||
| 386 | $this->lastRequest = $consulta; |
||
| 387 | //montagem dos dados da mensagem SOAP |
||
| 388 | $request = "<cteDadosMsg xmlns=\"$this->urlNamespace\">$consulta</cteDadosMsg>"; |
||
| 389 | $parameters = ['cteDistDFeInteresse' => $request]; |
||
| 390 | $body = "<cteDistDFeInteresse xmlns=\"$this->urlNamespace\">" |
||
| 391 | . $request |
||
| 392 | . "</cteDistDFeInteresse>"; |
||
| 393 | //este webservice não requer cabeçalho |
||
| 394 | $this->objHeader = null; |
||
| 395 | $this->lastResponse = $this->sendRequest($body, $parameters); |
||
| 396 | return $this->lastResponse; |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Request authorization for Letter of Correction |
||
| 401 | * @param string $chave |
||
| 402 | * @param array $infCorrecao |
||
| 403 | * @param int $nSeqEvento |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | public function sefazCCe($chave, $infCorrecao = [], $nSeqEvento = 1) |
||
| 407 | { |
||
| 408 | $uf = $this->validKeyByUF($chave); |
||
| 409 | $tpEvento = 110110; |
||
| 410 | $tagAdic = self::serializerCCe($infCorrecao); |
||
| 411 | return $this->sefazEvento( |
||
| 412 | $uf, |
||
| 413 | $chave, |
||
| 414 | $tpEvento, |
||
| 415 | $nSeqEvento, |
||
| 416 | $tagAdic |
||
| 417 | ); |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Request extension of the term of return of products of an NF-e of |
||
| 422 | * consignment for industrialization to order with suspension of ICMS |
||
| 423 | * in interstate operations |
||
| 424 | * @param string $chNFe |
||
| 425 | * @param string $nProt |
||
| 426 | * @param integer $tipo 1-primerio prazo, 2-segundo prazo |
||
| 427 | * @param array $itens |
||
| 428 | * @param integer $nSeqEvento |
||
| 429 | * @return string |
||
| 430 | */ |
||
| 431 | public function sefazEPP( |
||
| 432 | $chNFe, |
||
| 433 | $nProt, |
||
| 434 | $itens = array(), |
||
| 435 | $tipo = 1, |
||
| 436 | $nSeqEvento = 1 |
||
| 437 | ) { |
||
| 438 | $uf = UFList::getUFByCode(substr($chNFe, 0, 2)); |
||
| 439 | $tpEvento = 111500; |
||
| 440 | if ($tipo == 2) { |
||
| 441 | $tpEvento = 111501; |
||
| 442 | } |
||
| 443 | $tagAdic = "<nProt>$nProt</nProt>"; |
||
| 444 | foreach ($itens as $item) { |
||
| 445 | $tagAdic .= "<itemPedido numItem=\"" |
||
| 446 | . $item[0] |
||
| 447 | . "\"><qtdeItem>" |
||
| 448 | . $item[1] |
||
| 449 | ."</qtdeItem></itemPedido>"; |
||
| 450 | } |
||
| 451 | return $this->sefazEvento( |
||
| 452 | $uf, |
||
| 453 | $chNFe, |
||
| 454 | $tpEvento, |
||
| 455 | $nSeqEvento, |
||
| 456 | $tagAdic |
||
| 457 | ); |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Request the cancellation of the request for an extension of the term |
||
| 462 | * of return of products of an NF-e of consignment for industrialization |
||
| 463 | * by order with suspension of ICMS in interstate operations |
||
| 464 | * @param string $chNFe |
||
| 465 | * @param string $nProt |
||
| 466 | * @param integer $nSeqEvento |
||
| 467 | * @return string |
||
| 468 | */ |
||
| 469 | public function sefazECPP( |
||
| 470 | $chNFe, |
||
| 471 | $nProt, |
||
| 472 | $nSeqEvento = 1 |
||
| 473 | ) { |
||
| 474 | $uf = UFList::getUFByCode(substr($chNFe, 0, 2)); |
||
| 475 | $tpEvento = 111502; |
||
| 476 | $origEvent = 111500; |
||
| 477 | if ($nSeqEvento == 2) { |
||
| 478 | $tpEvento = 111503; |
||
| 479 | $origEvent = 111501; |
||
| 480 | } |
||
| 481 | $sSeqEvento = str_pad($nSeqEvento, 2, "0", STR_PAD_LEFT); |
||
| 482 | $idPedidoCancelado = "ID$origEvent$chNFe$sSeqEvento"; |
||
| 483 | $tagAdic = "<idPedidoCancelado>" |
||
| 484 | . "$idPedidoCancelado" |
||
| 485 | . "</idPedidoCancelado>" |
||
| 486 | . "<nProt>$nProt</nProt>"; |
||
| 487 | return $this->sefazEvento( |
||
| 488 | $uf, |
||
| 489 | $chNFe, |
||
| 490 | $tpEvento, |
||
| 491 | $nSeqEvento, |
||
| 492 | $tagAdic |
||
| 493 | ); |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Requires cte cancellation |
||
| 498 | * @param string $chave key of CTe |
||
| 499 | * @param string $xJust justificative 255 characters max |
||
| 500 | * @param string $nProt protocol number |
||
| 501 | * @return string |
||
| 502 | */ |
||
| 503 | public function sefazCancela($chave, $xJust, $nProt) |
||
| 504 | { |
||
| 505 | $uf = $this->validKeyByUF($chave); |
||
| 506 | $xJust = Strings::replaceSpecialsChars( |
||
| 507 | substr(trim($xJust), 0, 255) |
||
| 508 | ); |
||
| 509 | $tpEvento = 110111; |
||
| 510 | $nSeqEvento = 1; |
||
| 511 | $tagAdic = "<evCancCTe>" |
||
| 512 | . "<descEvento>Cancelamento</descEvento>" |
||
| 513 | . "<nProt>$nProt</nProt>" |
||
| 514 | . "<xJust>$xJust</xJust>" |
||
| 515 | . "</evCancCTe>"; |
||
| 516 | return $this->sefazEvento( |
||
| 517 | $uf, |
||
| 518 | $chave, |
||
| 519 | $tpEvento, |
||
| 520 | $nSeqEvento, |
||
| 521 | $tagAdic |
||
| 522 | ); |
||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Request the registration of the manifestation of recipient |
||
| 527 | * @param string $chNFe |
||
| 528 | * @param int $tpEvento |
||
| 529 | * @param string $xJust Justification for not carrying out the operation |
||
| 530 | * @param int $nSeqEvento |
||
| 531 | * @return string |
||
| 532 | */ |
||
| 533 | public function sefazManifesta( |
||
| 534 | $chNFe, |
||
| 535 | $tpEvento, |
||
| 536 | $xJust = '', |
||
| 537 | $nSeqEvento = 1, |
||
| 538 | $ufEvento = 'RS' |
||
| 539 | ) { |
||
| 540 | $tagAdic = ''; |
||
| 541 | if ($tpEvento == 610110) { |
||
| 542 | $xJust = Strings::replaceSpecialsChars(substr(trim($xJust), 0, 255)); |
||
| 543 | $tagAdic = "<evPrestDesacordo>" |
||
| 544 | . "<descEvento>Prestacao do Servico em Desacordo</descEvento>" |
||
| 545 | . "<indDesacordoOper>1</indDesacordoOper>" |
||
| 546 | . "<xObs>$xJust</xObs>" |
||
| 547 | . "</evPrestDesacordo>"; |
||
| 548 | } |
||
| 549 | return $this->sefazEvento( |
||
| 550 | $ufEvento, |
||
| 551 | $chNFe, |
||
| 552 | $tpEvento, |
||
| 553 | $nSeqEvento, |
||
| 554 | $tagAdic |
||
| 555 | ); |
||
| 556 | } |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Request authorization for issuance in contingency EPEC |
||
| 560 | * @param string $xml |
||
| 561 | * @return string |
||
| 562 | */ |
||
| 563 | public function sefazEPEC(&$xml) |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Send event to SEFAZ |
||
| 636 | * @param string $uf |
||
| 637 | * @param string $chave |
||
| 638 | * @param int $tpEvento |
||
| 639 | * @param int $nSeqEvento |
||
| 640 | * @param string $tagAdic |
||
| 641 | * @return string |
||
| 642 | */ |
||
| 643 | public function sefazEvento( |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Request the NFe download already manifested by its recipient, by the key |
||
| 708 | * using new service in CTeDistribuicaoDFe |
||
| 709 | * @param string $chave |
||
| 710 | * @return string |
||
| 711 | */ |
||
| 712 | public function sefazDownload($chave) |
||
| 744 | |||
| 745 | /** |
||
| 746 | * Maintenance of the Taxpayer Security Code - CSC (Old Token) |
||
| 747 | * @param int $indOp Identificador do tipo de operação: |
||
| 748 | * 1 - Consulta CSC Ativos; |
||
| 749 | * 2 - Solicita novo CSC; |
||
| 750 | * 3 - Revoga CSC Ativo |
||
| 751 | * @return string |
||
| 752 | */ |
||
| 753 | public function sefazCsc($indOp) |
||
| 794 | |||
| 795 | /** |
||
| 796 | * Checks the validity of an CTe, normally used for received CTe |
||
| 797 | * @param string $cte |
||
| 798 | * @return boolean |
||
| 799 | */ |
||
| 800 | public function sefazValidate($cte) |
||
| 845 | |||
| 846 | /** |
||
| 847 | * Requires CE |
||
| 848 | * @param string $chave key of CTe |
||
| 849 | * @param string $nProt Protocolo do CTe |
||
| 850 | * @param string $xNome Nome de quem recebeu a entrega |
||
| 851 | * @param string $nDoc Documento de quem recebeu a entrega |
||
| 852 | * @param string $hash Hash da Chave de acesso do CT-e + Imagem da assinatura no formato Base64 |
||
| 853 | * @param int $latitude Latitude do ponto da entrega |
||
| 854 | * @param int $longitude Longitude do ponto da entrega |
||
| 855 | * @param int $nSeqEvento No. sequencial do evento |
||
| 856 | * @param string $dhEventoEntrega Data e hora da geração do hash da entrega |
||
| 857 | * @param array $aNFes Chave das NFes entregues |
||
| 858 | * @return string |
||
| 859 | */ |
||
| 860 | public function sefazCE( |
||
| 903 | |||
| 904 | /** |
||
| 905 | * Requires CE cancellation |
||
| 906 | * @param string $chave key of CTe |
||
| 907 | * @param string $nProt protocolo do CTe |
||
| 908 | * @param string $nProtCE protocolo do CE |
||
| 909 | * @param int $nSeqEvento No. sequencial do evento |
||
| 910 | * @return string |
||
| 911 | */ |
||
| 912 | public function sefazCancelaCE($chave, $nProt, $nProtCE, $nSeqEvento) |
||
| 929 | |||
| 930 | /** |
||
| 931 | * |
||
| 932 | * @param int $tpEvento |
||
| 933 | * @return \stdClass |
||
| 934 | * @throws Exception |
||
| 935 | */ |
||
| 936 | private function tpEv($tpEvento) |
||
| 980 | |||
| 981 | private static function serializerCCe(array $infCorrecoes) |
||
| 1015 | } |
||
| 1016 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.