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 |
||
| 32 | class Tools |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * config class |
||
| 36 | * @var \stdClass |
||
| 37 | */ |
||
| 38 | public $config; |
||
| 39 | /** |
||
| 40 | * Path to storage folder |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | public $pathwsfiles = ''; |
||
| 44 | /** |
||
| 45 | * Path to schemes folder |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | public $pathschemes = ''; |
||
| 49 | /** |
||
| 50 | * ambiente |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | public $ambiente = 'homologacao'; |
||
| 54 | /** |
||
| 55 | * Environment |
||
| 56 | * @var int |
||
| 57 | */ |
||
| 58 | public $tpAmb = 2; |
||
| 59 | /** |
||
| 60 | * soap class |
||
| 61 | * @var SoapInterface |
||
| 62 | */ |
||
| 63 | public $soap; |
||
| 64 | /** |
||
| 65 | * Application version |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | public $verAplic = ''; |
||
| 69 | /** |
||
| 70 | * last soap request |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | public $lastRequest = ''; |
||
| 74 | /** |
||
| 75 | * last soap response |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | public $lastResponse = ''; |
||
| 79 | /** |
||
| 80 | * certificate class |
||
| 81 | * @var Certificate |
||
| 82 | */ |
||
| 83 | protected $certificate; |
||
| 84 | /** |
||
| 85 | * Sign algorithm from OPENSSL |
||
| 86 | * @var int |
||
| 87 | */ |
||
| 88 | protected $algorithm = OPENSSL_ALGO_SHA1; |
||
| 89 | /** |
||
| 90 | * Canonical conversion options |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | protected $canonical = [true, false, null, null]; |
||
| 94 | /** |
||
| 95 | * Model of MDFe 58 |
||
| 96 | * @var int |
||
| 97 | */ |
||
| 98 | protected $modelo = 58; |
||
| 99 | /** |
||
| 100 | * Version of layout |
||
| 101 | * @var string |
||
| 102 | */ |
||
| 103 | protected $versao = '3.00'; |
||
| 104 | /** |
||
| 105 | * urlPortal |
||
| 106 | * Instância do WebService |
||
| 107 | * |
||
| 108 | * @var string |
||
| 109 | */ |
||
| 110 | protected $urlPortal = 'http://www.portalfiscal.inf.br/mdfe'; |
||
| 111 | /** |
||
| 112 | * urlcUF |
||
| 113 | * @var string |
||
| 114 | */ |
||
| 115 | protected $urlcUF = ''; |
||
| 116 | /** |
||
| 117 | * urlVersion |
||
| 118 | * @var string |
||
| 119 | */ |
||
| 120 | protected $urlVersion = ''; |
||
| 121 | /** |
||
| 122 | * urlService |
||
| 123 | * @var string |
||
| 124 | */ |
||
| 125 | protected $urlService = ''; |
||
| 126 | /** |
||
| 127 | * @var string |
||
| 128 | */ |
||
| 129 | protected $urlMethod = ''; |
||
| 130 | /** |
||
| 131 | * @var string |
||
| 132 | */ |
||
| 133 | protected $urlOperation = ''; |
||
| 134 | /** |
||
| 135 | * @var string |
||
| 136 | */ |
||
| 137 | protected $urlNamespace = ''; |
||
| 138 | /** |
||
| 139 | * @var string |
||
| 140 | */ |
||
| 141 | protected $urlAction = ''; |
||
| 142 | /** |
||
| 143 | * @var \SOAPHeader |
||
| 144 | */ |
||
| 145 | protected $objHeader; |
||
| 146 | /** |
||
| 147 | * @var string |
||
| 148 | */ |
||
| 149 | protected $urlHeader = ''; |
||
| 150 | /** |
||
| 151 | * @var array |
||
| 152 | */ |
||
| 153 | /** |
||
| 154 | * @var string |
||
| 155 | */ |
||
| 156 | protected $typePerson = 'J'; |
||
| 157 | |||
| 158 | protected $soapnamespaces = [ |
||
| 159 | 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance", |
||
| 160 | 'xmlns:xsd' => "http://www.w3.org/2001/XMLSchema", |
||
| 161 | 'xmlns:soap' => "http://www.w3.org/2003/05/soap-envelope" |
||
| 162 | ]; |
||
| 163 | /** |
||
| 164 | * @var array |
||
| 165 | */ |
||
| 166 | protected $availableVersions = [ |
||
| 167 | '3.00' => 'PL_MDFe_300a' |
||
| 168 | ]; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Constructor |
||
| 172 | * load configurations, |
||
| 173 | * load Digital Certificate, |
||
| 174 | * map all paths, |
||
| 175 | * set timezone and |
||
| 176 | * @param string $configJson content of config in json format |
||
| 177 | * @param Certificate $certificate |
||
| 178 | */ |
||
| 179 | public function __construct($configJson, Certificate $certificate) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Sets environment time zone |
||
| 194 | * @param string $acronym (ou seja a sigla do estado) |
||
| 195 | * @return void |
||
| 196 | */ |
||
| 197 | public function setEnvironmentTimeZone($acronym) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Set application version |
||
| 204 | * @param string $ver |
||
| 205 | */ |
||
| 206 | public function setVerAplic($ver) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Return J or F from existing type in ASN.1 certificate |
||
| 213 | * J - pessoa juridica (CNPJ) |
||
| 214 | * F - pessoa física (CPF) |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public function getTypeOfPersonFromCertificate() |
||
| 234 | |||
| 235 | |||
| 236 | /** |
||
| 237 | * Load Soap Class |
||
| 238 | * Soap Class may be \NFePHP\Common\Soap\SoapNative |
||
| 239 | * or \NFePHP\Common\Soap\SoapCurl |
||
| 240 | * @param SoapInterface $soap |
||
| 241 | * @return void |
||
| 242 | */ |
||
| 243 | public function loadSoapClass(SoapInterface $soap) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Set OPENSSL Algorithm using OPENSSL constants |
||
| 251 | * @param int $algorithm |
||
| 252 | * @return void |
||
| 253 | */ |
||
| 254 | public function setSignAlgorithm($algorithm = OPENSSL_ALGO_SHA1) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Set or get parameter layout version |
||
| 261 | * @param string $version |
||
| 262 | * @return string |
||
| 263 | * @throws InvalidArgumentException |
||
| 264 | */ |
||
| 265 | public function version($version = '') |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Recover cUF number from state acronym |
||
| 282 | * @param string $acronym Sigla do estado |
||
| 283 | * @return int number cUF |
||
| 284 | */ |
||
| 285 | public function getcUF($acronym) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Recover state acronym from cUF number |
||
| 292 | * @param int $cUF |
||
| 293 | * @return string acronym sigla |
||
| 294 | */ |
||
| 295 | public function getAcronym($cUF) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Validate cUF from the key content and returns the state acronym |
||
| 302 | * @param string $chave |
||
| 303 | * @return string |
||
| 304 | * @throws InvalidArgumentException |
||
| 305 | */ |
||
| 306 | public function validKeyByUF($chave) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Sign MDFe |
||
| 319 | * @param string $xml MDFe xml content |
||
| 320 | * @return string singed MDFe xml |
||
| 321 | * @throws RuntimeException |
||
| 322 | */ |
||
| 323 | public function signMDFe($xml) |
||
| 366 | |||
| 367 | public function getModalXML($dom, $modal) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Performs xml validation with its respective |
||
| 376 | * XSD structure definition document |
||
| 377 | * NOTE: if dont exists the XSD file will return true |
||
| 378 | * @param string $version layout version |
||
| 379 | * @param string $body |
||
| 380 | * @param string $method |
||
| 381 | * @return boolean |
||
| 382 | */ |
||
| 383 | protected function isValid($version, $body, $method) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Alter environment from "homologacao" to "producao" and vice-versa |
||
| 397 | * @param int $tpAmb |
||
| 398 | * @return void |
||
| 399 | */ |
||
| 400 | public function setEnvironment($tpAmb = 2) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Set option for canonical transformation see C14n |
||
| 410 | * @param array $opt |
||
| 411 | * @return array |
||
| 412 | */ |
||
| 413 | public function canonicalOptions($opt = [true, false, null, null]) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Assembles all the necessary parameters for soap communication |
||
| 423 | * @param string $service |
||
| 424 | * @param string $uf |
||
| 425 | * @param string $tpAmb |
||
| 426 | * @return void |
||
| 427 | */ |
||
| 428 | protected function servico( |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Send request message to webservice |
||
| 491 | * @param array $parameters |
||
| 492 | * @param string $request |
||
| 493 | * @return string |
||
| 494 | */ |
||
| 495 | protected function sendRequest($request, array $parameters = []) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Recover path to xml data base with list of soap services |
||
| 512 | * @return string |
||
| 513 | */ |
||
| 514 | protected function getXmlUrlPath() |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Add QRCode Tag to signed XML from a NFCe |
||
| 527 | * @param DOMDocument $dom |
||
| 528 | * @return string |
||
| 529 | */ |
||
| 530 | protected function addQRCode(DOMDocument $dom) |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Verify if SOAP class is loaded, if not, force load SoapCurl |
||
| 540 | */ |
||
| 541 | protected function checkSoap() |
||
| 547 | } |
||
| 548 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.