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 CTe 57 or 67 |
||
| 103 | * @var int |
||
| 104 | */ |
||
| 105 | protected $modelo = 57; |
||
| 106 | /** |
||
| 107 | * Version of layout |
||
| 108 | * @var string |
||
| 109 | */ |
||
| 110 | protected $versao = '3.00'; |
||
| 111 | /** |
||
| 112 | * urlPortal |
||
| 113 | * Instância do WebService |
||
| 114 | * |
||
| 115 | * @var string |
||
| 116 | */ |
||
| 117 | protected $urlPortal = 'http://www.portalfiscal.inf.br/cte'; |
||
| 118 | /** |
||
| 119 | * urlcUF |
||
| 120 | * @var string |
||
| 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 |
||
| 151 | */ |
||
| 152 | protected $objHeader; |
||
| 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 = [ |
||
| 169 | '3.00' => 'PL_CTe_300a' |
||
| 170 | ]; |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Constructor |
||
| 174 | * load configurations, |
||
| 175 | * load Digital Certificate, |
||
| 176 | * map all paths, |
||
| 177 | * set timezone and |
||
| 178 | * and instanciate Contingency::class |
||
| 179 | * @param string $configJson content of config in json format |
||
| 180 | * @param Certificate $certificate |
||
| 181 | */ |
||
| 182 | public function __construct($configJson, Certificate $certificate) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Sets environment time zone |
||
| 197 | * @param string $acronym (ou seja a sigla do estado) |
||
| 198 | * @return void |
||
| 199 | */ |
||
| 200 | public function setEnvironmentTimeZone($acronym) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Set application version |
||
| 207 | * @param string $ver |
||
| 208 | */ |
||
| 209 | public function setVerAplic($ver) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Load Soap Class |
||
| 216 | * Soap Class may be \NFePHP\Common\Soap\SoapNative |
||
| 217 | * or \NFePHP\Common\Soap\SoapCurl |
||
| 218 | * @param SoapInterface $soap |
||
| 219 | * @return void |
||
| 220 | */ |
||
| 221 | public function loadSoapClass(SoapInterface $soap) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Set OPENSSL Algorithm using OPENSSL constants |
||
| 229 | * @param int $algorithm |
||
| 230 | * @return void |
||
| 231 | */ |
||
| 232 | public function setSignAlgorithm($algorithm = OPENSSL_ALGO_SHA1) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Set or get model of document CTe = 57 or CTeOS = 67 |
||
| 239 | * @param int $model |
||
| 240 | * @return int modelo class parameter |
||
| 241 | */ |
||
| 242 | public function model($model = null) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Set or get parameter layout version |
||
| 252 | * @param string $version |
||
| 253 | * @return string |
||
| 254 | * @throws InvalidArgumentException |
||
| 255 | */ |
||
| 256 | public function version($version = '') |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Recover cUF number from state acronym |
||
| 273 | * @param string $acronym Sigla do estado |
||
| 274 | * @return int number cUF |
||
| 275 | */ |
||
| 276 | public function getcUF($acronym) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Recover state acronym from cUF number |
||
| 283 | * @param int $cUF |
||
| 284 | * @return string acronym sigla |
||
| 285 | */ |
||
| 286 | public function getAcronym($cUF) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Validate cUF from the key content and returns the state acronym |
||
| 293 | * @param string $chave |
||
| 294 | * @return string |
||
| 295 | * @throws InvalidArgumentException |
||
| 296 | */ |
||
| 297 | public function validKeyByUF($chave) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Sign CTe |
||
| 310 | * @param string $xml CTe xml content |
||
| 311 | * @return string singed CTe xml |
||
| 312 | * @throws RuntimeException |
||
| 313 | */ |
||
| 314 | public function signCTe($xml) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param string $Dom CTe xml content |
||
| 393 | * @param string $xml CTe xml content |
||
| 394 | * @return string|bool |
||
| 395 | * @todo |
||
| 396 | * Retorna o xml do modal especifico |
||
| 397 | */ |
||
| 398 | public function getModalXML($dom, $modal) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @todo |
||
| 410 | * Corret NFe fields when in contingency mode is set |
||
| 411 | * @param string $xml CTe xml content |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | protected function correctCTeForContingencyMode($xml) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Performs xml validation with its respective |
||
| 425 | * XSD structure definition document |
||
| 426 | * NOTE: if dont exists the XSD file will return true |
||
| 427 | * @param string $version layout version |
||
| 428 | * @param string $body |
||
| 429 | * @param string $method |
||
| 430 | * @return boolean |
||
| 431 | */ |
||
| 432 | protected function isValid($version, $body, $method) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Verifies the existence of the service |
||
| 446 | * @param string $service |
||
| 447 | * @throws RuntimeException |
||
| 448 | */ |
||
| 449 | protected function checkContingencyForWebServices($service) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Alter environment from "homologacao" to "producao" and vice-versa |
||
| 470 | * @param int $tpAmb |
||
| 471 | * @return void |
||
| 472 | */ |
||
| 473 | public function setEnvironment($tpAmb = 2) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Set option for canonical transformation see C14n |
||
| 483 | * @param array $opt |
||
| 484 | * @return array |
||
| 485 | */ |
||
| 486 | public function canonicalOptions($opt = [true, false, null, null]) |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Assembles all the necessary parameters for soap communication |
||
| 496 | * @param string $service |
||
| 497 | * @param string $uf |
||
| 498 | * @param string $tpAmb |
||
| 499 | * @param bool $ignoreContingency |
||
| 500 | * @return void |
||
| 501 | */ |
||
| 502 | protected function servico( |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Send request message to webservice |
||
| 576 | * @param array $parameters |
||
| 577 | * @param string $request |
||
| 578 | * @return string |
||
| 579 | */ |
||
| 580 | protected function sendRequest($request, array $parameters = []) |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Recover path to xml data base with list of soap services |
||
| 597 | * @return string |
||
| 598 | */ |
||
| 599 | protected function getXmlUrlPath() |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Add QRCode Tag to signed XML from a NFCe |
||
| 612 | * @param DOMDocument $dom |
||
| 613 | * @return string |
||
| 614 | */ |
||
| 615 | protected function addQRCode(DOMDocument $dom) |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Verify if SOAP class is loaded, if not, force load SoapCurl |
||
| 649 | */ |
||
| 650 | protected function checkSoap() |
||
| 656 | } |
||
| 657 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: