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 |
||
| 34 | class Tools |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * config class |
||
| 38 | * @var \stdClass |
||
| 39 | */ |
||
| 40 | public $config; |
||
| 41 | /** |
||
| 42 | * Path to storage folder |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | public $pathwsfiles = ''; |
||
| 46 | /** |
||
| 47 | * Path to schemes folder |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | public $pathschemes = ''; |
||
| 51 | /** |
||
| 52 | * ambiente |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | public $ambiente = 'homologacao'; |
||
| 56 | /** |
||
| 57 | * Environment |
||
| 58 | * @var int |
||
| 59 | */ |
||
| 60 | public $tpAmb = 2; |
||
| 61 | /** |
||
| 62 | * contingency class |
||
| 63 | * @var Contingency |
||
| 64 | */ |
||
| 65 | public $contingency; |
||
| 66 | /** |
||
| 67 | * soap class |
||
| 68 | * @var SoapInterface |
||
| 69 | */ |
||
| 70 | public $soap; |
||
| 71 | /** |
||
| 72 | * Application version |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | public $verAplic = ''; |
||
| 76 | /** |
||
| 77 | * last soap request |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | public $lastRequest = ''; |
||
| 81 | /** |
||
| 82 | * last soap response |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | public $lastResponse = ''; |
||
| 86 | /** |
||
| 87 | * certificate class |
||
| 88 | * @var Certificate |
||
| 89 | */ |
||
| 90 | protected $certificate; |
||
| 91 | /** |
||
| 92 | * Sign algorithm from OPENSSL |
||
| 93 | * @var int |
||
| 94 | */ |
||
| 95 | protected $algorithm = OPENSSL_ALGO_SHA1; |
||
| 96 | /** |
||
| 97 | * Canonical conversion options |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | protected $canonical = [true,false,null,null]; |
||
| 101 | /** |
||
| 102 | * Model of NFe 55 or 65 |
||
| 103 | * @var int |
||
| 104 | */ |
||
| 105 | protected $modelo = 55; |
||
| 106 | /** |
||
| 107 | * Version of layout |
||
| 108 | * @var string |
||
| 109 | */ |
||
| 110 | protected $versao = '4.00'; |
||
| 111 | /** |
||
| 112 | * urlPortal |
||
| 113 | * Instância do WebService |
||
| 114 | * |
||
| 115 | * @var string |
||
| 116 | */ |
||
| 117 | protected $urlPortal = 'http://www.portalfiscal.inf.br/nfe'; |
||
| 118 | /** |
||
| 119 | * urlcUF |
||
| 120 | * @var int |
||
| 121 | */ |
||
| 122 | protected $urlcUF; |
||
| 123 | /** |
||
| 124 | * urlVersion |
||
| 125 | * @var string |
||
| 126 | */ |
||
| 127 | protected $urlVersion = ''; |
||
| 128 | /** |
||
| 129 | * urlService |
||
| 130 | * @var string |
||
| 131 | */ |
||
| 132 | protected $urlService = ''; |
||
| 133 | /** |
||
| 134 | * @var string |
||
| 135 | */ |
||
| 136 | protected $urlMethod = ''; |
||
| 137 | /** |
||
| 138 | * @var string |
||
| 139 | */ |
||
| 140 | protected $urlOperation = ''; |
||
| 141 | /** |
||
| 142 | * @var string |
||
| 143 | */ |
||
| 144 | protected $urlNamespace = ''; |
||
| 145 | /** |
||
| 146 | * @var string |
||
| 147 | */ |
||
| 148 | protected $urlAction = ''; |
||
| 149 | /** |
||
| 150 | * @var \SoapHeader | null |
||
| 151 | */ |
||
| 152 | protected $objHeader = null; |
||
| 153 | /** |
||
| 154 | * @var string |
||
| 155 | */ |
||
| 156 | protected $urlHeader = ''; |
||
| 157 | /** |
||
| 158 | * @var array |
||
| 159 | */ |
||
| 160 | protected $soapnamespaces = [ |
||
| 161 | 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance", |
||
| 162 | 'xmlns:xsd' => "http://www.w3.org/2001/XMLSchema", |
||
| 163 | 'xmlns:soap' => "http://www.w3.org/2003/05/soap-envelope" |
||
| 164 | ]; |
||
| 165 | /** |
||
| 166 | * @var array |
||
| 167 | */ |
||
| 168 | protected $availableVersions = ['4.00' => 'PL_009_V4']; |
||
| 169 | /** |
||
| 170 | * @var string |
||
| 171 | */ |
||
| 172 | protected $typePerson = 'J'; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Loads configurations and Digital Certificate, map all paths, set timezone and instanciate Contingency::class |
||
| 176 | * @param string $configJson content of config in json format |
||
| 177 | * @param Certificate $certificate |
||
| 178 | * @param Contingency|null $contingency |
||
| 179 | */ |
||
| 180 | 58 | public function __construct($configJson, Certificate $certificate, Contingency $contingency = null) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Sets environment time zone |
||
| 198 | * @param string $acronym (ou seja a sigla do estado) |
||
| 199 | * @return void |
||
| 200 | */ |
||
| 201 | 58 | public function setEnvironmentTimeZone($acronym) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Return J or F from existing type in ASN.1 certificate |
||
| 208 | * J - pessoa juridica (CNPJ) |
||
| 209 | * F - pessoa física (CPF) |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | 58 | public function getTypeOfPersonFromCertificate() |
|
| 213 | { |
||
| 214 | 58 | $cnpj = $this->certificate->getCnpj(); |
|
| 215 | 58 | $type = 'J'; |
|
| 216 | 58 | if (substr($cnpj, 0, 1) === 'N') { |
|
| 217 | //não é CNPJ, então verificar se é CPF |
||
| 218 | $cpf = $this->certificate->getCpf(); |
||
| 219 | if (substr($cpf, 0, 1) !== 'N') { |
||
| 220 | $type = 'F'; |
||
| 221 | } else { |
||
| 222 | //não foi localizado nem CNPJ e nem CPF esse certificado não é usável |
||
| 223 | //throw new RuntimeException('Faltam elementos CNPJ/CPF no certificado digital.'); |
||
| 224 | $type = ''; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | 58 | return $type; |
|
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Set application version |
||
| 232 | * @param string $ver |
||
| 233 | */ |
||
| 234 | public function setVerAplic($ver) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Load Soap Class |
||
| 241 | * Soap Class may be \NFePHP\Common\Soap\SoapNative |
||
| 242 | * or \NFePHP\Common\Soap\SoapCurl |
||
| 243 | * @param SoapInterface $soap |
||
| 244 | * @return void |
||
| 245 | */ |
||
| 246 | public function loadSoapClass(SoapInterface $soap) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Set OPENSSL Algorithm using OPENSSL constants |
||
| 254 | * @param int $algorithm |
||
| 255 | * @return void |
||
| 256 | */ |
||
| 257 | public function setSignAlgorithm($algorithm = OPENSSL_ALGO_SHA1) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Set or get model of document NFe = 55 or NFCe = 65 |
||
| 264 | * @param int $model |
||
| 265 | * @return int modelo class parameter |
||
| 266 | */ |
||
| 267 | public function model($model = null) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Set or get parameter layout version |
||
| 277 | * @param string $version |
||
| 278 | * @return string |
||
| 279 | * @throws InvalidArgumentException |
||
| 280 | */ |
||
| 281 | 58 | public function version($version = null) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Recover cUF number from state acronym |
||
| 302 | * @param string $acronym Sigla do estado |
||
| 303 | * @return int number cUF |
||
| 304 | */ |
||
| 305 | public function getcUF($acronym) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Recover state acronym from cUF number |
||
| 312 | * @param int $cUF |
||
| 313 | * @return string acronym sigla |
||
| 314 | */ |
||
| 315 | public function getAcronym($cUF) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Validate cUF from the key content and returns the state acronym |
||
| 322 | * @param string $chave |
||
| 323 | * @return string |
||
| 324 | * @throws InvalidArgumentException |
||
| 325 | */ |
||
| 326 | public function validKeyByUF($chave) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Sign NFe or NFCe |
||
| 339 | * @param string $xml NFe xml content |
||
| 340 | * @return string signed NFe xml |
||
| 341 | * @throws RuntimeException |
||
| 342 | */ |
||
| 343 | public function signNFe($xml) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Corrects NFe fields when in contingency mode |
||
| 377 | * @param string $xml NFe xml content |
||
| 378 | * @return string |
||
| 379 | */ |
||
| 380 | protected function correctNFeForContingencyMode($xml) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Performs xml validation with its respective XSD structure definition document |
||
| 387 | * NOTE: if don't exists the XSD file will return true |
||
| 388 | * @param string $version layout version |
||
| 389 | * @param string $body |
||
| 390 | * @param string $method |
||
| 391 | * @return bool |
||
| 392 | */ |
||
| 393 | protected function isValid($version, $body, $method) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Verifies the existence of the service |
||
| 404 | * @param string $service |
||
| 405 | * @throws RuntimeException |
||
| 406 | */ |
||
| 407 | protected function checkContingencyForWebServices($service) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Alter environment from "homologacao" to "producao" and vice-versa |
||
| 442 | * @param int $tpAmb |
||
| 443 | * @return void |
||
| 444 | */ |
||
| 445 | 58 | public function setEnvironment($tpAmb = 2) |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Set option for canonical transformation see C14n |
||
| 455 | * @param array $opt |
||
| 456 | * @return array |
||
| 457 | */ |
||
| 458 | public function canonicalOptions(array $opt = [true, false, null, null]) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Assembles all the necessary parameters for soap communication |
||
| 468 | * @param string $service |
||
| 469 | * @param string $uf |
||
| 470 | * @param int $tpAmb 1-Production or 2-Homologation |
||
| 471 | * @param bool $ignoreContingency |
||
| 472 | * @throws RuntimeException |
||
| 473 | * @return void |
||
| 474 | */ |
||
| 475 | protected function servico($service, $uf, $tpAmb, $ignoreContingency = false) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Send request message to webservice |
||
| 505 | * @param string $request |
||
| 506 | * @param array $parameters |
||
| 507 | * @return string |
||
| 508 | * @throws RuntimeException |
||
| 509 | */ |
||
| 510 | protected function sendRequest($request, array $parameters = []) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Recover path to xml data base with list of soap services |
||
| 530 | * @return string |
||
| 531 | */ |
||
| 532 | protected function getXmlUrlPath() |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Add QRCode Tag to signed XML from a NFCe |
||
| 548 | * @param DOMDocument $dom |
||
| 549 | * @return string |
||
| 550 | */ |
||
| 551 | protected function addQRCode(DOMDocument $dom) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Get URI for search NFCe by key (chave) |
||
| 576 | * @param string $uf Abbreviation of the UF |
||
| 577 | * @param string $tpAmb SEFAZ environment, 1-Production or 2-Homologation |
||
| 578 | * @return string |
||
| 579 | */ |
||
| 580 | 54 | protected function getURIConsultaNFCe($uf, $tpAmb) |
|
| 586 | |||
| 587 | /** |
||
| 588 | * Verify if SOAP class is loaded, if not, force load SoapCurl |
||
| 589 | */ |
||
| 590 | protected function checkSoap() |
||
| 596 | } |
||
| 597 |